@longlast/equals 0.3.0 → 0.4.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
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
+ import { type Curried2 } from "@longlast/curry";
4
5
  /**
6
+ * @function
5
7
  * Deeply compares two values, returning true if they're equal and false
6
8
  * otherwise. The following criteria are used to determine equality:
7
9
  *
@@ -24,5 +26,7 @@
24
26
  * `equals()` can throw a `RangeError` if one of its arguments contains a
25
27
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
26
28
  * that they do not contain cycles.
29
+ *
30
+ * `equals()` is curried.
27
31
  */
28
- export declare function equals(a: unknown, b: unknown): boolean;
32
+ export declare const equals: Curried2<unknown, unknown, boolean>;
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
+ import { curry } from "@longlast/curry";
4
5
  /**
6
+ * @function
5
7
  * Deeply compares two values, returning true if they're equal and false
6
8
  * otherwise. The following criteria are used to determine equality:
7
9
  *
@@ -24,8 +26,13 @@
24
26
  * `equals()` can throw a `RangeError` if one of its arguments contains a
25
27
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
26
28
  * that they do not contain cycles.
29
+ *
30
+ * `equals()` is curried.
27
31
  */
28
- export function equals(a, b) {
32
+ export const equals = curry(_equals);
33
+ function _equals(a, b) {
34
+ // This is an optimized implementation. There is a simpler, equivalent one
35
+ // in pkg/equals/alt/reference.ts.
29
36
  if (Object.is(a, b)) {
30
37
  return true;
31
38
  }
@@ -40,23 +47,23 @@ export function equals(a, b) {
40
47
  Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
41
48
  }
42
49
  if (a instanceof Set && b instanceof Set) {
43
- return equalSets(a, b);
50
+ return a.size === b.size && [...a].every((v) => b.has(v));
44
51
  }
45
52
  if (Array.isArray(a) && Array.isArray(b)) {
46
53
  return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
47
54
  }
48
- if (isObject(a) && isObject(b)) {
49
- const aKeys = Object.keys(a);
50
- const bKeys = Object.keys(b);
51
- return (equalSets(new Set(aKeys), new Set(bKeys)) &&
52
- aKeys.every((k) => equals(a[k], b[k])) &&
53
- Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
55
+ if (a && b && typeof a === "object" && protoOf(a) === protoOf(b)) {
56
+ const bKeys = new Set(Object.keys(b));
57
+ for (const key of Object.keys(a)) {
58
+ if (!bKeys.has(key)) {
59
+ return false;
60
+ }
61
+ if (!equals(a[key], b[key])) {
62
+ return false;
63
+ }
64
+ }
65
+ return true;
54
66
  }
55
67
  return false;
56
68
  }
57
- function equalSets(a, b) {
58
- return a.size === b.size && [...a].every((v) => b.has(v));
59
- }
60
- function isObject(x) {
61
- return !!x && typeof x === "object";
62
- }
69
+ const protoOf = Object.getPrototypeOf;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longlast/equals",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -9,5 +9,8 @@
9
9
  "types": "dist/index.d.ts",
10
10
  "imports": {
11
11
  "#@longlast/equals": "./src/index.ts"
12
+ },
13
+ "dependencies": {
14
+ "@longlast/curry": "^0.2.1"
12
15
  }
13
16
  }