Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (69 loc) · 1.56 KB
/
Copy pathindex.js
File metadata and controls
78 lines (69 loc) · 1.56 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
function look(obj, key) {
if (!obj || !key) return;
var segs = key.split('.');
while (segs.length) {
obj = obj[segs.shift()];
if (!obj) return;
}
return obj;
}
function transform(src, schema) {
if (!src || !schema) return;
var obj = {};
function _transform(val) {
switch (typeOf(val)) {
case 'object':
return transform(src, val);
case 'array':
return val.map(_transform);
case 'function':
return val.call(obj, src);
default:
return look(src, val);
}
}
Object.keys(schema).forEach(function (key) {
obj[key] = _transform(schema[key]);
});
return obj;
}
function without(src, props) {
if (!src || !props) return src;
switch (typeOf(props)) {
case 'string':
props = props.split(' ').map(function (prop) {
return prop.trim();
});
break;
case 'object':
props = Object.keys(props);
break;
}
return Object.keys(src).reduce(function (obj, key) {
if (!~props.indexOf(key)) obj[key] = src[key];
return obj;
}, {});
}
function typeOf(e) {
return Object.prototype.toString.call(e)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase();
}
function pluck(arr, prop) {
return (arr || []).reduce(function (memo, e) {
e[prop] && memo.push(e[prop]);
return memo;
}, []);
}
function values(obj) {
return Object.keys(obj).map(function (key) {
return obj[key];
});
}
exports.look = look;
exports.transform = transform;
exports.without = without;
exports.typeOf = typeOf;
exports.pluck = pluck;
exports.values = values;