1
0
Fork 0
mirror of https://github.com/paricafe/misskey.git synced 2025-04-01 09:09:29 -05:00

Update deep-equal.ts

This commit is contained in:
syuilo 2025-03-19 20:32:15 +09:00
parent aed95a765d
commit 4ab9f66356

View file

@ -3,24 +3,35 @@
* 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 (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);
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[k])) return false;
if (!deepEqual(a[k], (b as { [key: string]: JsonLike })[k])) return false;
}
return true;
}