-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.js
196 lines (172 loc) · 4.73 KB
/
object.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module.exports = {
merge: merge
, deepCopy: deepCopy
, extract: extract
, deepEqual: deepEqual
, only: objectWithOnly
, filter: filter
, assign: assign
, matchingVal: matchingVal
, lookup: lookup
};
function merge () {
var merged = {};
for (var i = 0, l = arguments.length; i < l; i++) {
var obj = arguments[i];
for (var k in obj) {
merged[k] = obj[k];
}
}
return merged;
}
// TODO Test this
function deepCopy (obj) {
if (obj === null) return null;
if (typeof obj === 'object') {
var copy;
if (Array.isArray(obj)) {
copy = [];
for (var i = obj.length; i--; ) copy[i] = deepCopy(obj[i]);
return copy;
}
copy = {}
for (var k in obj) copy[k] = deepCopy(obj[k]);
return copy;
}
return obj;
}
function extract (key, obj) {
return obj[key];
}
/**
* Modified from node's assert.js
*/
function deepEqual (actual, expected, ignore) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) return true;
// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
if (actual instanceof Date && expected instanceof Date)
return actual.getTime() === expected.getTime();
if (typeof actual === 'function' && typeof expected === 'function')
return actual === expected || actual.toString() === expected.toString();
// 7.3. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
if (typeof actual !== 'object' && typeof expected !== 'object')
return actual === expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays.
if (ignore) {
var ignoreMap = {}
, i = ignore.length
while (i--) {
ignoreMap[ignore[i]] = true;
}
}
return objEquiv(actual, expected, ignoreMap);
}
/** Private Functions **/
/**
* Modified from node's assert.js
*/
function objEquiv (a, b, ignoreMap) {
var i, key, ka, kb;
if (a == null || b == null) return false;
// an identical 'prototype' property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (isArguments(a)) {
if (! isArguments(b)) return false;
a = pSlice.call(a);
b = pSlice.call(b);
return deepEqual(a, b);
}
try {
if (ignoreMap) {
ka = keysWithout(a, ignoreMap);
kb = keysWithout(b, ignoreMap);
} else {
ka = Object.keys(a);
kb = Object.keys(b);
}
} catch (e) {
// happens when one is a string literal and the other isn't
return false;
}
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length !== kb.length) return false;
// the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
i = ka.length;
while (i--) {
if (ka[i] !== kb[i]) return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
i = ka.length;
while (i--) {
key = ka[i];
if (! deepEqual(a[key], b[key])) return false;
}
return true;
}
function isArguments (obj) {
return toString.call(obj) === '[object Arguments]';
}
function objectWithOnly (obj, paths) {
var projectedDoc = {};
for (var i = 0, l = paths.length; i < l; i++) {
var path = paths[i];
assign(projectedDoc, path, lookup(path, obj));
}
return projectedDoc;
}
function filter (obj, fn) {
var filtered = {};
for (var k in obj) {
var curr = obj[k];
if (fn(curr, k)) filtered[k] = curr;
}
return filtered;
}
function assign (obj, path, val) {
var parts = path.split('.')
, lastIndex = parts.length - 1;
for (var i = 0, l = parts.length; i < l; i++) {
var prop = parts[i];
if(/^\d+$/.test(prop)) {
prop = parseInt(prop, 10);
}
if (i === lastIndex) {
obj[prop] = val;
} else {
obj = obj[prop] || (obj[prop] = {});
}
}
}
function lookup (path, obj) {
if (!obj) return;
if (path.indexOf('.') === -1) return obj[path];
var parts = path.split('.');
for (var i = 0, l = parts.length; i < l; i++) {
if (!obj) return obj;
var prop = parts[i];
obj = obj[prop];
}
return obj;
}
function matchingVal (obj, predicate) {
var val;
for (var k in obj) {
val = obj[k];
if (predicate(k, val)) return val;
}
}