forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep-equal.ts
More file actions
51 lines (42 loc) · 909 Bytes
/
Copy pathdeep-equal.ts
File metadata and controls
51 lines (42 loc) · 909 Bytes
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
export function deepEqual(a: unknown, b: unknown): boolean {
if (a === b) {
return true
}
if (
typeof a !== 'object' ||
a === null ||
typeof b !== 'object' ||
b === null
) {
return false
}
if (Array.isArray(a) !== Array.isArray(b)) {
return false
}
if (Array.isArray(a)) {
if (a.length !== (b as any[]).length) {
return false
}
// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], (b as any[])[i])) {
return false
}
}
return true
}
const keysA = Object.keys(a)
const keysB = Object.keys(b)
if (keysA.length !== keysB.length) {
return false
}
for (const key of keysA) {
if (
!Object.prototype.hasOwnProperty.call(b, key) ||
!deepEqual((a as any)[key], (b as any)[key])
) {
return false
}
}
return true
}