blob: 2859641dc7475208582eeb652c1f8f979f6ca7ab (
plain)
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
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
type JsonLike = string | number | boolean | null | undefined | JsonLike[] | { [key: string]: JsonLike } | Map<string, JsonLike>;
export function deepEqual(a: JsonLike, b: JsonLike): boolean {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if (a === null) return b === null;
if (a === undefined) return b === undefined;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
} else if (a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
for (const [k, v] of a) {
if (!deepEqual(v, b.get(k))) return false;
}
return true;
} else if (((typeof a) === 'object') && ((typeof b) === 'object')) {
const aks = Object.keys(a);
const bks = Object.keys(b as { [key: string]: JsonLike });
if (aks.length !== bks.length) return false;
for (let i = 0; i < aks.length; i++) {
const k = aks[i];
if (!deepEqual(a[k], (b as { [key: string]: JsonLike })[k])) return false;
}
return true;
}
return false;
}
|