Debian Codegraph @

node/dequal/dist/index.js

raw
1 var has = Object.prototype.hasOwnProperty;
2
3 function find(iter, tar, key) {
4 for (key of iter.keys()) {
5 if (dequal(key, tar)) return key;
6 }
7 }
8
9 function dequal(foo, bar) {
10 var ctor, len, tmp;
11 if (foo === bar) return true;
12
13 if (foo && bar && (ctor=foo.constructor) === bar.constructor) {
14 if (ctor === Date) return foo.getTime() === bar.getTime();
15 if (ctor === RegExp) return foo.toString() === bar.toString();
16
17 if (ctor === Array) {
18 if ((len=foo.length) === bar.length) {
19 while (len-- && dequal(foo[len], bar[len]));
20 }
21 return len === -1;
22 }
23
24 if (ctor === Set) {
25 if (foo.size !== bar.size) {
26 return false;
27 }
28 for (len of foo) {
29 tmp = len;
30 if (tmp && typeof tmp === 'object') {
31 tmp = find(bar, tmp);
32 if (!tmp) return false;
33 }
34 if (!bar.has(tmp)) return false;
35 }
36 return true;
37 }
38
39 if (ctor === Map) {
40 if (foo.size !== bar.size) {
41 return false;
42 }
43 for (len of foo) {
44 tmp = len[0];
45 if (tmp && typeof tmp === 'object') {
46 tmp = find(bar, tmp);
47 if (!tmp) return false;
48 }
49 if (!dequal(len[1], bar.get(tmp))) {
50 return false;
51 }
52 }
53 return true;
54 }
55
56 if (ctor === ArrayBuffer) {
57 foo = new Uint8Array(foo);
58 bar = new Uint8Array(bar);
59 } else if (ctor === DataView) {
60 if ((len=foo.byteLength) === bar.byteLength) {
61 while (len-- && foo.getInt8(len) === bar.getInt8(len));
62 }
63 return len === -1;
64 }
65
66 if (ArrayBuffer.isView(foo)) {
67 if ((len=foo.byteLength) === bar.byteLength) {
68 while (len-- && foo[len] === bar[len]);
69 }
70 return len === -1;
71 }
72
73 if (!ctor || typeof foo === 'object') {
74 len = 0;
75 for (ctor in foo) {
76 if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
77 if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
78 }
79 return Object.keys(bar).length === len;
80 }
81 }
82
83 return foo !== foo && bar !== bar;
84 }
85
86 exports.dequal = dequal;