-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlodash-like.js
80 lines (62 loc) · 1.76 KB
/
lodash-like.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
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
79
80
'use strict';
var _ = require('lodash');
function toPath(object) {
function _keysDeep(parent, child, finalLeaves) {
var prefix = (parent.length > 0 ? parent + '.' : '');
_.each(child, function(value, keyName) {
if (typeof value === 'object' && value !== null) {
_keysDeep(prefix + keyName, value, finalLeaves);
} else {
finalLeaves[prefix + keyName] = value;
}
});
return finalLeaves;
}
return _keysDeep('', object, {});
}
function keysDeep(object) {
function _keysDeep(parent, child, finalLeaves) {
var prefix = (parent.length > 0 ? parent + '.' : '');
_.each(child, function(value, keyName) {
if (typeof value === 'object' && value !== null) {
_keysDeep(prefix + keyName, value, finalLeaves);
} else {
finalLeaves.push(prefix + keyName);
}
});
return finalLeaves;
}
return _keysDeep('', object, []);
}
function unassign() {
var ignoreProperties;
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
var filteredResult = args.shift();
while ((ignoreProperties = args.shift()) !== undefined) {
keysDeep(ignoreProperties).forEach(function(ignoreProperty) {
_.unset(filteredResult, ignoreProperty);
});
}
return filteredResult;
}
function sortObjectDeep(source, doReverse) {
var keys = _.keys(source).sort();
var dest = {};
if (doReverse) {
keys.reverse();
}
if (_.isEmpty(keys)) {
return null;
}
_.each(keys, function(key) {
dest[key] = (typeof source[key] === 'object' ?
sortObjectDeep(source[key], doReverse) :
source[key]
);
});
return dest;
}
exports.toPath = toPath;
exports.keysDeep = keysDeep;
exports.unassign = unassign;
exports.sortObjectDeep = sortObjectDeep;