-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deep_equal.ts
37 lines (28 loc) 路 795 Bytes
/
deep_equal.ts
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
// deno-lint-ignore-file no-explicit-any
export function deepEqual(first: any, second: any) {
const [firstType, secondType] = [typeof first, typeof second];
const [firstObject, secondObject] = [
firstType === "object",
secondType === "object",
];
const [firstIsObject, secondIsObject] = [
firstObject && first !== null,
secondObject && second !== null,
];
if (!firstIsObject || !secondIsObject) {
return first === second;
}
const [firstKeys, secondKeys] = [first, second].map(Object.keys);
if (firstKeys.length !== secondKeys.length) {
return false;
}
for (const key of firstKeys) {
if (!secondKeys.includes(key)) {
return false;
}
if (!deepEqual(first[key], second[key])) {
return false;
}
}
return true;
}