-
Notifications
You must be signed in to change notification settings - Fork 1
/
deep.js
138 lines (133 loc) · 3.68 KB
/
deep.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Generated by CoffeeScript 1.6.3
(function() {
var deep, puts, _,
__slice = [].slice;
_ = require('underscore');
puts = require('util').puts;
deep = {
isPlainObject: function(obj) {
if ((obj != null ? obj.constructor : void 0) == null) {
return false;
}
return obj.constructor.name === 'Object';
},
clone: function(obj) {
var clone, k, v, _i, _len;
if (_.isArray(obj)) {
clone = [];
for (_i = 0, _len = obj.length; _i < _len; _i++) {
v = obj[_i];
clone.push(deep.clone(v));
}
return clone;
} else if (deep.isPlainObject(obj)) {
clone = {};
for (k in obj) {
v = obj[k];
clone[k] = deep.clone(v);
}
return clone;
} else {
return obj;
}
},
equals: function(a, b) {
var i, k, size_a, _i, _ref;
if (a === b) {
return true;
} else if (_.isArray(a)) {
if (!(_.isArray(b) && a.length === b.length)) {
return false;
}
for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
if (!deep.equals(a[i], b[i])) {
return false;
}
}
return true;
} else if (deep.isPlainObject(a)) {
size_a = _.size(a);
if (!(deep.isPlainObject(b) && size_a === _.size(b))) {
return false;
}
for (k in a) {
if (!deep.equals(a[k], b[k])) {
return false;
}
}
return true;
} else {
return false;
}
},
extend: function() {
var destination, k, source, sources, _i, _len;
destination = arguments[0], sources = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
source = sources[_i];
for (k in source) {
if (deep.isPlainObject(destination[k]) && deep.isPlainObject(source[k])) {
deep.extend(destination[k], source[k]);
} else {
destination[k] = deep.clone(source[k]);
}
}
}
return destination;
},
select: function(root, filter, path) {
var elementPath, k, selected, v;
if (path == null) {
path = [];
}
selected = [];
if (filter(root)) {
selected.push({
path: path,
value: root
});
} else if (_.isObject(root)) {
for (k in root) {
v = root[k];
elementPath = _.clone(path);
elementPath.push(k);
selected = selected.concat(deep.select(v, filter, elementPath));
}
}
return selected;
},
set: function(root, path, value) {
var lastPath, pathElement, _i, _len;
path = _.clone(path);
lastPath = path.pop();
for (_i = 0, _len = path.length; _i < _len; _i++) {
pathElement = path[_i];
root = root[pathElement];
}
return root[lastPath] = value;
},
transform: function(obj, filter, transform) {
var k, transformed, v, _i, _len;
if (filter(obj)) {
return transform(obj);
} else if (_.isArray(obj)) {
transformed = [];
for (_i = 0, _len = obj.length; _i < _len; _i++) {
v = obj[_i];
transformed.push(deep.transform(v, filter, transform));
}
return transformed;
} else if (deep.isPlainObject(obj)) {
transformed = {};
for (k in obj) {
v = obj[k];
transformed[k] = deep.transform(v, filter, transform);
}
return transformed;
} else {
return obj;
}
}
};
module.exports = deep;
}).call(this);