-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathareEqual.js
More file actions
73 lines (73 loc) · 1.44 KB
/
areEqual.js
File metadata and controls
73 lines (73 loc) · 1.44 KB
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
export default function areEqual(a, b) {
if (a === b) {
return true;
}
else if (typeof a !== typeof b) {
return false;
}
else if (a == null || b == null) {
return a === b;
}
else if (typeof a === 'boolean') {
return a === b;
}
else if (typeof a === 'number') {
return a === b;
}
else if (typeof a === 'string') {
return a === b;
}
else if (typeof a === 'bigint') {
return a === b;
}
else if (typeof a === 'function') {
return a === b;
}
else if (typeof a !== 'object') {
return a === b;
}
else if (a.constructor !== b.constructor) {
return false;
}
else if (a instanceof Boolean && b instanceof Boolean) {
return a.valueOf() === b.valueOf();
}
else if (a instanceof Number && b instanceof Number) {
return a.valueOf() === b.valueOf();
}
else if (a instanceof String && b instanceof String) {
return a.valueOf() === b.valueOf();
}
else if (a instanceof BigInt && b instanceof BigInt) {
return a.valueOf() === b.valueOf();
}
else if (a instanceof Array && b instanceof Array) {
if (a.length !== b.length) {
return false;
}
for (const i in a) {
if (!areEqual(a[i], b[i])) {
return false;
}
}
return true;
}
else {
const checkedKeys = {};
for (const k in a) {
if (!areEqual(a[k], b[k])) {
return false;
}
checkedKeys[k] = true;
}
for (const k in b) {
if (checkedKeys[k]) {
continue;
}
if (!areEqual(a[k], b[k])) {
return false;
}
}
return true;
}
}