fast-equals 3.0.3 → 4.0.1-beta.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.
- package/.prettierrc +4 -0
- package/CHANGELOG.md +31 -0
- package/README.md +60 -44
- package/dist/fast-equals.cjs.js +353 -274
- package/dist/fast-equals.cjs.js.map +1 -1
- package/dist/fast-equals.esm.js +352 -274
- package/dist/fast-equals.esm.js.map +1 -1
- package/dist/fast-equals.js +353 -274
- package/dist/fast-equals.js.map +1 -1
- package/dist/fast-equals.min.js +1 -1
- package/dist/fast-equals.mjs +352 -274
- package/dist/fast-equals.mjs.map +1 -1
- package/index.d.ts +26 -25
- package/package.json +21 -19
- package/recipes/explicit-property-check.md +28 -0
- package/recipes/legacy-circular-equal-support.md +66 -0
- package/recipes/legacy-regexp-support.md +26 -0
- package/recipes/non-standard-properties.md +34 -0
- package/recipes/strict-property-descriptor-check.md +42 -0
- package/recipes/using-meta-in-comparison.md +23 -0
- package/src/arrays.ts +36 -0
- package/src/comparator.ts +146 -0
- package/src/dates.ts +12 -0
- package/src/index.ts +139 -0
- package/src/maps.ts +66 -0
- package/src/objects.ts +62 -0
- package/src/regexps.ts +11 -0
- package/src/sets.ts +61 -0
- package/src/utils.ts +128 -0
package/dist/fast-equals.esm.js
CHANGED
|
@@ -1,136 +1,190 @@
|
|
|
1
|
-
var HAS_WEAK_MAP_SUPPORT = typeof WeakMap === 'function';
|
|
2
|
-
var keys = Object.keys;
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @param a the value to compare against
|
|
7
|
-
* @param b the value to test
|
|
8
|
-
* @returns are the values equal by the SameValueZero principle
|
|
2
|
+
* Default equality comparator pass-through, used as the standard `isEqual` creator for
|
|
3
|
+
* use inside the built comparator.
|
|
9
4
|
*/
|
|
10
|
-
function
|
|
11
|
-
return
|
|
5
|
+
function createDefaultIsNestedEqual(comparator) {
|
|
6
|
+
return function isEqual(a, b, _indexOrKeyA, _indexOrKeyB, _parentA, _parentB, meta) {
|
|
7
|
+
return comparator(a, b, meta);
|
|
8
|
+
};
|
|
12
9
|
}
|
|
13
10
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @returns is the value a plain object
|
|
11
|
+
* Wrap the provided `areItemsEqual` method to manage the circular cache, allowing
|
|
12
|
+
* for circular references to be safely included in the comparison without creating
|
|
13
|
+
* stack overflows.
|
|
18
14
|
*/
|
|
19
|
-
function
|
|
20
|
-
return
|
|
15
|
+
function createIsCircular(areItemsEqual) {
|
|
16
|
+
return function isCircular(a, b, isEqual, cache) {
|
|
17
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
|
|
18
|
+
return areItemsEqual(a, b, isEqual, cache);
|
|
19
|
+
}
|
|
20
|
+
var cachedA = cache.get(a);
|
|
21
|
+
var cachedB = cache.get(b);
|
|
22
|
+
if (cachedA && cachedB) {
|
|
23
|
+
return cachedA === b && cachedB === a;
|
|
24
|
+
}
|
|
25
|
+
cache.set(a, b);
|
|
26
|
+
cache.set(b, a);
|
|
27
|
+
var result = areItemsEqual(a, b, isEqual, cache);
|
|
28
|
+
cache.delete(a);
|
|
29
|
+
cache.delete(b);
|
|
30
|
+
return result;
|
|
31
|
+
};
|
|
21
32
|
}
|
|
22
33
|
/**
|
|
23
|
-
*
|
|
34
|
+
* Targeted shallow merge of two objects.
|
|
24
35
|
*
|
|
25
|
-
* @
|
|
26
|
-
*
|
|
36
|
+
* @NOTE
|
|
37
|
+
* This exists as a tinier compiled version of the `__assign` helper that
|
|
38
|
+
* `tsc` injects in case of `Object.assign` not being present.
|
|
27
39
|
*/
|
|
28
|
-
function
|
|
29
|
-
|
|
40
|
+
function merge(a, b) {
|
|
41
|
+
var merged = {};
|
|
42
|
+
for (var key in a) {
|
|
43
|
+
merged[key] = a[key];
|
|
44
|
+
}
|
|
45
|
+
for (var key in b) {
|
|
46
|
+
merged[key] = b[key];
|
|
47
|
+
}
|
|
48
|
+
return merged;
|
|
30
49
|
}
|
|
31
50
|
/**
|
|
32
|
-
*
|
|
51
|
+
* Whether the value is a plain object.
|
|
33
52
|
*
|
|
34
|
-
* @
|
|
35
|
-
*
|
|
53
|
+
* @NOTE
|
|
54
|
+
* This is a same-realm compariosn only.
|
|
36
55
|
*/
|
|
37
|
-
function
|
|
38
|
-
return
|
|
56
|
+
function isPlainObject(value) {
|
|
57
|
+
return value.constructor === Object || value.constructor == null;
|
|
39
58
|
}
|
|
40
59
|
/**
|
|
41
|
-
*
|
|
42
|
-
* object that mimics the necessary API aspects for cache purposes
|
|
43
|
-
*
|
|
44
|
-
* @returns the new cache object
|
|
60
|
+
* When the value is `Promise`-like, aka "then-able".
|
|
45
61
|
*/
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
delete: function (key) {
|
|
50
|
-
for (var index = 0; index < entries.length; ++index) {
|
|
51
|
-
if (entries[index][0] === key) {
|
|
52
|
-
entries.splice(index, 1);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
get: function (key) {
|
|
58
|
-
for (var index = 0; index < entries.length; ++index) {
|
|
59
|
-
if (entries[index][0] === key) {
|
|
60
|
-
return entries[index][1];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
set: function (key, value) {
|
|
65
|
-
for (var index = 0; index < entries.length; ++index) {
|
|
66
|
-
if (entries[index][0] === key) {
|
|
67
|
-
entries[index][1] = value;
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
entries.push([key, value]);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
62
|
+
function isPromiseLike(value) {
|
|
63
|
+
return typeof value.then === 'function';
|
|
74
64
|
}
|
|
75
65
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @returns the new cache object
|
|
66
|
+
* Whether the values passed are strictly equal or both NaN.
|
|
79
67
|
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
68
|
+
function sameValueZeroEqual(a, b) {
|
|
69
|
+
return a === b || (a !== a && b !== b);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var ARGUMENTS_TAG = '[object Arguments]';
|
|
73
|
+
var BOOLEAN_TAG = '[object Boolean]';
|
|
74
|
+
var DATE_TAG = '[object Date]';
|
|
75
|
+
var REG_EXP_TAG = '[object RegExp]';
|
|
76
|
+
var MAP_TAG = '[object Map]';
|
|
77
|
+
var NUMBER_TAG = '[object Number]';
|
|
78
|
+
var OBJECT_TAG = '[object Object]';
|
|
79
|
+
var SET_TAG = '[object Set]';
|
|
80
|
+
var STRING_TAG = '[object String]';
|
|
81
|
+
var toString = Object.prototype.toString;
|
|
82
|
+
function createComparator(_a) {
|
|
83
|
+
var areArraysEqual = _a.areArraysEqual, areDatesEqual = _a.areDatesEqual, areMapsEqual = _a.areMapsEqual, areObjectsEqual = _a.areObjectsEqual, areRegExpsEqual = _a.areRegExpsEqual, areSetsEqual = _a.areSetsEqual, createIsNestedEqual = _a.createIsNestedEqual;
|
|
84
|
+
var isEqual = createIsNestedEqual(comparator);
|
|
85
|
+
/**
|
|
86
|
+
* compare the value of the two objects and return true if they are equivalent in values
|
|
87
|
+
*/
|
|
88
|
+
function comparator(a, b, meta) {
|
|
89
|
+
// If the items are strictly equal, no need to do a value comparison.
|
|
90
|
+
if (a === b) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
// If the items are not non-nullish objects, then the only possibility
|
|
94
|
+
// of them being equal but not strictly is if they are both `NaN`. Since
|
|
95
|
+
// `NaN` is uniquely not equal to itself, we can use self-comparison of
|
|
96
|
+
// both objects, which is faster than `isNaN()`.
|
|
97
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
|
|
98
|
+
return a !== a && b !== b;
|
|
99
|
+
}
|
|
100
|
+
// Checks are listed in order of commonality of use-case:
|
|
101
|
+
// 1. Common complex object types (plain object, array)
|
|
102
|
+
// 2. Common data values (date, regexp)
|
|
103
|
+
// 3. Less-common complex object types (map, set)
|
|
104
|
+
// 4. Less-common data values (promise, primitive wrappers)
|
|
105
|
+
// Inherently this is both subjective and assumptive, however
|
|
106
|
+
// when reviewing comparable libraries in the wild this order
|
|
107
|
+
// appears to be generally consistent.
|
|
108
|
+
// `isPlainObject` only checks against the object's own realm. Cross-realm
|
|
109
|
+
// comparisons are rare, and will be handled in the ultimate fallback, so
|
|
110
|
+
// we can avoid the `toString.call()` cost unless necessary.
|
|
111
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
112
|
+
return areObjectsEqual(a, b, isEqual, meta);
|
|
113
|
+
}
|
|
114
|
+
// `isArray()` works on subclasses and is cross-realm, so we can again avoid
|
|
115
|
+
// the `toString.call()` cost unless necessary by just checking if either
|
|
116
|
+
// and then both are arrays.
|
|
117
|
+
var aArray = Array.isArray(a);
|
|
118
|
+
var bArray = Array.isArray(b);
|
|
119
|
+
if (aArray || bArray) {
|
|
120
|
+
return aArray === bArray && areArraysEqual(a, b, isEqual, meta);
|
|
121
|
+
}
|
|
122
|
+
// Since this is a custom object, use the classic `toString.call()` to get its
|
|
123
|
+
// type. This is reasonably performant in modern environments like v8 and
|
|
124
|
+
// SpiderMonkey, and allows for cross-realm comparison when other checks like
|
|
125
|
+
// `instanceof` do not.
|
|
126
|
+
var aTag = toString.call(a);
|
|
127
|
+
if (aTag !== toString.call(b)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (aTag === DATE_TAG) {
|
|
131
|
+
// `getTime()` showed better results compared to alternatives like `valueOf()`
|
|
132
|
+
// or the unary `+` operator.
|
|
133
|
+
return areDatesEqual(a, b, isEqual, meta);
|
|
134
|
+
}
|
|
135
|
+
if (aTag === REG_EXP_TAG) {
|
|
136
|
+
return areRegExpsEqual(a, b, isEqual, meta);
|
|
137
|
+
}
|
|
138
|
+
if (aTag === MAP_TAG) {
|
|
139
|
+
return areMapsEqual(a, b, isEqual, meta);
|
|
140
|
+
}
|
|
141
|
+
if (aTag === SET_TAG) {
|
|
142
|
+
return areSetsEqual(a, b, isEqual, meta);
|
|
143
|
+
}
|
|
144
|
+
// If a simple object tag, then we can prioritize a simple object comparison because
|
|
145
|
+
// it is likely a custom class. If an arguments tag, it should be treated as a standard
|
|
146
|
+
// object.
|
|
147
|
+
if (aTag === OBJECT_TAG || aTag === ARGUMENTS_TAG) {
|
|
148
|
+
// The exception for value comparison is `Promise`-like contracts. These should be
|
|
149
|
+
// treated the same as standard `Promise` objects, which means strict equality.
|
|
150
|
+
return isPromiseLike(a) || isPromiseLike(b)
|
|
151
|
+
? a === b
|
|
152
|
+
: areObjectsEqual(a, b, isEqual, meta);
|
|
153
|
+
}
|
|
154
|
+
// As the penultimate fallback, check if the values passed are primitive wrappers. This
|
|
155
|
+
// is very rare in modern JS, which is why it is deprioritized compared to all other object
|
|
156
|
+
// types.
|
|
157
|
+
if (aTag === BOOLEAN_TAG || aTag === NUMBER_TAG || aTag === STRING_TAG) {
|
|
158
|
+
return sameValueZeroEqual(a.valueOf(), b.valueOf());
|
|
159
|
+
}
|
|
160
|
+
// If not matching any tags that require a specific type of comparison, then use strict
|
|
161
|
+
// equality. This is for a few reasons:
|
|
162
|
+
// - For types that cannot be introspected (`Promise`, `WeakMap`, etc.), this is the only
|
|
163
|
+
// comparison that can be made.
|
|
164
|
+
// - For types that can be introspected, but rarely have requirements to be compared
|
|
165
|
+
// (`ArrayBuffer`, `DataView`, etc.), the cost is avoided to prioritize the common
|
|
166
|
+
// use-cases.
|
|
167
|
+
// - For types that can be introspected, but do not have an objective definition of what
|
|
168
|
+
// equality is (`Error`, etc.), the subjective decision was to be conservative.
|
|
169
|
+
// In all cases, these decisions should be reevaluated based on changes to the language and
|
|
170
|
+
// common development practices.
|
|
171
|
+
return a === b;
|
|
85
172
|
}
|
|
86
|
-
return
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
* create a custom isEqual handler specific to circular objects
|
|
90
|
-
*
|
|
91
|
-
* @param [isEqual] the isEqual comparator to use instead of isDeepEqual
|
|
92
|
-
* @returns the method to create the `isEqual` function
|
|
93
|
-
*/
|
|
94
|
-
function createCircularEqualCreator(isEqual) {
|
|
95
|
-
return function createCircularEqual(comparator) {
|
|
96
|
-
var _comparator = isEqual || comparator;
|
|
97
|
-
return function circularEqual(a, b, indexOrKeyA, indexOrKeyB, parentA, parentB, cache) {
|
|
98
|
-
if (cache === void 0) { cache = getNewCache(); }
|
|
99
|
-
var isCacheableA = !!a && typeof a === 'object';
|
|
100
|
-
var isCacheableB = !!b && typeof b === 'object';
|
|
101
|
-
if (isCacheableA !== isCacheableB) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
if (!isCacheableA && !isCacheableB) {
|
|
105
|
-
return _comparator(a, b, cache);
|
|
106
|
-
}
|
|
107
|
-
var cachedA = cache.get(a);
|
|
108
|
-
if (cachedA && cache.get(b)) {
|
|
109
|
-
return cachedA === b;
|
|
110
|
-
}
|
|
111
|
-
cache.set(a, b);
|
|
112
|
-
cache.set(b, a);
|
|
113
|
-
var result = _comparator(a, b, cache);
|
|
114
|
-
cache.delete(a);
|
|
115
|
-
cache.delete(b);
|
|
116
|
-
return result;
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
}
|
|
173
|
+
return comparator;
|
|
174
|
+
}
|
|
175
|
+
|
|
120
176
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* @param a the array to test
|
|
124
|
-
* @param b the array to test against
|
|
125
|
-
* @param isEqual the comparator to determine equality
|
|
126
|
-
* @param meta the meta object to pass through
|
|
127
|
-
* @returns are the arrays equal
|
|
177
|
+
* Whether the arrays are equal in value.
|
|
128
178
|
*/
|
|
129
179
|
function areArraysEqual(a, b, isEqual, meta) {
|
|
130
180
|
var index = a.length;
|
|
131
181
|
if (b.length !== index) {
|
|
132
182
|
return false;
|
|
133
183
|
}
|
|
184
|
+
// Decrementing `while` showed faster results than either incrementing or
|
|
185
|
+
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
186
|
+
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
187
|
+
// cost of anonymous callbacks.
|
|
134
188
|
while (index-- > 0) {
|
|
135
189
|
if (!isEqual(a[index], b[index], index, index, a, b, meta)) {
|
|
136
190
|
return false;
|
|
@@ -139,206 +193,230 @@ function areArraysEqual(a, b, isEqual, meta) {
|
|
|
139
193
|
return true;
|
|
140
194
|
}
|
|
141
195
|
/**
|
|
142
|
-
*
|
|
196
|
+
* Whether the arrays are equal in value, including circular references.
|
|
197
|
+
*/
|
|
198
|
+
var areArraysEqualCircular = createIsCircular(areArraysEqual);
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Whether the dates passed are equal in value.
|
|
143
202
|
*
|
|
144
|
-
* @
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
|
|
148
|
-
|
|
203
|
+
* @NOTE
|
|
204
|
+
* This is a standalone function instead of done inline in the comparator
|
|
205
|
+
* to allow for overrides.
|
|
206
|
+
*/
|
|
207
|
+
function areDatesEqual(a, b) {
|
|
208
|
+
return sameValueZeroEqual(a.valueOf(), b.valueOf());
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether the `Map`s are equal in value.
|
|
149
213
|
*/
|
|
150
214
|
function areMapsEqual(a, b, isEqual, meta) {
|
|
151
215
|
var isValueEqual = a.size === b.size;
|
|
152
|
-
if (isValueEqual
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
216
|
+
if (!isValueEqual) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (!a.size) {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
// The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
|
|
223
|
+
// the inability to control the performance of the resulting code. It also avoids excessive
|
|
224
|
+
// iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
|
|
225
|
+
// we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
|
|
226
|
+
// equality checks themselves.
|
|
227
|
+
var matchedIndices = {};
|
|
228
|
+
var indexA = 0;
|
|
229
|
+
a.forEach(function (aValue, aKey) {
|
|
230
|
+
if (!isValueEqual) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
var hasMatch = false;
|
|
234
|
+
var matchIndexB = 0;
|
|
235
|
+
b.forEach(function (bValue, bKey) {
|
|
236
|
+
if (!hasMatch &&
|
|
237
|
+
!matchedIndices[matchIndexB] &&
|
|
238
|
+
(hasMatch =
|
|
239
|
+
isEqual(aKey, bKey, indexA, matchIndexB, a, b, meta) &&
|
|
240
|
+
isEqual(aValue, bValue, aKey, bKey, a, b, meta))) {
|
|
241
|
+
matchedIndices[matchIndexB] = true;
|
|
172
242
|
}
|
|
243
|
+
matchIndexB++;
|
|
173
244
|
});
|
|
174
|
-
|
|
245
|
+
indexA++;
|
|
246
|
+
isValueEqual = hasMatch;
|
|
247
|
+
});
|
|
175
248
|
return isValueEqual;
|
|
176
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Whether the `Map`s are equal in value, including circular references.
|
|
252
|
+
*/
|
|
253
|
+
var areMapsEqualCircular = createIsCircular(areMapsEqual);
|
|
254
|
+
|
|
177
255
|
var OWNER = '_owner';
|
|
178
|
-
var hasOwnProperty =
|
|
256
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
179
257
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* @param a the object to test
|
|
183
|
-
* @param b the object to test against
|
|
184
|
-
* @param isEqual the comparator to determine equality
|
|
185
|
-
* @param meta the meta object to pass through
|
|
186
|
-
* @returns are the objects equal
|
|
258
|
+
* Whether the objects are equal in value.
|
|
187
259
|
*/
|
|
188
260
|
function areObjectsEqual(a, b, isEqual, meta) {
|
|
189
|
-
var keysA = keys(a);
|
|
261
|
+
var keysA = Object.keys(a);
|
|
190
262
|
var index = keysA.length;
|
|
191
|
-
if (keys(b).length !== index) {
|
|
263
|
+
if (Object.keys(b).length !== index) {
|
|
192
264
|
return false;
|
|
193
265
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
if (!hasOwnProperty(b, key) ||
|
|
207
|
-
!isEqual(a[key], b[key], key, key, a, b, meta)) {
|
|
266
|
+
var key;
|
|
267
|
+
// Decrementing `while` showed faster results than either incrementing or
|
|
268
|
+
// decrementing `for` loop and than an incrementing `while` loop. Declarative
|
|
269
|
+
// methods like `some` / `every` were not used to avoid incurring the garbage
|
|
270
|
+
// cost of anonymous callbacks.
|
|
271
|
+
while (index-- > 0) {
|
|
272
|
+
key = keysA[index];
|
|
273
|
+
if (key === OWNER) {
|
|
274
|
+
var reactElementA = !!a.$$typeof;
|
|
275
|
+
var reactElementB = !!b.$$typeof;
|
|
276
|
+
if ((reactElementA || reactElementB) && reactElementA !== reactElementB) {
|
|
208
277
|
return false;
|
|
209
278
|
}
|
|
210
279
|
}
|
|
280
|
+
if (!hasOwnProperty.call(b, key) ||
|
|
281
|
+
!isEqual(a[key], b[key], key, key, a, b, meta)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
211
284
|
}
|
|
212
285
|
return true;
|
|
213
286
|
}
|
|
214
287
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* @param a the regExp to test
|
|
218
|
-
* @param b the regExp to test agains
|
|
219
|
-
* @returns are the regExps equal
|
|
288
|
+
* Whether the objects are equal in value, including circular references.
|
|
220
289
|
*/
|
|
221
|
-
var
|
|
222
|
-
|
|
223
|
-
return function areRegExpsEqual(a, b) {
|
|
224
|
-
return a.source === b.source && a.flags === b.flags;
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
return function areRegExpsEqualFallback(a, b) {
|
|
228
|
-
return (a.source === b.source &&
|
|
229
|
-
a.global === b.global &&
|
|
230
|
-
a.ignoreCase === b.ignoreCase &&
|
|
231
|
-
a.multiline === b.multiline &&
|
|
232
|
-
a.unicode === b.unicode &&
|
|
233
|
-
a.sticky === b.sticky &&
|
|
234
|
-
a.lastIndex === b.lastIndex);
|
|
235
|
-
};
|
|
236
|
-
})();
|
|
290
|
+
var areObjectsEqualCircular = createIsCircular(areObjectsEqual);
|
|
291
|
+
|
|
237
292
|
/**
|
|
238
|
-
*
|
|
293
|
+
* Whether the regexps passed are equal in value.
|
|
239
294
|
*
|
|
240
|
-
* @
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
|
|
295
|
+
* @NOTE
|
|
296
|
+
* This is a standalone function instead of done inline in the comparator
|
|
297
|
+
* to allow for overrides. An example of this would be supporting a
|
|
298
|
+
* pre-ES2015 environment where the `flags` property is not available.
|
|
299
|
+
*/
|
|
300
|
+
function areRegExpsEqual(a, b) {
|
|
301
|
+
return a.source === b.source && a.flags === b.flags;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Whether the `Set`s are equal in value.
|
|
245
306
|
*/
|
|
246
307
|
function areSetsEqual(a, b, isEqual, meta) {
|
|
247
308
|
var isValueEqual = a.size === b.size;
|
|
248
|
-
if (isValueEqual
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
309
|
+
if (!isValueEqual) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
if (!a.size) {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
// The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
|
|
316
|
+
// the inability to control the performance of the resulting code. It also avoids excessive
|
|
317
|
+
// iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
|
|
318
|
+
// we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
|
|
319
|
+
// equality checks themselves.
|
|
320
|
+
var matchedIndices = {};
|
|
321
|
+
a.forEach(function (aValue, aKey) {
|
|
322
|
+
if (!isValueEqual) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
var hasMatch = false;
|
|
326
|
+
var matchIndex = 0;
|
|
327
|
+
b.forEach(function (bValue, bKey) {
|
|
328
|
+
if (!hasMatch &&
|
|
329
|
+
!matchedIndices[matchIndex] &&
|
|
330
|
+
(hasMatch = isEqual(aValue, bValue, aKey, bKey, a, b, meta))) {
|
|
331
|
+
matchedIndices[matchIndex] = true;
|
|
264
332
|
}
|
|
333
|
+
matchIndex++;
|
|
265
334
|
});
|
|
266
|
-
|
|
335
|
+
isValueEqual = hasMatch;
|
|
336
|
+
});
|
|
267
337
|
return isValueEqual;
|
|
268
|
-
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Whether the `Set`s are equal in value, including circular references.
|
|
341
|
+
*/
|
|
342
|
+
var areSetsEqualCircular = createIsCircular(areSetsEqual);
|
|
269
343
|
|
|
270
|
-
var
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
344
|
+
var DEFAULT_CONFIG = Object.freeze({
|
|
345
|
+
areArraysEqual: areArraysEqual,
|
|
346
|
+
areDatesEqual: areDatesEqual,
|
|
347
|
+
areMapsEqual: areMapsEqual,
|
|
348
|
+
areObjectsEqual: areObjectsEqual,
|
|
349
|
+
areRegExpsEqual: areRegExpsEqual,
|
|
350
|
+
areSetsEqual: areSetsEqual,
|
|
351
|
+
createIsNestedEqual: createDefaultIsNestedEqual,
|
|
352
|
+
});
|
|
353
|
+
var DEFAULT_CIRCULAR_CONFIG = Object.freeze({
|
|
354
|
+
areArraysEqual: areArraysEqualCircular,
|
|
355
|
+
areDatesEqual: areDatesEqual,
|
|
356
|
+
areMapsEqual: areMapsEqualCircular,
|
|
357
|
+
areObjectsEqual: areObjectsEqualCircular,
|
|
358
|
+
areRegExpsEqual: areRegExpsEqual,
|
|
359
|
+
areSetsEqual: areSetsEqualCircular,
|
|
360
|
+
createIsNestedEqual: createDefaultIsNestedEqual,
|
|
361
|
+
});
|
|
362
|
+
var isDeepEqual = createComparator(DEFAULT_CONFIG);
|
|
363
|
+
/**
|
|
364
|
+
* Whether the items passed are deeply-equal in value.
|
|
365
|
+
*/
|
|
366
|
+
function deepEqual(a, b) {
|
|
367
|
+
return isDeepEqual(a, b, undefined);
|
|
368
|
+
}
|
|
369
|
+
var isShallowEqual = createComparator(merge(DEFAULT_CONFIG, { createIsNestedEqual: function () { return sameValueZeroEqual; } }));
|
|
370
|
+
/**
|
|
371
|
+
* Whether the items passed are shallowly-equal in value.
|
|
372
|
+
*/
|
|
373
|
+
function shallowEqual(a, b) {
|
|
374
|
+
return isShallowEqual(a, b, undefined);
|
|
375
|
+
}
|
|
376
|
+
var isCircularDeepEqual = createComparator(DEFAULT_CIRCULAR_CONFIG);
|
|
377
|
+
/**
|
|
378
|
+
* Whether the items passed are deeply-equal in value, including circular references.
|
|
379
|
+
*/
|
|
380
|
+
function circularDeepEqual(a, b) {
|
|
381
|
+
return isCircularDeepEqual(a, b, new WeakMap());
|
|
382
|
+
}
|
|
383
|
+
var isCircularShallowEqual = createComparator(merge(DEFAULT_CIRCULAR_CONFIG, {
|
|
384
|
+
createIsNestedEqual: function () { return sameValueZeroEqual; },
|
|
385
|
+
}));
|
|
386
|
+
/**
|
|
387
|
+
* Whether the items passed are shallowly-equal in value, including circular references.
|
|
388
|
+
*/
|
|
389
|
+
function circularShallowEqual(a, b) {
|
|
390
|
+
return isCircularShallowEqual(a, b, new WeakMap());
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Create a custom equality comparison method.
|
|
394
|
+
*
|
|
395
|
+
* This can be done to create very targeted comparisons in extreme hot-path scenarios
|
|
396
|
+
* where the standard methods are not performant enough, but can also be used to provide
|
|
397
|
+
* support for legacy environments that do not support expected features like
|
|
398
|
+
* `RegExp.prototype.flags` out of the box.
|
|
399
|
+
*/
|
|
400
|
+
function createCustomEqual(getComparatorOptions) {
|
|
401
|
+
return createComparator(merge(DEFAULT_CONFIG, getComparatorOptions(DEFAULT_CONFIG)));
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Create a custom equality comparison method that handles circular references. This is very
|
|
405
|
+
* similar to `createCustomEqual`, with the only difference being that `meta` expects to be
|
|
406
|
+
* populated with a `WeakMap`-like contract.
|
|
407
|
+
*
|
|
408
|
+
* This can be done to create very targeted comparisons in extreme hot-path scenarios
|
|
409
|
+
* where the standard methods are not performant enough, but can also be used to provide
|
|
410
|
+
* support for legacy environments that do not support expected features like
|
|
411
|
+
* `WeakMap` out of the box.
|
|
412
|
+
*/
|
|
413
|
+
function createCustomCircularEqual(getComparatorOptions) {
|
|
414
|
+
var comparator = createComparator(merge(DEFAULT_CIRCULAR_CONFIG, getComparatorOptions(DEFAULT_CIRCULAR_CONFIG)));
|
|
415
|
+
return (function (a, b, meta) {
|
|
416
|
+
if (meta === void 0) { meta = new WeakMap(); }
|
|
417
|
+
return comparator(a, b, meta);
|
|
418
|
+
});
|
|
336
419
|
}
|
|
337
420
|
|
|
338
|
-
|
|
339
|
-
var shallowEqual = createComparator(function () { return sameValueZeroEqual; });
|
|
340
|
-
var circularDeepEqual = createComparator(createCircularEqualCreator());
|
|
341
|
-
var circularShallowEqual = createComparator(createCircularEqualCreator(sameValueZeroEqual));
|
|
342
|
-
|
|
343
|
-
export { circularDeepEqual, circularShallowEqual, createComparator as createCustomEqual, deepEqual, sameValueZeroEqual, shallowEqual };
|
|
421
|
+
export { circularDeepEqual, circularShallowEqual, createCustomCircularEqual, createCustomEqual, deepEqual, sameValueZeroEqual, shallowEqual };
|
|
344
422
|
//# sourceMappingURL=fast-equals.esm.js.map
|