-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathequals.ts
33 lines (25 loc) · 896 Bytes
/
equals.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
// It may compare any values so we allow `any` here.
/* eslint-disable @typescript-eslint/no-explicit-any */
interface IPrototype { prototype: any }
export default function equals(a: any, b: any): boolean {
if (a === b) {
return true;
}
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) {
return a === b;
}
if ((a as IPrototype).prototype !== (b as IPrototype).prototype) {
return false;
}
const keys = Object.keys(a as object);
if (keys.length !== Object.keys(b as object).length) {
return false;
}
// TS thinks that `a` and `b` may be any here
// however we know that they are objects, because we've checked them above
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return keys.every(k => equals(a[k], b[k]));
}