fast-equals 4.0.3 → 5.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.
Files changed (71) hide show
  1. package/.babelrc +34 -0
  2. package/.prettierrc +4 -0
  3. package/CHANGELOG.md +39 -0
  4. package/README.md +145 -39
  5. package/build/rollup/config.base.js +49 -0
  6. package/build/rollup/config.cjs.js +10 -0
  7. package/build/rollup/config.esm.js +10 -0
  8. package/build/rollup/config.min.js +13 -0
  9. package/build/rollup/config.umd.js +10 -0
  10. package/build/tsconfig/base.json +32 -0
  11. package/build/tsconfig/cjs.json +8 -0
  12. package/build/tsconfig/declarations.json +9 -0
  13. package/build/tsconfig/esm.json +8 -0
  14. package/build/tsconfig/min.json +8 -0
  15. package/build/tsconfig/umd.json +8 -0
  16. package/build/webpack.config.js +57 -0
  17. package/dist/cjs/index.cjs +510 -0
  18. package/dist/cjs/index.cjs.map +1 -0
  19. package/dist/cjs/types/comparator.d.ts +2 -0
  20. package/dist/cjs/types/equals.d.ts +46 -0
  21. package/dist/cjs/types/index.d.ts +56 -0
  22. package/dist/cjs/types/internalTypes.d.ts +36 -0
  23. package/dist/cjs/types/utils.d.ts +19 -0
  24. package/dist/esm/index.mjs +499 -0
  25. package/dist/esm/index.mjs.map +1 -0
  26. package/dist/esm/types/comparator.d.ts +2 -0
  27. package/dist/esm/types/equals.d.ts +46 -0
  28. package/dist/esm/types/index.d.ts +56 -0
  29. package/dist/esm/types/internalTypes.d.ts +36 -0
  30. package/dist/esm/types/utils.d.ts +19 -0
  31. package/dist/min/index.js +1 -0
  32. package/dist/min/types/comparator.d.ts +2 -0
  33. package/dist/min/types/equals.d.ts +46 -0
  34. package/dist/min/types/index.d.ts +56 -0
  35. package/dist/min/types/internalTypes.d.ts +36 -0
  36. package/dist/min/types/utils.d.ts +19 -0
  37. package/dist/umd/index.js +516 -0
  38. package/dist/umd/index.js.map +1 -0
  39. package/dist/umd/types/comparator.d.ts +2 -0
  40. package/dist/umd/types/equals.d.ts +46 -0
  41. package/dist/umd/types/index.d.ts +56 -0
  42. package/dist/umd/types/internalTypes.d.ts +36 -0
  43. package/dist/umd/types/utils.d.ts +19 -0
  44. package/package.json +58 -37
  45. package/recipes/explicit-property-check.md +4 -6
  46. package/recipes/legacy-circular-equal-support.md +28 -20
  47. package/recipes/legacy-regexp-support.md +15 -16
  48. package/recipes/non-standard-properties.md +35 -23
  49. package/recipes/using-meta-in-comparison.md +10 -13
  50. package/src/comparator.ts +43 -40
  51. package/src/equals.ts +263 -0
  52. package/src/index.ts +183 -79
  53. package/src/internalTypes.ts +74 -0
  54. package/src/utils.ts +42 -49
  55. package/dist/fast-equals.cjs.js +0 -432
  56. package/dist/fast-equals.cjs.js.map +0 -1
  57. package/dist/fast-equals.esm.js +0 -422
  58. package/dist/fast-equals.esm.js.map +0 -1
  59. package/dist/fast-equals.js +0 -438
  60. package/dist/fast-equals.js.map +0 -1
  61. package/dist/fast-equals.min.js +0 -1
  62. package/dist/fast-equals.mjs +0 -422
  63. package/dist/fast-equals.mjs.map +0 -1
  64. package/index.d.ts +0 -58
  65. package/recipes/strict-property-descriptor-check.md +0 -42
  66. package/src/arrays.ts +0 -36
  67. package/src/dates.ts +0 -12
  68. package/src/maps.ts +0 -66
  69. package/src/objects.ts +0 -62
  70. package/src/regexps.ts +0 -11
  71. package/src/sets.ts +0 -61
