@longlast/equals 0.5.4 → 0.5.6
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.js +70 -16
- package/package.json +4 -1
package/dist/index.js
CHANGED
|
@@ -67,40 +67,65 @@ export const equals = curry(_equals);
|
|
|
67
67
|
function _equals(a, b) {
|
|
68
68
|
// This is an optimized implementation. There is a simpler, equivalent one
|
|
69
69
|
// in pkg/equals/alt/reference.ts.
|
|
70
|
+
if (a == null) {
|
|
71
|
+
return a === b;
|
|
72
|
+
}
|
|
70
73
|
// TODO: (pre-1.0.0) decide if we should pass `equals` as the second
|
|
71
74
|
// argument to [$equals]. This would open the "protocol" up a bit more,
|
|
72
75
|
// since people could then define their own implementations of `equals`
|
|
73
76
|
// that work consistently through custom equality comparisons.
|
|
74
|
-
|
|
75
|
-
if (a != null && typeof a[$equals] === "function") {
|
|
77
|
+
if (typeof a[$equals] === "function") {
|
|
76
78
|
return Boolean(a[$equals](b));
|
|
77
79
|
}
|
|
78
80
|
if (Object.is(a, b)) {
|
|
79
81
|
return true;
|
|
80
82
|
}
|
|
81
|
-
if (a
|
|
83
|
+
if (typeof a === "function" && typeof b === "function") {
|
|
84
|
+
const aUnapplied = a[$unapplied];
|
|
85
|
+
const bUnapplied = b[$unapplied];
|
|
86
|
+
return (aUnapplied != null &&
|
|
87
|
+
aUnapplied === bUnapplied &&
|
|
88
|
+
_equals(getBoundArguments(a), getBoundArguments(b)));
|
|
89
|
+
}
|
|
90
|
+
// If `a` is a primitive at this point, return false, since we already know
|
|
91
|
+
// it is not identical to `b`.
|
|
92
|
+
if (typeof a !== "object") {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const aConstructorString = functionString(constructorOf(a));
|
|
96
|
+
const bConstructorString = functionString(constructorOf(b));
|
|
97
|
+
if (aConstructorString !== bConstructorString) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (dateConstructorString === aConstructorString) {
|
|
101
|
+
unsafeNarrow(a);
|
|
102
|
+
unsafeNarrow(b);
|
|
82
103
|
return Object.is(+a, +b);
|
|
83
104
|
}
|
|
84
|
-
if (
|
|
105
|
+
if (regexConstructorString === aConstructorString) {
|
|
85
106
|
return String(a) === String(b);
|
|
86
107
|
}
|
|
87
|
-
if (a instanceof Error
|
|
88
|
-
|
|
108
|
+
if (a instanceof Error ||
|
|
109
|
+
nativeErrorConstructorStrings.includes(aConstructorString)) {
|
|
110
|
+
unsafeNarrow(a);
|
|
111
|
+
unsafeNarrow(b);
|
|
112
|
+
return a.message === b.message;
|
|
89
113
|
}
|
|
90
|
-
if (Array.isArray(a)
|
|
114
|
+
if (Array.isArray(a)) {
|
|
115
|
+
unsafeNarrow(b);
|
|
91
116
|
return a.length === b.length && a.every((_, i) => _equals(a[i], b[i]));
|
|
92
117
|
}
|
|
93
|
-
if (
|
|
118
|
+
if (setConstructorString === aConstructorString) {
|
|
119
|
+
unsafeNarrow(a);
|
|
120
|
+
unsafeNarrow(b);
|
|
94
121
|
return a.size === b.size && [...a].every((v) => b.has(v));
|
|
95
122
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
if (a && b && typeof a === "object" && protoOf(a) === protoOf(b)) {
|
|
123
|
+
// TODO: typed arrays
|
|
124
|
+
// TODO: Map
|
|
125
|
+
if (objectConstructorString === aConstructorString ||
|
|
126
|
+
protoOf(a) === protoOf(b)) {
|
|
127
|
+
unsafeNarrow(a);
|
|
128
|
+
unsafeNarrow(b);
|
|
104
129
|
const aKeys = Object.keys(a);
|
|
105
130
|
const bKeys = Object.keys(b);
|
|
106
131
|
if (aKeys.length !== bKeys.length) {
|
|
@@ -123,4 +148,33 @@ function getBoundArguments(f) {
|
|
|
123
148
|
// TODO: (pre-1.0.0) remove `f[$boundArguments]` fallback.
|
|
124
149
|
return f[$getBoundArguments]?.() ?? f[$boundArguments];
|
|
125
150
|
}
|
|
151
|
+
function functionString(f) {
|
|
152
|
+
if (typeof f !== "function") {
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
return Function.prototype.toString.call(f);
|
|
156
|
+
}
|
|
157
|
+
function constructorOf(value) {
|
|
158
|
+
if (value == null) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
return protoOf(value)?.constructor;
|
|
162
|
+
}
|
|
163
|
+
function unsafeNarrow(value) {
|
|
164
|
+
value;
|
|
165
|
+
}
|
|
126
166
|
const protoOf = Object.getPrototypeOf;
|
|
167
|
+
const objectConstructorString = functionString(Object);
|
|
168
|
+
const dateConstructorString = functionString(Date);
|
|
169
|
+
const regexConstructorString = functionString(RegExp);
|
|
170
|
+
const setConstructorString = functionString(Set);
|
|
171
|
+
const nativeErrorConstructorStrings = [
|
|
172
|
+
functionString(Error),
|
|
173
|
+
// TODO: add DOMException? Be sure to check the `name` property.
|
|
174
|
+
functionString(EvalError),
|
|
175
|
+
functionString(RangeError),
|
|
176
|
+
functionString(ReferenceError),
|
|
177
|
+
functionString(SyntaxError),
|
|
178
|
+
functionString(TypeError),
|
|
179
|
+
functionString(URIError),
|
|
180
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@longlast/equals",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6",
|
|
4
4
|
"description": "Deeply compares objects",
|
|
5
5
|
"homepage": "https://longlast.js.org/",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,5 +19,8 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@longlast/curry": "^0.5.0",
|
|
21
21
|
"@longlast/symbols": "^1.0.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"fast-deep-equal": "3.1.3"
|
|
22
25
|
}
|
|
23
26
|
}
|