@longlast/equals 0.4.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,7 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
- import { type Curried2 } from "./curry.ts";
4
+ import { type Curried2 } from "@longlast/curry";
5
5
  /**
6
6
  * @function
7
7
  * Deeply compares two values, returning true if they're equal and false
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
- import { curry } from "./curry.js";
4
+ import { curry } from "@longlast/curry";
5
5
  /**
6
6
  * @function
7
7
  * Deeply compares two values, returning true if they're equal and false
@@ -31,6 +31,8 @@ import { curry } from "./curry.js";
31
31
  */
32
32
  export const equals = curry(_equals);
33
33
  function _equals(a, b) {
34
+ // This is an optimized implementation. There is a simpler, equivalent one
35
+ // in pkg/equals/alt/reference.ts.
34
36
  if (Object.is(a, b)) {
35
37
  return true;
36
38
  }
@@ -45,23 +47,23 @@ function _equals(a, b) {
45
47
  Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
46
48
  }
47
49
  if (a instanceof Set && b instanceof Set) {
48
- return equalSets(a, b);
50
+ return a.size === b.size && [...a].every((v) => b.has(v));
49
51
  }
50
52
  if (Array.isArray(a) && Array.isArray(b)) {
51
53
  return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
52
54
  }
53
- if (isObject(a) && isObject(b)) {
54
- const aKeys = Object.keys(a);
55
- const bKeys = Object.keys(b);
56
- return (equalSets(new Set(aKeys), new Set(bKeys)) &&
57
- aKeys.every((k) => equals(a[k], b[k])) &&
58
- 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;
59
66
  }
60
67
  return false;
61
68
  }
62
- function equalSets(a, b) {
63
- return a.size === b.size && [...a].every((v) => b.has(v));
64
- }
65
- function isObject(x) {
66
- return !!x && typeof x === "object";
67
- }
69
+ const protoOf = Object.getPrototypeOf;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longlast/equals",
3
- "version": "0.4.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
  }
package/dist/curry.d.ts DELETED
@@ -1,7 +0,0 @@
1
- type Func2<A, B, Out> = (a: A, b: B) => Out;
2
- export interface Curried2<A, B, Out> {
3
- (a: A, b: B): Out;
4
- (a: A): (b: B) => Out;
5
- }
6
- export declare function curry<A, B, Out>(f: Func2<A, B, Out>): Curried2<A, B, Out>;
7
- export {};
package/dist/curry.js DELETED
@@ -1,12 +0,0 @@
1
- export function curry(f) {
2
- return function (a, b) {
3
- if (arguments.length === f.length) {
4
- return f(a, b);
5
- }
6
- else {
7
- return function (b) {
8
- return f(a, b);
9
- };
10
- }
11
- };
12
- }