@longlast/equals 0.2.1 → 0.4.0

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.
@@ -0,0 +1,7 @@
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 ADDED
@@ -0,0 +1,12 @@
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
+ }
package/dist/index.d.ts CHANGED
@@ -1,27 +1,32 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
+ import { type Curried2 } from "./curry.ts";
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
  *
8
10
  * - All values are always equal to themselves.
11
+ * - Primitives `a` and `b` are equal iff `Object.is(a, b)`. This is similar
12
+ * to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
13
+ * different from `-0`.
14
+ * - Dates are equal iff they have the same millisecond-precision timestamp.
15
+ * - RegExps are equal iff they have the same pattern and flags.
16
+ * - Errors are equal iff they have the same class and message.
9
17
  * - Arrays are equal iff they have the same length and their corresponding
10
18
  * elements are equal (according to `equals`).
11
- * - Dates are equal iff they serialize to the same ISO string.
12
19
  * - Sets are equal iff they contain the same elements. Note that set
13
20
  * elements are _not_ deeply compared.
14
- * - Errors are equal iff they have the same class and message.
15
21
  * - Other objects are equal iff they have the same prototype (e.g. the same
16
22
  * class) and the same set of enumerable string-keyed properties, and the
17
23
  * values of their corresponding properties are equal (according to
18
24
  * `equals`).
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`.
22
25
  *
23
26
  * `equals()` can throw a `RangeError` if one of its arguments contains a
24
27
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
25
28
  * that they do not contain cycles.
29
+ *
30
+ * `equals()` is curried.
26
31
  */
27
- export declare function equals(a: unknown, b: unknown): boolean;
32
+ export declare const equals: Curried2<unknown, unknown, boolean>;
package/dist/index.js CHANGED
@@ -1,46 +1,55 @@
1
1
  /**
2
2
  * @module equals
3
3
  */
4
+ import { curry } from "./curry.js";
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
  *
8
10
  * - All values are always equal to themselves.
11
+ * - Primitives `a` and `b` are equal iff `Object.is(a, b)`. This is similar
12
+ * to `===` comparison, but treats `NaN` as equal to `NaN` and `0` as
13
+ * different from `-0`.
14
+ * - Dates are equal iff they have the same millisecond-precision timestamp.
15
+ * - RegExps are equal iff they have the same pattern and flags.
16
+ * - Errors are equal iff they have the same class and message.
9
17
  * - Arrays are equal iff they have the same length and their corresponding
10
18
  * elements are equal (according to `equals`).
11
- * - Dates are equal iff they serialize to the same ISO string.
12
19
  * - Sets are equal iff they contain the same elements. Note that set
13
20
  * elements are _not_ deeply compared.
14
- * - Errors are equal iff they have the same class and message.
15
21
  * - Other objects are equal iff they have the same prototype (e.g. the same
16
22
  * class) and the same set of enumerable string-keyed properties, and the
17
23
  * values of their corresponding properties are equal (according to
18
24
  * `equals`).
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`.
22
25
  *
23
26
  * `equals()` can throw a `RangeError` if one of its arguments contains a
24
27
  * reference cycle. Avoid passing mutable objects to `equals()` unless you know
25
28
  * that they do not contain cycles.
29
+ *
30
+ * `equals()` is curried.
26
31
  */
27
- export function equals(a, b) {
32
+ export const equals = curry(_equals);
33
+ function _equals(a, b) {
28
34
  if (Object.is(a, b)) {
29
35
  return true;
30
36
  }
31
- if (Array.isArray(a) && Array.isArray(b)) {
32
- return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
33
- }
34
37
  if (a instanceof Date && b instanceof Date) {
35
38
  return Object.is(+a, +b);
36
39
  }
37
- if (a instanceof Set && b instanceof Set) {
38
- return equalSets(a, b);
40
+ if (a instanceof RegExp && b instanceof RegExp) {
41
+ return String(a) === String(b);
39
42
  }
40
43
  if (a instanceof Error && b instanceof Error) {
41
44
  return (a.message === b.message &&
42
45
  Object.getPrototypeOf(a) === Object.getPrototypeOf(b));
43
46
  }
47
+ if (a instanceof Set && b instanceof Set) {
48
+ return equalSets(a, b);
49
+ }
50
+ if (Array.isArray(a) && Array.isArray(b)) {
51
+ return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
52
+ }
44
53
  if (isObject(a) && isObject(b)) {
45
54
  const aKeys = Object.keys(a);
46
55
  const bKeys = Object.keys(b);
@@ -51,7 +60,7 @@ export function equals(a, b) {
51
60
  return false;
52
61
  }
53
62
  function equalSets(a, b) {
54
- return a.size === b.size && [...a.values()].every((v) => b.has(v));
63
+ return a.size === b.size && [...a].every((v) => b.has(v));
55
64
  }
56
65
  function isObject(x) {
57
66
  return !!x && typeof x === "object";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longlast/equals",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"