-
Notifications
You must be signed in to change notification settings - Fork 7
/
deep.js
525 lines (459 loc) · 14 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
export {
deepCopy,
deepCopyAdded,
deepAssign,
deepAssignAdded,
deepEqual,
deepEqualAdded,
deepDifference,
};
/**
only works with undefined, null, Number, Symbol, String, Big Int, Object, Array,
warning
will enter infite recursion with cyclic objects
does not copy internal links
does not work with anything created with new
*/
const deepCopy = x => {
if (typeof x !== `object` || x === null) {
return x;
}
if (Array.isArray(x)) {
return x.map(deepCopy);
}
const copy = {};
Object.entries(x).forEach(([key, value]) => {
copy[key] = deepCopy(value);
});
return copy;
};
/**
like deepCopy but supports more types
works with
undefined, null, Number, Symbol, String, Big Int,
Object, Array,
Date, RegExp, Set, Map,
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
Int8Array, Int16Array, Int32Array
warning
will enter infite recursion with cyclic objects
does not copy internal links
*/
const deepCopyAdded = x => {
if (typeof x !== `object` || x === null) {
return x;
}
if (x instanceof Date) {
return new Date(x);
}
if (x instanceof RegExp) {
return new RegExp(x);
}
if (x instanceof Set) {
return new Set(Array.from(x, deepCopyAdded));
}
if (x instanceof Map) {
const map = new Map();
// todo keep internal links
x.forEach((value, key) => {
map.set(deepCopyAdded(key), deepCopyAdded(value));
});
return map;
}
if (Array.isArray(x)) {
return x.map(deepCopy);
}
if (ArrayBuffer.isView(x) && !(x instanceof DataView)) {
return new x.constructor(x);
}
const copy = {};
Object.entries(x).forEach(([key, value]) => {
copy[key] = deepCopy(value);
});
return copy;
};
/**
Like Object.assign but deep,
does not try to assign partial arrays inside, they are overwritten
only works with undefined, null, Number, Symbol, String, Big Int, Object, Array,
warning
will enter infite recursion with cyclic objects
does not copy internal links
does not work with anything created with new
@param {Object} target must be an object
@param {Object} source1 should be an object, silently discards if not (like Object.assign)
@return {Object} target
*/
const deepAssign = (target, ...sources) => {
sources.forEach(source => {
if (!source || typeof source !== `object`) {
return;
}
Object.entries(source).forEach(([key, value]) => {
if (key === `__proto__`) {
return;
}
if (typeof value !== `object` || value === null) {
target[key] = value;
return;
}
if (Array.isArray(value)) {
target[key] = [];
}
// value is an Object
if (typeof target[key] !== `object` || !target[key]) {
target[key] = {};
}
deepAssign(target[key], value);
});
});
return target;
};
/**
Like deepAssign but supports more types,
does not try to assign partial arrays inside, they are overwritten
works with
undefined, null, Number, Symbol, String, Big Int,
Object, Array,
Date, RegExp, Set, Map,
Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
Int8Array, Int16Array, Int32Array
warning
will enter infite recursion with cyclic objects
does not copy internal links
@param {Object} target must be an object
@param {Object} source1 should be an object, silently discards if not (like Object.assign)
@return {Object} target
*/
const deepAssignAdded = (target, ...sources) => {
sources.forEach(source => {
if (!source || typeof source !== `object`) {
return;
}
Object.entries(source).forEach(([key, value]) => {
if (key === `__proto__`) {
return;
}
if (typeof value !== `object` || value === null) {
target[key] = value;
return;
}
if (value instanceof Date) {
target[key] = new Date(value);
return;
}
if (value instanceof RegExp) {
target[key] = new RegExp(value);
return;
}
if (value instanceof Set) {
let tempArray = Array.from(value, deepCopyAdded);
if (target[key] instanceof Set) {
tempArray = tempArray.concat(Array.from(target[key]));
}
target[key] = new Set(tempArray);
return;
}
if (value instanceof Map) {
let map;
if (target[key] instanceof Map) {
map = target[key];
} else {
map = new Map();
target[key] = map;
}
// todo keep internal links
value.forEach((intlValue, intlKey) => {
map.set(deepCopyAdded(intlKey), deepCopyAdded(intlValue));
});
return;
}
if (Array.isArray(value)) {
target[key] = [];
}
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
target[key] = new value.constructor(value);
return;
}
// value is an Object
if (typeof target[key] !== `object` || !target[key]) {
target[key] = {};
}
deepAssignAdded(target[key], value);
});
});
return target;
};
/**
works with
undefined, null, Number, Symbol, String, Big Int,
Object, Array,
warning
will enter infite recursion with cyclic objects
* @param {Object} a can be either an object or array
* @param {Object} b can be either an object or array
* @returns {Boolean}
*/
const deepEqual = (a, b) => {
if (a === b) {
return true;
}
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length !== b.length) {
return false;
}
return a.every((value, index) => {
return deepEqual(value, b[index]);
});
}
if (Array.isArray(b)) {
return false;
}
if (isObject(a) && isObject(b)) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
return (
deepEqual(keysA, keysB) &&
keysA.every(key => {
return deepEqual(a[key], b[key]);
})
);
}
return false;
};
/**
works with
undefined, null, Number, Symbol, String, Big Int,
Object, Array,
Date, RegExp, Set, Map,
Uint8Array, Uint16Array, Uint32Array,
Int8Array, Int16Array, Int32Array
warning
will enter infite recursion with cyclic objects
* @param {Object} a can be either an object or array
* @param {Object} b can be either an object or array
* @returns {Boolean}
*/
const deepEqualAdded = (a, b) => {
if (a === b) {
return true;
}
if (a instanceof Date) {
if (!(b instanceof Date)) {
return false;
}
return (a.getTime() === b.getTime());
}
if (a instanceof RegExp) {
if (!(b instanceof RegExp)) {
return false;
}
return String(a) === String(b);
}
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
return validateArrayLike(a, b);
}
if (Array.isArray(b)) {
return false;
}
if ((a instanceof Uint8Array) ||
(a instanceof Uint8ClampedArray) ||
(a instanceof Uint16Array) ||
(a instanceof Uint32Array) ||
(a instanceof Int8Array) ||
(a instanceof Int16Array) ||
(a instanceof Int32Array)) {
if (!(b instanceof a.constructor) && !(a instanceof b.constructor)) {
return false;
}
return validateArrayLike(a, b);
}
if ((a instanceof Set)) {
if (!(b instanceof Set)) {
return false;
}
// Sets have size, not length
const arr1 = Array.from(a);
const arr2 = Array.from(b);
return validateArrayLike(arr1, arr2);
}
if (a instanceof Map) {
if (!(b instanceof Map)) {
return false;
}
if (a.size !== b.size) {
return false;
}
const keysA = a.keys();
for (const key of keysA) {
// todo don't check for strict equal key, use deepEqual
if (!b.has(key) || !deepEqualAdded(a.get(key), b.get(key))) {
return false;
}
}
return true;
}
if (isObject(a) && isObject(b)) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
return (
deepEqualAdded(keysA, keysB) &&
keysA.every(key => {
return deepEqualAdded(a[key], b[key]);
})
);
}
return false;
};
/**
*
* warning
will enter infite recursion with cyclic objects
* @function deepDifference finds the differences betwenn two objects
* @param {*} obj1 The first object
* @param {*} obj2 The second object
* @var {Object} deepDifferences contains the differences results
* @return {deepDifferences} returns an Object of differences
*/
const deepDifference = function(obj1, obj2) {
if (!obj2 || typeof obj2 !== `object`) {
return obj1;
}
const deepDifferences = {
additions: [],
removals: [],
changes: [],
};
/**
* @function compare two items and push non-matches to object
* @param {*} item1 The first item
* @param {*} item2 The second item
* @param {String} key The key in our object
*
* Grab the object types for better comparison, since arrays by default return type object
* @var {*} type1 the object type for the first item
* @var {*} type2 the object type for the first item
*/
const compare = function(item1, item2, key) {
const type1 = Object.prototype.toString.call(item1);
const type2 = Object.prototype.toString.call(item2);
//if item2 has undefined type, assign null to its value
if (type2 === `[object Undefined]`) {
const nameArray = [];
nameArray.push(key);
const changed = { name: nameArray, oldValue: item1, newValue: null };
deepDifferences.changes.push(changed);
return;
}
//if object call deepDifference recursively
if (type1 === `[object Object]`) {
const nameArray = [];
nameArray.push(key);
const objdeepDifference = deepDifference(item1, item2);
if (objdeepDifference.additions.length > 0) {
nameArray.push(objdeepDifference.additions[0].name[0]);
const added = {
name: nameArray,
value: objdeepDifference.additions[0].value,
};
deepDifferences.additions.push(added);
}
if (objdeepDifference.removals.length > 0) {
nameArray.push(objdeepDifference.removals[0].name[0]);
const removed = {
name: nameArray,
value: objdeepDifference.removals[0].value,
};
deepDifferences.removals.push(removed);
}
return;
}
//if function. convert to string and compare
if (type1 === `[object Function]`) {
if (item1.toString() !== item2.toString()) {
const nameArray = [];
nameArray.push(key);
const changed = { name: nameArray, oldValue: item1, newValue: item2 };
deepDifferences.changes.push(changed);
}
}
if (type1 === `[object Array]`) {
if (!arraysMatch(item1, item2)) {
const nameArray = [];
nameArray.push(key);
const changed = { name: nameArray, oldValue: item1, newValue: item2 };
deepDifferences.changes.push(changed);
}
return;
}
if (item1 !== item2) {
const nameArray = [];
nameArray.push(key);
const changed = { name: nameArray, oldValue: item1, newValue: item2 };
deepDifferences.changes.push(changed);
return;
}
if (type1 !== type2) {
const nameArray = [];
nameArray.push(key);
const changed = { name: nameArray, oldValue: item1, newValue: item2 };
deepDifferences.changes.push(changed);
return;
}
};
Object.keys(obj1).forEach(key => {
/**
* If obj2 is missing a property in obj1
* add property to removals array
**/
if (!Object.hasOwn(obj2, key)) {
const nameArray = [];
nameArray.push(key);
const removed = { name: nameArray, value: obj1[key] };
deepDifferences.removals.push(removed);
return;
}
compare(obj1[key], obj2[key], key);
});
Object.keys(obj2).forEach(key => {
if (!Object.hasOwn(obj1, key)) {
const nameArray = [];
nameArray.push(key);
const added = { name: nameArray, value: obj2[key] };
deepDifferences.additions.push(added);
return;
}
});
return deepDifferences;
};
const arraysMatch = function(arr1, arr2) {
// Check if the arrays are the same length
if (arr1.length !== arr2.length) {
return false;
}
// Check if all items exist and are in the same order
for (let i = 0; i < arr1.length; i = +1) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
// Otherwise, return true
return true;
};
const isObject = x => {
return typeof x === `object` && x !== null;
};
const validateArrayLike = (a, b) => {
return (
(a.length === b.length) &&
(a.every((value, index) => {
return deepEqualAdded(value, b[index]);
}))
);
};