-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (44 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
function longcut(obj) {
function result(path) {
let _arguments = Array.prototype.slice.call(arguments);
if(obj === undefined || obj === null) {
return longcut(obj);
}
if(path instanceof Function) {
let result;
let add;
if(obj instanceof Array) {
result = [];
add = (k, v) => { result.push(v); };
} else {
result = {};
add = (k, v) => { result[k] = v; };
}
for(let k in obj) {
let v = path(obj[k], k, obj);
if(v) {
add(k, v);
}
}
return longcut(result);
} else if(path instanceof RegExp) {
if(obj instanceof Array) {
return longcut(obj)((v) => { return path.test(v) ? v : undefined });
} else if(obj instanceof Object) {
return longcut(obj)((v, k) => { return path.test(k) ? v : undefined });
} else if(typeof obj === 'string') {
return longcut(obj.replace.apply(obj, _arguments));
} else {
throw new Error("Unsupported object type for RexExp match: " + typeof obj);
}
} else {
return longcut(obj[path]);
}
}
result.val = () => {
return obj;
};
return result;
}
module.exports.longcut = longcut;