@longlast/equals 0.1.2 → 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 CHANGED
@@ -16,9 +16,11 @@
16
16
  * class) and the same set of enumerable string-keyed properties, and the
17
17
  * values of their corresponding properties are equal (according to
18
18
  * `equals`).
19
- * - Primitives `a` and `b` are equal iff `a === b`.
19
+ * - Primitives `a` and `b` are equal iff `Object.is(a, b)`. This is similar
20
+ * to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
21
+ * different from `-0`.
20
22
  *
21
- * `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
22
24
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
23
25
  * that they do not contain cycles.
24
26
  */
package/dist/index.js CHANGED
@@ -16,24 +16,26 @@
16
16
  * class) and the same set of enumerable string-keyed properties, and the
17
17
  * values of their corresponding properties are equal (according to
18
18
  * `equals`).
19
- * - Primitives `a` and `b` are equal iff `a === b`.
19
+ * - Primitives `a` and `b` are equal iff `Object.is(a, b)`. This is similar
20
+ * to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
21
+ * different from `-0`.
20
22
  *
21
- * `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
22
24
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
23
25
  * that they do not contain cycles.
24
26
  */
25
27
  export function equals(a, b) {
26
- if (a === b) {
28
+ if (Object.is(a, b)) {
27
29
  return true;
28
30
  }
29
31
  if (Array.isArray(a) && Array.isArray(b)) {
30
32
  return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
31
33
  }
32
34
  if (a instanceof Date && b instanceof Date) {
33
- return a.toISOString() === b.toISOString();
35
+ return Object.is(+a, +b);
34
36
  }
35
37
  if (a instanceof Set && b instanceof Set) {
36
- return a.size === b.size && [...a.values()].every((v) => b.has(v));
38
+ return equalSets(a, b);
37
39
  }
38
40
  if (a instanceof Error && b instanceof Error) {
39
41
  return (a.message === b.message &&
@@ -42,11 +44,14 @@ export function equals(a, b) {
42
44
  if (isObject(a) && isObject(b)) {
43
45
  const aKeys = Object.keys(a);
44
46
  const bKeys = Object.keys(b);
45
- return (aKeys.length === bKeys.length &&
47
+ return (equalSets(new Set(aKeys), new Set(bKeys)) &&
46
48
  aKeys.every((k) => equals(a[k], b[k])) &&
47
49
  Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
48
50
  }
49
- return a === b;
51
+ return false;
52
+ }
53
+ function equalSets(a, b) {
54
+ return a.size === b.size && [...a.values()].every((v) => b.has(v));
50
55
  }
51
56
  function isObject(x) {
52
57
  return !!x && typeof x === "object";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longlast/equals",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"