@longlast/equals 0.1.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 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { test, expect, is } from "@benchristel/taste";
2
+ import { equals } from "../index.js";
3
+ test("equals", {
4
+ "compares booleans"() {
5
+ expect(equals(true, false), is, false);
6
+ expect(equals(false, false), is, true);
7
+ },
8
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { test, expect } from "tstyche";
2
+ import { equals } from "../index.js";
3
+ test("equals", () => {
4
+ expect(equals(true, true)).type.toBeAssignableTo();
5
+ });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @module equals
3
+ */
4
+ /**
5
+ * Deeply compares two values, returning true if they're equal and false
6
+ * otherwise. The following criteria are used to determine equality:
7
+ *
8
+ * - All values are always equal to themselves.
9
+ * - Arrays are equal iff they have the same length and their corresponding
10
+ * elements are equal (according to `equals`).
11
+ * - Dates are equal iff they serialize to the same ISO string.
12
+ * - Sets are equal iff they contain the same elements. Note that set
13
+ * elements are _not_ deeply compared.
14
+ * - Errors are equal iff they have the same class and message.
15
+ * - Other objects are equal iff they have the same prototype (e.g. the same
16
+ * class) and the same set of enumerable string-keyed properties, and the
17
+ * values of their corresponding properties are equal (according to
18
+ * `equals`).
19
+ * - Primitives `a` and `b` are equal iff `a === b`.
20
+ *
21
+ * `equals()` can throw a RangeError if one of its arguments contains a
22
+ * reference cycle. Avoid passing mutable objects to `equals()` unless you know
23
+ * that they do not contain cycles.
24
+ */
25
+ export declare function equals(a: unknown, b: unknown): boolean;
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @module equals
3
+ */
4
+ /**
5
+ * Deeply compares two values, returning true if they're equal and false
6
+ * otherwise. The following criteria are used to determine equality:
7
+ *
8
+ * - All values are always equal to themselves.
9
+ * - Arrays are equal iff they have the same length and their corresponding
10
+ * elements are equal (according to `equals`).
11
+ * - Dates are equal iff they serialize to the same ISO string.
12
+ * - Sets are equal iff they contain the same elements. Note that set
13
+ * elements are _not_ deeply compared.
14
+ * - Errors are equal iff they have the same class and message.
15
+ * - Other objects are equal iff they have the same prototype (e.g. the same
16
+ * class) and the same set of enumerable string-keyed properties, and the
17
+ * values of their corresponding properties are equal (according to
18
+ * `equals`).
19
+ * - Primitives `a` and `b` are equal iff `a === b`.
20
+ *
21
+ * `equals()` can throw a RangeError if one of its arguments contains a
22
+ * reference cycle. Avoid passing mutable objects to `equals()` unless you know
23
+ * that they do not contain cycles.
24
+ */
25
+ export function equals(a, b) {
26
+ if (a === b) {
27
+ return true;
28
+ }
29
+ if (Array.isArray(a) && Array.isArray(b)) {
30
+ return a.length === b.length && a.every((_, i) => equals(a[i], b[i]));
31
+ }
32
+ if (a instanceof Date && b instanceof Date) {
33
+ return a.toISOString() === b.toISOString();
34
+ }
35
+ if (a instanceof Set && b instanceof Set) {
36
+ return a.size === b.size && [...a.values()].every((v) => b.has(v));
37
+ }
38
+ if (a instanceof Error && b instanceof Error) {
39
+ return (a.message === b.message &&
40
+ Object.getPrototypeOf(a)?.constructor ===
41
+ Object.getPrototypeOf(b)?.constructor);
42
+ }
43
+ if (isObject(a) && isObject(b)) {
44
+ const aKeys = Object.keys(a);
45
+ const bKeys = Object.keys(b);
46
+ return (aKeys.length === bKeys.length &&
47
+ aKeys.every((k) => equals(a[k], b[k])) &&
48
+ Object.getPrototypeOf(a)?.constructor ===
49
+ Object.getPrototypeOf(b)?.constructor);
50
+ }
51
+ return a === b;
52
+ }
53
+ function isObject(x) {
54
+ return !!x && typeof x === "object";
55
+ }
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@longlast/equals",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "imports": {
11
+ "#@longlast/equals": "./src/index.ts"
12
+ }
13
+ }