Spaces:
Sleeping
Sleeping
File size: 1,585 Bytes
05c5ed5 | 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 52 53 54 55 56 57 58 59 60 | export default function equal(a: any, b: any): boolean {
if (a === b) return true;
if (a && b && typeof a === "object" && typeof b === "object") {
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
// Handle Array
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (!equal(a[i], b[i])) return false;
}
return true;
}
// Handle Date
if (a instanceof Date)
return b instanceof Date && a.getTime() === b.getTime();
// Handle RegExp
if (a instanceof RegExp)
return (
b instanceof RegExp && a.source === b.source && a.flags === b.flags
);
// Handle Map
if (a instanceof Map) {
if (!(b instanceof Map) || a.size !== b.size) return false;
for (const [key, val] of a) {
if (!b.has(key) || !equal(val, b.get(key))) return false;
}
return true;
}
// Handle Set
if (a instanceof Set) {
if (!(b instanceof Set) || a.size !== b.size) return false;
for (const val of a) {
if (!b.has(val)) return false;
}
return true;
}
// Handle general object
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)) return false;
if (!equal(a[key], b[key])) return false;
}
return true;
}
// Handle NaN
return Number.isNaN(a) && Number.isNaN(b);
}
|