Update deep-equal.ts
This commit is contained in:
parent
aed95a765d
commit
4ab9f66356
|
@ -3,24 +3,35 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function deepEqual(a: any, b: any): boolean {
|
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 (a === b) return true;
|
||||||
|
if (typeof a !== typeof b) return false;
|
||||||
|
|
||||||
if (a === null) return b === null;
|
if (a === null) return b === null;
|
||||||
|
|
||||||
|
if (a === undefined) return b === undefined;
|
||||||
|
|
||||||
if (Array.isArray(a) && Array.isArray(b)) {
|
if (Array.isArray(a) && Array.isArray(b)) {
|
||||||
if (a.length !== b.length) return false;
|
if (a.length !== b.length) return false;
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
if (!deepEqual(a[i], b[i])) return false;
|
if (!deepEqual(a[i], b[i])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
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')) {
|
} else if (((typeof a) === 'object') && ((typeof b) === 'object')) {
|
||||||
const aks = Object.keys(a);
|
const aks = Object.keys(a);
|
||||||
const bks = Object.keys(b);
|
const bks = Object.keys(b as { [key: string]: JsonLike });
|
||||||
if (aks.length !== bks.length) return false;
|
if (aks.length !== bks.length) return false;
|
||||||
for (let i = 0; i < aks.length; i++) {
|
for (let i = 0; i < aks.length; i++) {
|
||||||
const k = aks[i];
|
const k = aks[i];
|
||||||
if (!deepEqual(a[k], b[k])) return false;
|
if (!deepEqual(a[k], (b as { [key: string]: JsonLike })[k])) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue