/
equals.ts
123 lines (105 loc) · 3.01 KB
/
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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eslint-disable jsdoc/check-param-names -- ignore for deprecated files */
import { purry } from "./purry";
/**
* Returns true if its arguments are equivalent, false otherwise.
* NOTE: Doesn't handle cyclical data structures.
*
* ! **DEPRECATED**: Use `R.isDeepEqual(a, b)`. Will be removed in V2.
*
* @param a - The first object to compare.
* @param b - The second object to compare.
* @signature
* R.equals(a, b)
* @example
* R.equals(1, 1) //=> true
* R.equals(1, '1') //=> false
* R.equals([1, 2, 3], [1, 2, 3]) //=> true
* @dataFirst
* @category Deprecated
* @deprecated Use `R.isDeepEqual(a, b)`. Will be removed in V2.
*/
export function equals(a: unknown, b: unknown): boolean;
/**
* Returns true if its arguments are equivalent, false otherwise.
* NOTE: Doesn't handle cyclical data structures.
*
* ! **DEPRECATED**: Use `R.isDeepEqual(b)`. Will be removed in V2.
*
* @param a - The first object to compare.
* @param b - The second object to compare.
* @signature
* R.equals(b)(a)
* @example
* R.equals(1)(1) //=> true
* R.equals('1')(1) //=> false
* R.equals([1, 2, 3])([1, 2, 3]) //=> true
* @dataLast
* @category Deprecated
* @deprecated Use `R.isDeepEqual(b)`. Will be removed in V2.
*/
export function equals(a: unknown): (b: unknown) => boolean;
export function equals(): unknown {
return purry(_equals, arguments);
}
function _equals(a: unknown, b: unknown): boolean {
if (a === b) {
return true;
}
if (typeof a === "number" && typeof b === "number") {
// TODO: This is a temporary fix for NaN, we should use Number.isNaN once we bump our target above ES5.
// eslint-disable-next-line no-self-compare -- We should use Number.isNaN here, but it's ES2015.
return a !== a && b !== b;
}
if (typeof a !== "object" || typeof b !== "object") {
return false;
}
if (a === null || b === null) {
return false;
}
const isArrayA = Array.isArray(a);
const isArrayB = Array.isArray(b);
if (isArrayA && isArrayB) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!_equals(a[i], b[i])) {
return false;
}
}
return true;
}
if (isArrayA !== isArrayB) {
return false;
}
const isDateA = a instanceof Date;
const isDateB = b instanceof Date;
if (isDateA && isDateB) {
return a.getTime() === b.getTime();
}
if (isDateA !== isDateB) {
return false;
}
const isRegExpA = a instanceof RegExp;
const isRegExpB = b instanceof RegExp;
if (isRegExpA && isRegExpB) {
return a.toString() === b.toString();
}
if (isRegExpA !== isRegExpB) {
return false;
}
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) {
return false;
}
for (const key of keys) {
if (!Object.prototype.hasOwnProperty.call(b, key)) {
return false;
}
// @ts-expect-error [ts7053] - There's no easy way to tell typescript these keys are safe.
if (!_equals(a[key], b[key])) {
return false;
}
}
return true;
}