@longlast/equals 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
|
|
21
21
|
* different from `-0`.
|
|
22
22
|
*
|
|
23
|
-
* `equals()` can throw a RangeError if one of its arguments contains a
|
|
23
|
+
* `equals()` can throw a `RangeError` if one of its arguments contains a
|
|
24
24
|
* reference cycle. Avoid passing mutable objects to `equals()` unless you know
|
|
25
25
|
* that they do not contain cycles.
|
|
26
26
|
*/
|
package/dist/index.js
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
|
|
21
21
|
* different from `-0`.
|
|
22
22
|
*
|
|
23
|
-
* `equals()` can throw a RangeError if one of its arguments contains a
|
|
23
|
+
* `equals()` can throw a `RangeError` if one of its arguments contains a
|
|
24
24
|
* reference cycle. Avoid passing mutable objects to `equals()` unless you know
|
|
25
25
|
* that they do not contain cycles.
|
|
26
26
|
*/
|
|
@@ -32,10 +32,10 @@ export function equals(a, b) {
|
|
|
32
32
|
return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
|
|
33
33
|
}
|
|
34
34
|
if (a instanceof Date && b instanceof Date) {
|
|
35
|
-
return
|
|
35
|
+
return Object.is(+a, +b);
|
|
36
36
|
}
|
|
37
37
|
if (a instanceof Set && b instanceof Set) {
|
|
38
|
-
return a
|
|
38
|
+
return equalSets(a, b);
|
|
39
39
|
}
|
|
40
40
|
if (a instanceof Error && b instanceof Error) {
|
|
41
41
|
return (a.message === b.message &&
|
|
@@ -44,12 +44,15 @@ export function equals(a, b) {
|
|
|
44
44
|
if (isObject(a) && isObject(b)) {
|
|
45
45
|
const aKeys = Object.keys(a);
|
|
46
46
|
const bKeys = Object.keys(b);
|
|
47
|
-
return (aKeys
|
|
47
|
+
return (equalSets(new Set(aKeys), new Set(bKeys)) &&
|
|
48
48
|
aKeys.every((k) => equals(a[k], b[k])) &&
|
|
49
49
|
Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
|
|
50
50
|
}
|
|
51
51
|
return false;
|
|
52
52
|
}
|
|
53
|
+
function equalSets(a, b) {
|
|
54
|
+
return a.size === b.size && [...a.values()].every((v) => b.has(v));
|
|
55
|
+
}
|
|
53
56
|
function isObject(x) {
|
|
54
57
|
return !!x && typeof x === "object";
|
|
55
58
|
}
|