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