v2 / vlib / v / gen / js / fast_deep_equal.js
72 lines · 57 sloc · 1.87 KB · 11d2b8b354866aef1f7bba8d371beb51f407ff86
Raw
1// https://www.npmjs.com/package/fast-deep-equal - 3/3/2021
2const envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
3function vEq(a, b) {
4 if (a === b) return true;
5
6 if (a && b && typeof a == 'object' && typeof b == 'object') {
7 if (a.constructor !== b.constructor) return false;
8 // we want to convert all V types to JS for comparison.
9 if ('$toJS' in a)
10 a = a.$toJS();
11
12 if ('$toJS' in b)
13 b = b.$toJS();
14
15 var length, i, keys;
16 if (Array.isArray(a)) {
17 length = a.length;
18 if (length != b.length) return false;
19 for (i = length; i-- !== 0;)
20 if (!vEq(a[i], b[i])) return false;
21 return true;
22 }
23
24
25 if ((a instanceof Map) && (b instanceof Map)) {
26 if (a.size !== b.size) return false;
27 for (i of a.entries())
28 if (!b.has(i[0])) return false;
29 for (i of a.entries())
30 if (!vEq(i[1], b.get(i[0]))) return false;
31 return true;
32 }
33
34 if ((a instanceof Set) && (b instanceof Set)) {
35 if (a.size !== b.size) return false;
36 for (i of a.entries())
37 if (!b.has(i[0])) return false;
38 return true;
39 }
40
41 if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
42 length = a.length;
43 if (length != b.length) return false;
44 for (i = length; i-- !== 0;)
45 if (a[i] !== b[i]) return false;
46 return true;
47 }
48
49
50 if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
51 if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
52 if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
53
54 keys = Object.keys(a);
55 length = keys.length;
56 if (length !== Object.keys(b).length) return false;
57
58 for (i = length; i-- !== 0;)
59 if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
60
61 for (i = length; i-- !== 0;) {
62 var key = keys[i];
63
64 if (!vEq(a[key], b[key])) return false;
65 }
66
67 return true;
68 }
69
70 // true if both NaN, false otherwise
71 return a !== a && b !== b;
72};