-
Notifications
You must be signed in to change notification settings - Fork 0
/
equals.ts
35 lines (29 loc) · 963 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
34
35
/**
* Performs a deep comparison between two values to determine if they are equivalent.
* @param a - The first value to compare.
* @param b - The second value to compare.
* @returns `true` if the values are equivalent, otherwise `false`.
* @example
* ```typescript
* equals(
* { a: [2, { e: 3 }], b: [4], c: 'foo' },
* { a: [2, { e: 3 }], b: [4], c: 'foo' }
* ); // true
* equals([1, 2, 3], { 0: 1, 1: 2, 2: 3 }); // true
* ```
* @category Object
* @since 0.1.0
* @public
*/
const 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.prototype !== b.prototype) return false;
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
return keys.every((k) => equals(a[k], b[k]));
};
export default equals;