@@ -0,0 +1,56 @@
1
+ import type { CreateCustomComparatorConfig, CreateState, EqualityComparator } from './internalTypes';
2
+ import { createInternalComparator, sameValueZeroEqual } from './utils';
3
+ export { sameValueZeroEqual };
4
+ interface DefaultEqualCreatorOptions<Meta> {
5
+ comparator?: EqualityComparator<Meta>;
6
+ circular?: boolean;
7
+ strict?: boolean;
8
+ }
9
+ interface CustomEqualCreatorOptions<Meta> extends DefaultEqualCreatorOptions<Meta> {
10
+ createCustomConfig?: CreateCustomComparatorConfig<Meta>;
11
+ createInternalComparator?: typeof createInternalComparator;
12
+ createState?: CreateState<Meta>;
13
+ }
14
+ /**
15
+ * Whether the items passed are deeply-equal in value.
16
+ */
17
+ export declare const deepEqual: <A, B>(a: A, b: B) => boolean;
18
+ /**
19
+ * Whether the items passed are deeply-equal in value based on strict comparison.
20
+ */
21
+ export declare const strictDeepEqual: <A, B>(a: A, b: B) => boolean;
22
+ /**
23
+ * Whether the items passed are deeply-equal in value, including circular references.
24
+ */
25
+ export declare const circularDeepEqual: <A, B>(a: A, b: B) => boolean;
26
+ /**
27
+ * Whether the items passed are deeply-equal in value, including circular references,
28
+ * based on strict comparison.
29
+ */
30
+ export declare const strictCircularDeepEqual: <A, B>(a: A, b: B) => boolean;
31
+ /**
32
+ * Whether the items passed are shallowly-equal in value.
33
+ */
34
+ export declare const shallowEqual: <A, B>(a: A, b: B) => boolean;
35
+ /**
36
+ * Whether the items passed are shallowly-equal in value based on strict comparison
37
+ */
38
+ export declare const strictShallowEqual: <A, B>(a: A, b: B) => boolean;
39
+ /**
40
+ * Whether the items passed are shallowly-equal in value, including circular references.
41
+ */
42
+ export declare const circularShallowEqual: <A, B>(a: A, b: B) => boolean;
43
+ /**
44
+ * Whether the items passed are shallowly-equal in value, including circular references,
45
+ * based on strict comparison.
46
+ */
47
+ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
48
+ /**
49
+ * Create a custom equality comparison method.
50
+ *
51
+ * This can be done to create very targeted comparisons in extreme hot-path scenarios
52
+ * where the standard methods are not performant enough, but can also be used to provide
53
+ * support for legacy environments that do not support expected features like
54
+ * `RegExp.prototype.flags` out of the box.
55
+ */
56
+ export declare function createCustomEqual<Meta>(options?: CustomEqualCreatorOptions<Meta>): <A, B>(a: A, b: B, metaOverride?: Meta) => boolean;
@@ -0,0 +1,36 @@
1
+ export interface BaseCircular extends Pick<WeakMap<any, any>, 'delete' | 'get'> {
2
+ set(key: object, value: any): any;
3
+ }
4
+ export type State<Meta> = CircularState<Meta> | DefaultState<Meta>;
5
+ export interface CircularState<Meta> {
6
+ readonly cache: BaseCircular;
7
+ readonly equals: InternalEqualityComparator<Meta>;
8
+ meta: Meta;
9
+ readonly strict: boolean;
10
+ }
11
+ export interface DefaultState<Meta> {
12
+ readonly cache: undefined;
13
+ readonly equals: InternalEqualityComparator<Meta>;
14
+ meta: Meta;
15
+ readonly strict: boolean;
16
+ }
17
+ export interface Dictionary<Value = any> {
18
+ [key: string | symbol]: Value;
19
+ $$typeof?: any;
20
+ }
21
+ export interface ComparatorConfig<Meta> {
22
+ areArraysEqual: TypeEqualityComparator<any, Meta>;
23
+ areDatesEqual: TypeEqualityComparator<any, Meta>;
24
+ areMapsEqual: TypeEqualityComparator<any, Meta>;
25
+ areObjectsEqual: TypeEqualityComparator<any, Meta>;
26
+ arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
27
+ areRegExpsEqual: TypeEqualityComparator<any, Meta>;
28
+ areSetsEqual: TypeEqualityComparator<any, Meta>;
29
+ }
30
+ export type CreateCustomComparatorConfig<Meta> = (config: ComparatorConfig<Meta>) => Partial<ComparatorConfig<Meta>>;
31
+ export type CreateState<Meta> = (comparator: EqualityComparator<Meta>) => Partial<State<Meta>>;
32
+ export type EqualityComparator<Meta> = <A, B>(a: A, b: B, state: State<Meta>) => boolean;
33
+ export type AnyEqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
34
+ export type EqualityComparatorCreator<Meta> = (fn: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
35
+ export type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
36
+ export type TypeEqualityComparator<Type, Meta = undefined> = (a: Type, b: Type, state: State<Meta>) => boolean;
@@ -0,0 +1,19 @@
1
+ import { AnyEqualityComparator, Dictionary, EqualityComparator, InternalEqualityComparator, State, TypeEqualityComparator } from './internalTypes';
2
+ export declare function combineComparators<Meta>(comparatorA: AnyEqualityComparator<Meta>, comparatorB: AnyEqualityComparator<Meta>): <A, B>(a: A, b: B, state: State<Meta>) => boolean;
3
+ /**
4
+ * Default equality comparator pass-through, used as the standard `isEqual` creator for
5
+ * use inside the built comparator.
6
+ */
7
+ export declare function createInternalComparator<Meta>(compare: EqualityComparator<Meta>): InternalEqualityComparator<Meta>;
8
+ /**
9
+ * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
10
+ * for circular references to be safely included in the comparison without creating
11
+ * stack overflows.
12
+ */
13
+ export declare function createIsCircular<AreItemsEqual extends TypeEqualityComparator<any, any>>(areItemsEqual: AreItemsEqual): AreItemsEqual;
14
+ export declare function getStrictProperties(object: Dictionary): Array<string | symbol>;
15
+ export declare const hasOwn: (o: object, v: PropertyKey) => boolean;
16
+ /**
17
+ * Whether the values passed are strictly equal or both NaN.
18
+ */
19
+ export declare function sameValueZeroEqual(a: any, b: any): boolean;
@@ -0,0 +1,499 @@
1
+ var ARGUMENTS_TAG = '[object Arguments]';
2
+ var BOOLEAN_TAG = '[object Boolean]';
3
+ var DATE_TAG = '[object Date]';
4
+ var MAP_TAG = '[object Map]';
5
+ var NUMBER_TAG = '[object Number]';
6
+ var OBJECT_TAG = '[object Object]';
7
+ var REG_EXP_TAG = '[object RegExp]';
8
+ var SET_TAG = '[object Set]';
9
+ var STRING_TAG = '[object String]';
10
+ var isArray = Array.isArray;
11
+ var getTag = Object.prototype.toString.call.bind(Object.prototype.toString);
12
+ function createComparator(_a) {
13
+ var areArraysEqual = _a.areArraysEqual, areDatesEqual = _a.areDatesEqual, areMapsEqual = _a.areMapsEqual, areObjectsEqual = _a.areObjectsEqual, arePrimitiveWrappersEqual = _a.arePrimitiveWrappersEqual, areRegExpsEqual = _a.areRegExpsEqual, areSetsEqual = _a.areSetsEqual;
14
+ /**
15
+ * compare the value of the two objects and return true if they are equivalent in values
16
+ */
17
+ return function comparator(a, b, state) {
18
+ // If the items are strictly equal, no need to do a value comparison.
19
+ if (a === b) {
20
+ return true;
21
+ }
22
+ // If the items are not non-nullish objects, then the only possibility
23
+ // of them being equal but not strictly is if they are both `NaN`. Since
24
+ // `NaN` is uniquely not equal to itself, we can use self-comparison of
25
+ // both objects, which is faster than `isNaN()`.
26
+ if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
27
+ return a !== a && b !== b;
28
+ }
29
+ // Checks are listed in order of commonality of use-case:
30
+ // 1. Common complex object types (plain object, array)
31
+ // 2. Common data values (date, regexp)
32
+ // 3. Less-common complex object types (map, set)
33
+ // 4. Less-common data values (promise, primitive wrappers)
34
+ // Inherently this is both subjective and assumptive, however
35
+ // when reviewing comparable libraries in the wild this order
36
+ // appears to be generally consistent.
37
+ // `isPlainObject` only checks against the object's own realm. Cross-realm
38
+ // comparisons are rare, and will be handled in the ultimate fallback, so
39
+ // we can avoid the `toString.call()` cost unless necessary.
40
+ if (a.constructor === Object && b.constructor === Object) {
41
+ return areObjectsEqual(a, b, state);
42
+ }
43
+ // `isArray()` works on subclasses and is cross-realm, so we can again avoid
44
+ // the `toString.call()` cost unless necessary by just checking if either
45
+ // and then both are arrays.
46
+ var aArray = isArray(a);
47
+ var bArray = isArray(b);
48
+ if (aArray || bArray) {
49
+ return aArray === bArray && areArraysEqual(a, b, state);
50
+ }
51
+ // Since this is a custom object, use the classic `toString.call()` to get its
52
+ // type. This is reasonably performant in modern environments like v8 and
53
+ // SpiderMonkey, and allows for cross-realm comparison when other checks like
54
+ // `instanceof` do not.
55
+ var tag = getTag(a);
56
+ if (tag !== getTag(b)) {
57
+ return false;
58
+ }
59
+ if (tag === DATE_TAG) {
60
+ return areDatesEqual(a, b, state);
61
+ }
62
+ if (tag === REG_EXP_TAG) {
63
+ return areRegExpsEqual(a, b, state);
64
+ }
65
+ if (tag === MAP_TAG) {
66
+ return areMapsEqual(a, b, state);
67
+ }
68
+ if (tag === SET_TAG) {
69
+ return areSetsEqual(a, b, state);
70
+ }
71
+ // If a simple object tag, then we can prioritize a simple object comparison because
72
+ // it is likely a custom class.
73
+ if (tag === OBJECT_TAG) {
74
+ // The exception for value comparison is `Promise`-like contracts. These should be
75
+ // treated the same as standard `Promise` objects, which means strict equality, and if
76
+ // it reaches this point then that strict equality comparison has already failed.
77
+ return (typeof a.then !== 'function' &&
78
+ typeof b.then !== 'function' &&
79
+ areObjectsEqual(a, b, state));
80
+ }
81
+ // If an arguments tag, it should be treated as a standard object.
82
+ if (tag === ARGUMENTS_TAG) {
83
+ return areObjectsEqual(a, b, state);
84
+ }
85
+ // As the penultimate fallback, check if the values passed are primitive wrappers. This
86
+ // is very rare in modern JS, which is why it is deprioritized compared to all other object
87
+ // types.
88
+ if (tag === BOOLEAN_TAG || tag === NUMBER_TAG || tag === STRING_TAG) {
89
+ return arePrimitiveWrappersEqual(a, b, state);
90
+ }
91
+ // If not matching any tags that require a specific type of comparison, then we hard-code false because
92
+ // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
93
+ // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
94
+ // comparison that can be made.
95
+ // - For types that can be introspected, but rarely have requirements to be compared
96
+ // (`ArrayBuffer`, `DataView`, etc.), the cost is avoided to prioritize the common
97
+ // use-cases (may be included in a future release, if requested enough).
98
+ // - For types that can be introspected but do not have an objective definition of what
99
+ // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
100
+ // In all cases, these decisions should be reevaluated based on changes to the language and
101
+ // common development practices.
102
+ return false;
103
+ };
104
+ }
105
+
106
+ var getOwnPropertyNames = Object.getOwnPropertyNames, getOwnPropertySymbols = Object.getOwnPropertySymbols;
107
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
108
+ function combineComparators(comparatorA, comparatorB) {
109
+ return function isEqual(a, b, state) {
110
+ return comparatorA(a, b, state) && comparatorB(a, b, state);
111
+ };
112
+ }
113
+ /**
114
+ * Default equality comparator pass-through, used as the standard `isEqual` creator for
115
+ * use inside the built comparator.
116
+ */
117
+ function createInternalComparator(compare) {
118
+ return function (a, b, _indexOrKeyA, _indexOrKeyB, _parentA, _parentB, state) {
119
+ return compare(a, b, state);
120
+ };
121
+ }
122
+ /**
123
+ * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
124
+ * for circular references to be safely included in the comparison without creating
125
+ * stack overflows.
126
+ */
127
+ function createIsCircular(areItemsEqual) {
128
+ return function isCircular(a, b, state) {
129
+ if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
130
+ return areItemsEqual(a, b, state);
131
+ }
132
+ var cache = state.cache;
133
+ var cachedA = cache.get(a);
134
+ var cachedB = cache.get(b);
135
+ if (cachedA && cachedB) {
136
+ return cachedA === b && cachedB === a;
137
+ }
138
+ cache.set(a, b);
139
+ cache.set(b, a);
140
+ var result = areItemsEqual(a, b, state);
141
+ cache.delete(a);
142
+ cache.delete(b);
143
+ return result;
144
+ };
145
+ }
146
+ function getStrictProperties(object) {
147
+ return getOwnPropertyNames(object).concat(getOwnPropertySymbols(object));
148
+ }
149
+ var hasOwn = Object.hasOwn ||
150
+ (function (object, property) {
151
+ return hasOwnProperty.call(object, property);
152
+ });
153
+ /**
154
+ * Whether the values passed are strictly equal or both NaN.
155
+ */
156
+ function sameValueZeroEqual(a, b) {
157
+ return a || b ? a === b : a === b || (a !== a && b !== b);
158
+ }
159
+
160
+ var OWNER = '_owner';
161
+ var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor, keys = Object.keys;
162
+ /**
163
+ * Whether the arrays are equal in value.
164
+ */
165
+ function areArraysEqual(a, b, state) {
166
+ var index = a.length;
167
+ if (b.length !== index) {
168
+ return false;
169
+ }
170
+ while (index-- > 0) {
171
+ if (!state.equals(a[index], b[index], index, index, a, b, state)) {
172
+ return false;
173
+ }
174
+ }
175
+ return true;
176
+ }
177
+ /**
178
+ * Whether the dates passed are equal in value.
179
+ *
180
+ * @NOTE
181
+ * This is a standalone function instead of done inline in the comparator
182
+ * to allow for overrides.
183
+ */
184
+ function areDatesEqual(a, b) {
185
+ return sameValueZeroEqual(a.getTime(), b.getTime());
186
+ }
187
+ /**
188
+ * Whether the `Map`s are equal in value.
189
+ */
190
+ function areMapsEqual(a, b, state) {
191
+ var isValueEqual = a.size === b.size;
192
+ if (isValueEqual && a.size) {
193
+ // The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
194
+ // the inability to control the performance of the resulting code. It also avoids excessive
195
+ // iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
196
+ // we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
197
+ // equality checks themselves.
198
+ var matchedIndices_1 = {};
199
+ var indexA_1 = 0;
200
+ a.forEach(function (aValue, aKey) {
201
+ if (!isValueEqual) {
202
+ return;
203
+ }
204
+ var hasMatch = false;
205
+ var matchIndexB = 0;
206
+ b.forEach(function (bValue, bKey) {
207
+ if (!hasMatch &&
208
+ !matchedIndices_1[matchIndexB] &&
209
+ (hasMatch =
210
+ state.equals(aKey, bKey, indexA_1, matchIndexB, a, b, state) &&
211
+ state.equals(aValue, bValue, aKey, bKey, a, b, state))) {
212
+ matchedIndices_1[matchIndexB] = true;
213
+ }
214
+ matchIndexB++;
215
+ });
216
+ indexA_1++;
217
+ isValueEqual = hasMatch;
218
+ });
219
+ }
220
+ return isValueEqual;
221
+ }
222
+ /**
223
+ * Whether the objects are equal in value.
224
+ */
225
+ function areObjectsEqual(a, b, state) {
226
+ var properties = keys(a);
227
+ var index = properties.length;
228
+ if (keys(b).length !== index) {
229
+ return false;
230
+ }
231
+ var property;
232
+ // Decrementing `while` showed faster results than either incrementing or
233
+ // decrementing `for` loop and than an incrementing `while` loop. Declarative
234
+ // methods like `some` / `every` were not used to avoid incurring the garbage
235
+ // cost of anonymous callbacks.
236
+ while (index-- > 0) {
237
+ property = properties[index];
238
+ if (property === OWNER &&
239
+ (a.$$typeof || b.$$typeof) &&
240
+ a.$$typeof !== b.$$typeof) {
241
+ return false;
242
+ }
243
+ if (!hasOwn(b, property) ||
244
+ !state.equals(a[property], b[property], property, property, a, b, state)) {
245
+ return false;
246
+ }
247
+ }
248
+ return true;
249
+ }
250
+ /**
251
+ * Whether the objects are equal in value with strict property checking.
252
+ */
253
+ function areObjectsEqualStrict(a, b, state) {
254
+ var properties = getStrictProperties(a);
255
+ var index = properties.length;
256
+ if (getStrictProperties(b).length !== index) {
257
+ return false;
258
+ }
259
+ var property;
260
+ var descriptorA;
261
+ var descriptorB;
262
+ // Decrementing `while` showed faster results than either incrementing or
263
+ // decrementing `for` loop and than an incrementing `while` loop. Declarative
264
+ // methods like `some` / `every` were not used to avoid incurring the garbage
265
+ // cost of anonymous callbacks.
266
+ while (index-- > 0) {
267
+ property = properties[index];
268
+ if (property === OWNER &&
269
+ (a.$$typeof || b.$$typeof) &&
270
+ a.$$typeof !== b.$$typeof) {
271
+ return false;
272
+ }
273
+ if (!hasOwn(b, property)) {
274
+ return false;
275
+ }
276
+ if (!state.equals(a[property], b[property], property, property, a, b, state)) {
277
+ return false;
278
+ }
279
+ descriptorA = getOwnPropertyDescriptor(a, property);
280
+ descriptorB = getOwnPropertyDescriptor(b, property);
281
+ if ((descriptorA || descriptorB) &&
282
+ (!descriptorA ||
283
+ !descriptorB ||
284
+ descriptorA.configurable !== descriptorB.configurable ||
285
+ descriptorA.enumerable !== descriptorB.enumerable ||
286
+ descriptorA.writable !== descriptorB.writable)) {
287
+ return false;
288
+ }
289
+ }
290
+ return true;
291
+ }
292
+ /**
293
+ * Whether the primitive wrappers passed are equal in value.
294
+ *
295
+ * @NOTE
296
+ * This is a standalone function instead of done inline in the comparator
297
+ * to allow for overrides.
298
+ */
299
+ function arePrimitiveWrappersEqual(a, b) {
300
+ return sameValueZeroEqual(a.valueOf(), b.valueOf());
301
+ }
302
+ /**
303
+ * Whether the regexps passed are equal in value.
304
+ *
305
+ * @NOTE
306
+ * This is a standalone function instead of done inline in the comparator
307
+ * to allow for overrides. An example of this would be supporting a
308
+ * pre-ES2015 environment where the `flags` property is not available.
309
+ */
310
+ function areRegExpsEqual(a, b) {
311
+ return a.source === b.source && a.flags === b.flags;
312
+ }
313
+ /**
314
+ * Whether the `Set`s are equal in value.
315
+ */
316
+ function areSetsEqual(a, b, state) {
317
+ var isValueEqual = a.size === b.size;
318
+ if (isValueEqual && a.size) {
319
+ // The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
320
+ // the inability to control the performance of the resulting code. It also avoids excessive
321
+ // iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
322
+ // we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
323
+ // equality checks themselves.
324
+ var matchedIndices_2 = {};
325
+ a.forEach(function (aValue, aKey) {
326
+ if (!isValueEqual) {
327
+ return;
328
+ }
329
+ var hasMatch = false;
330
+ var matchIndex = 0;
331
+ b.forEach(function (bValue, bKey) {
332
+ if (!hasMatch &&
333
+ !matchedIndices_2[matchIndex] &&
334
+ (hasMatch = state.equals(aValue, bValue, aKey, bKey, a, b, state))) {
335
+ matchedIndices_2[matchIndex] = true;
336
+ }
337
+ matchIndex++;
338
+ });
339
+ isValueEqual = hasMatch;
340
+ });
341
+ }
342
+ return isValueEqual;
343
+ }
344
+
345
+ function createComparatorConfig(_a) {
346
+ var circular = _a.circular, strict = _a.strict;
347
+ var config = {
348
+ areArraysEqual: areArraysEqual,
349
+ areDatesEqual: areDatesEqual,
350
+ areMapsEqual: areMapsEqual,
351
+ areObjectsEqual: areObjectsEqual,
352
+ arePrimitiveWrappersEqual: arePrimitiveWrappersEqual,
353
+ areRegExpsEqual: areRegExpsEqual,
354
+ areSetsEqual: areSetsEqual,
355
+ };
356
+ if (strict) {
357
+ config.areArraysEqual = areObjectsEqual;
358
+ config.areMapsEqual = combineComparators(areMapsEqual, areObjectsEqual);
359
+ config.areObjectsEqual = areObjectsEqualStrict;
360
+ config.areSetsEqual = combineComparators(areSetsEqual, areObjectsEqual);
361
+ }
362
+ if (circular) {
363
+ config.areArraysEqual = createIsCircular(config.areArraysEqual);
364
+ config.areMapsEqual = createIsCircular(config.areMapsEqual);
365
+ config.areObjectsEqual = createIsCircular(config.areObjectsEqual);
366
+ config.areSetsEqual = createIsCircular(config.areSetsEqual);
367
+ }
368
+ return config;
369
+ }
370
+ function createDefaultEqualCreator(options) {
371
+ if (options === void 0) { options = {}; }
372
+ var config = createComparatorConfig(options);
373
+ var isEqual = createComparator(config);
374
+ var isEqualComparator = options.comparator || createInternalComparator(isEqual);
375
+ var strict = !!options.strict;
376
+ if (options.circular) {
377
+ return function equals(a, b) {
378
+ return isEqual(a, b, {
379
+ cache: new WeakMap(),
380
+ equals: isEqualComparator,
381
+ meta: undefined,
382
+ strict: strict,
383
+ });
384
+ };
385
+ }
386
+ var state = Object.freeze({
387
+ cache: undefined,
388
+ equals: isEqualComparator,
389
+ meta: undefined,
390
+ strict: strict,
391
+ });
392
+ return function equals(a, b) {
393
+ return isEqual(a, b, state);
394
+ };
395
+ }
396
+ /**
397
+ * Whether the items passed are deeply-equal in value.
398
+ */
399
+ var deepEqual = createDefaultEqualCreator();
400
+ /**
401
+ * Whether the items passed are deeply-equal in value based on strict comparison.
402
+ */
403
+ var strictDeepEqual = createDefaultEqualCreator({ strict: true });
404
+ /**
405
+ * Whether the items passed are deeply-equal in value, including circular references.
406
+ */
407
+ var circularDeepEqual = createDefaultEqualCreator({ circular: true });
408
+ /**
409
+ * Whether the items passed are deeply-equal in value, including circular references,
410
+ * based on strict comparison.
411
+ */
412
+ var strictCircularDeepEqual = createDefaultEqualCreator({
413
+ circular: true,
414
+ strict: true,
415
+ });
416
+ /**
417
+ * Whether the items passed are shallowly-equal in value.
418
+ */
419
+ var shallowEqual = createDefaultEqualCreator({
420
+ comparator: sameValueZeroEqual,
421
+ });
422
+ /**
423
+ * Whether the items passed are shallowly-equal in value based on strict comparison
424
+ */
425
+ var strictShallowEqual = createDefaultEqualCreator({
426
+ comparator: sameValueZeroEqual,
427
+ strict: true,
428
+ });
429
+ /**
430
+ * Whether the items passed are shallowly-equal in value, including circular references.
431
+ */
432
+ var circularShallowEqual = createDefaultEqualCreator({
433
+ comparator: sameValueZeroEqual,
434
+ circular: true,
435
+ });
436
+ /**
437
+ * Whether the items passed are shallowly-equal in value, including circular references,
438
+ * based on strict comparison.
439
+ */
440
+ var strictCircularShallowEqual = createDefaultEqualCreator({
441
+ comparator: sameValueZeroEqual,
442
+ circular: true,
443
+ strict: true,
444
+ });
445
+ /**
446
+ * Create a custom equality comparison method.
447
+ *
448
+ * This can be done to create very targeted comparisons in extreme hot-path scenarios
449
+ * where the standard methods are not performant enough, but can also be used to provide
450
+ * support for legacy environments that do not support expected features like
451
+ * `RegExp.prototype.flags` out of the box.
452
+ */
453
+ function createCustomEqual(options) {
454
+ if (options === void 0) { options = {}; }
455
+ var comparator = options.comparator, createCustomInternalComparator = options.createInternalComparator, createCustomConfig = options.createCustomConfig, createState = options.createState;
456
+ var baseConfig = createComparatorConfig(options);
457
+ var config = createCustomConfig
458
+ ? Object.assign({}, baseConfig, createCustomConfig(baseConfig))
459
+ : baseConfig;
460
+ var isEqualCustom = comparator || createComparator(config);
461
+ var isEqualCustomComparator = createCustomInternalComparator
462
+ ? createCustomInternalComparator(isEqualCustom)
463
+ : createInternalComparator(isEqualCustom);
464
+ var strict = !!options.strict;
465
+ if (createState) {
466
+ return function isEqual(a, b, metaOverride) {
467
+ var customState = createState(isEqualCustom);
468
+ return isEqualCustom(a, b, {
469
+ cache: customState.cache || new WeakMap(),
470
+ equals: customState.equals || isEqualCustomComparator,
471
+ // @ts-expect-error - inferred `Meta` may be undefined, which is okay
472
+ meta: metaOverride !== undefined ? metaOverride : customState.meta,
473
+ strict: customState.strict !== undefined ? customState.strict : strict,
474
+ });
475
+ };
476
+ }
477
+ if (options.circular) {
478
+ return function equals(a, b) {
479
+ return isEqualCustom(a, b, {
480
+ cache: new WeakMap(),
481
+ equals: isEqualCustomComparator,
482
+ meta: undefined,
483
+ strict: strict,
484
+ });
485
+ };
486
+ }
487
+ var state = Object.freeze({
488
+ cache: undefined,
489
+ equals: isEqualCustomComparator,
490
+ meta: undefined,
491
+ strict: strict,
492
+ });
493
+ return function equals(a, b) {
494
+ return isEqualCustom(a, b, state);
495
+ };
496
+ }
497
+
498
+ export { circularDeepEqual, circularShallowEqual, createCustomEqual, deepEqual, sameValueZeroEqual, shallowEqual, strictCircularDeepEqual, strictCircularShallowEqual, strictDeepEqual, strictShallowEqual };
499
+ //# sourceMappingURL=index.mjs.map