fast-equals 6.4.2 → 6.4.3

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  > fast-equals
2
2
 
3
3
  Perform [blazing fast](#benchmarks) equality comparisons between two objects, while also allowing for flexibility for
4
- various use-cases. It has no dependencies, and is ~3.06kB when minified and gzipped.
4
+ various use-cases. It has no dependencies, and is ~3.26kB when minified and gzipped.
5
5
 
6
6
  The following types are handled out-of-the-box:
7
7
 
@@ -2,6 +2,38 @@
2
2
 
3
3
  const { getOwnPropertyDescriptor: getOwnPropertyDescriptor$1, getOwnPropertyNames, getOwnPropertySymbols } = Object;
4
4
  const { hasOwnProperty, toString: toString$1 } = Object.prototype;
5
+ /**
6
+ * Compare the pair while it is tracked in the cache, so that reaching it again further down returns
7
+ * whether it was reached as the same pair rather than recursing. This is the single implementation
8
+ * of circular tracking, shared by `createIsCircular` and `areOwnPropertiesEqualStrict`.
9
+ */
10
+ function areCircularItemsEqual(a, b, state, areItemsEqual, start) {
11
+ const { cache } = state;
12
+ const cachedA = cache.get(a);
13
+ const cachedB = cache.get(b);
14
+ if (cachedA && cachedB) {
15
+ return cachedA === b && cachedB === a;
16
+ }
17
+ cache.set(a, b);
18
+ cache.set(b, a);
19
+ const result = areItemsEqual(a, b, state, start);
20
+ // Either object may already be paired with another further up the stack (a `Set` / `Map` trying
21
+ // candidates, or a shared reference). Restoring that pairing, rather than deleting it, keeps the
22
+ // outer comparison able to detect its own cycle once this one returns.
23
+ if (cachedA) {
24
+ cache.set(a, cachedA);
25
+ }
26
+ else {
27
+ cache.delete(a);
28
+ }
29
+ if (cachedB) {
30
+ cache.set(b, cachedB);
31
+ }
32
+ else {
33
+ cache.delete(b);
34
+ }
35
+ return result;
36
+ }
5
37
  /**
6
38
  * Whether the own properties from `end` onward are equal with strict property checking, where the
7
39
  * caller has already verified that both objects have the same number of own properties.
@@ -43,60 +75,44 @@ function areStrictPropertiesEqual(a, b, state, properties, end) {
43
75
  * and otherwise every property from `start` is compared.
44
76
  *
45
77
  * @note
46
- * These objects rarely have own properties beyond their intrinsic ones, so that case returns before
47
- * any circular tracking is done. When they do, the properties can reference the object itself, so
48
- * the pair is tracked in the cache (when present) here rather than by wrapping the comparator with
49
- * `createIsCircular`. This keeps the cost of tracking off the common case.
78
+ * When `circular` is `true`, the pair is tracked for circular references, but only once it is known
79
+ * to have own properties beyond the intrinsic ones. These objects rarely have any, so the common case
80
+ * never touches the cache. Only the default strict comparators used in strict circular mode pass
81
+ * `true`; replaced comparators are wrapped with `createIsCircular` instead.
50
82
  */
51
- function areOwnPropertiesEqualStrict(a, b, state, start, intrinsic) {
83
+ function areOwnPropertiesEqualStrict(a, b, state, start, intrinsic, circular) {
52
84
  const properties = getOwnKeys(a);
53
- const length = properties.length;
85
+ const { length } = properties;
54
86
  if (getOwnKeys(b).length !== length) {
55
87
  return false;
56
88
  }
57
89
  if (length === intrinsic) {
58
90
  return true;
59
91
  }
60
- const cache = state.cache;
61
- if (!cache) {
62
- return areStrictPropertiesEqual(a, b, state, properties, start);
63
- }
64
- const cachedA = cache.get(a);
65
- const cachedB = cache.get(b);
66
- if (cachedA && cachedB) {
67
- return cachedA === b && cachedB === a;
68
- }
69
- cache.set(a, b);
70
- cache.set(b, a);
71
- const result = areStrictPropertiesEqual(a, b, state, properties, start);
72
- cache.delete(a);
73
- cache.delete(b);
74
- return result;
92
+ return circular
93
+ ? areCircularItemsEqual(a, b, state, areStrictPropertiesFrom, start)
94
+ : areStrictPropertiesEqual(a, b, state, properties, start);
75
95
  }
76
96
  /**
77
97
  * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
78
98
  * for circular references to be safely included in the comparison without creating
79
99
  * stack overflows.
80
100
  */
81
- function createIsCircular(areItemsEqual) {
82
- return function isCircular(a, b, state) {
83
- if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
84
- return areItemsEqual(a, b, state);
85
- }
86
- const { cache } = state;
87
- const cachedA = cache.get(a);
88
- const cachedB = cache.get(b);
89
- if (cachedA && cachedB) {
90
- return cachedA === b && cachedB === a;
91
- }
92
- cache.set(a, b);
93
- cache.set(b, a);
94
- const result = areItemsEqual(a, b, state);
95
- cache.delete(a);
96
- cache.delete(b);
97
- return result;
101
+ function createAreCircularItemsEqual(areItemsEqual) {
102
+ return function (a, b, state) {
103
+ return a && b && typeof a === 'object' && typeof b === 'object'
104
+ ? areCircularItemsEqual(a, b, state, areItemsEqual, 0)
105
+ : areItemsEqual(a, b, state);
98
106
  };
99
107
  }
108
+ /**
109
+ * Whether the own properties from `start` onward are equal with strict property checking, in the
110
+ * shape `areCircularItemsEqual` calls. The keys are read again, which only happens for objects with own
111
+ * properties beyond their intrinsic ones.
112
+ */
113
+ function areStrictPropertiesFrom(a, b, state, start) {
114
+ return areStrictPropertiesEqual(a, b, state, getOwnKeys(a), start);
115
+ }
100
116
  /**
101
117
  * Get the properties to strictly examine, which include both own properties that are
102
118
  * not enumerable and symbol properties.
@@ -189,6 +205,23 @@ const MAX_SCANNED_PROPERTIES = 64;
189
205
  const { getOwnPropertyDescriptor, keys } = Object;
190
206
  const { propertyIsEnumerable } = Object.prototype;
191
207
  const { valueOf: stringValueOf } = String.prototype;
208
+ /**
209
+ * Create a strict comparator for types whose own properties are all compared after the value
210
+ * (`ArrayBuffer`, `DataView`, `Date`, `URL`, `URLSearchParams`), from the comparator for the value.
211
+ *
212
+ * @note
213
+ * Each type gets two comparators from this: the standard one (`circular` is `false`), which is also
214
+ * the default handed to `createCustomConfig`, and one that tracks circular references in the own
215
+ * properties (`circular` is `true`), used in its place in strict circular mode when the default has
216
+ * not been replaced. The circular ones must never be handed to `createCustomConfig`: a replacement is
217
+ * wrapped with `createIsCircular`, so if it delegated to one, the pair would already be tracked and
218
+ * the own properties would not be compared.
219
+ */
220
+ function createStrictComparatorDefault(areItemsEqual, circular) {
221
+ return function (a, b, state) {
222
+ return areItemsEqual(a, b, state) && areOwnPropertiesEqualStrict(a, b, state, 0, 0, circular);
223
+ };
224
+ }
192
225
  /**
193
226
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
194
227
  * Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
@@ -232,9 +265,11 @@ function areArrayBuffersEqual(a, b) {
232
265
  /**
233
266
  * Whether the array buffers are equal in value with strict property checking.
234
267
  */
235
- function areArrayBuffersEqualStrict(a, b, state) {
236
- return areArrayBuffersEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
237
- }
268
+ const areArrayBuffersEqualStrict = createStrictComparatorDefault(areArrayBuffersEqual, false);
269
+ /**
270
+ * Same as `areArrayBuffersEqualStrict`, tracking circular references in its own properties.
271
+ */
272
+ const areArrayBuffersEqualStrictCircular = createStrictComparatorDefault(areArrayBuffersEqual, true);
238
273
  /**
239
274
  * Whether the arrays are equal in value.
240
275
  */
@@ -260,9 +295,11 @@ function areDataViewsEqual(a, b) {
260
295
  /**
261
296
  * Whether the dataviews are equal in value with strict property checking.
262
297
  */
263
- function areDataViewsEqualStrict(a, b, state) {
264
- return areDataViewsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
265
- }
298
+ const areDataViewsEqualStrict = createStrictComparatorDefault(areDataViewsEqual, false);
299
+ /**
300
+ * Same as `areDataViewsEqualStrict`, tracking circular references in its own properties.
301
+ */
302
+ const areDataViewsEqualStrictCircular = createStrictComparatorDefault(areDataViewsEqual, true);
266
303
  /**
267
304
  * Whether the dates passed are equal in value.
268
305
  */
@@ -272,9 +309,11 @@ function areDatesEqual(a, b) {
272
309
  /**
273
310
  * Whether the dates passed are equal in value with strict property checking.
274
311
  */
275
- function areDatesEqualStrict(a, b, state) {
276
- return areDatesEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
277
- }
312
+ const areDatesEqualStrict = createStrictComparatorDefault(areDatesEqual, false);
313
+ /**
314
+ * Same as `areDatesEqualStrict`, tracking circular references in its own properties.
315
+ */
316
+ const areDatesEqualStrictCircular = createStrictComparatorDefault(areDatesEqual, true);
278
317
  /**
279
318
  * Whether the errors passed are equal in value.
280
319
  *
@@ -449,6 +488,22 @@ function areObjectsEqualStrict(a, b, state) {
449
488
  function arePrimitiveWrappersEqual(a, b) {
450
489
  return sameValueEqual(a.valueOf(), b.valueOf());
451
490
  }
491
+ /**
492
+ * Create the strict comparator for primitive wrappers, with or without circular tracking of the own
493
+ * properties. See `createStrictComparatorDefault` for how the two are used.
494
+ */
495
+ function createStrictComparatorPrimitiveWrappers(circular) {
496
+ return function (a, b, state) {
497
+ const value = a.valueOf();
498
+ if (!sameValueEqual(value, b.valueOf())) {
499
+ return false;
500
+ }
501
+ // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
502
+ return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
503
+ ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1, circular)
504
+ : areOwnPropertiesEqualStrict(a, b, state, 0, 0, circular);
505
+ };
506
+ }
452
507
  /**
453
508
  * Whether the primitive wrappers passed are equal in value with strict property checking.
454
509
  *
@@ -458,22 +513,40 @@ function arePrimitiveWrappersEqual(a, b) {
458
513
  * value comes from the native `valueOf`. Otherwise (overridden or cross-realm `valueOf`) the value
459
514
  * may not reflect the characters, so they are compared as properties.
460
515
  */
461
- function arePrimitiveWrappersEqualStrict(a, b, state) {
462
- const value = a.valueOf();
463
- if (!sameValueEqual(value, b.valueOf())) {
464
- return false;
465
- }
466
- // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
467
- return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
468
- ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1)
469
- : areOwnPropertiesEqualStrict(a, b, state, 0, 0);
470
- }
516
+ const arePrimitiveWrappersEqualStrict = createStrictComparatorPrimitiveWrappers(false);
517
+ /**
518
+ * Same as `arePrimitiveWrappersEqualStrict`, tracking circular references in its own properties.
519
+ */
520
+ const arePrimitiveWrappersEqualStrictCircular = createStrictComparatorPrimitiveWrappers(true);
471
521
  /**
472
522
  * Whether the regexps passed are equal in value.
473
523
  */
474
524
  function areRegExpsEqual(a, b) {
475
525
  return a.source === b.source && a.flags === b.flags;
476
526
  }
527
+ /**
528
+ * Create the strict comparator for regexps, with or without circular tracking of the own properties.
529
+ * See `createStrictComparatorDefault` for how the two are used.
530
+ */
531
+ function createStrictComparatorRegExp(circular) {
532
+ return function (a, b, state) {
533
+ if (!areRegExpsEqual(a, b)) {
534
+ return false;
535
+ }
536
+ // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
537
+ // it, in which case there is nothing to shortcut and every own property is compared.
538
+ const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
539
+ const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
540
+ return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
541
+ && descriptorB
542
+ && a.lastIndex === b.lastIndex
543
+ && descriptorA.configurable === descriptorB.configurable
544
+ && descriptorA.enumerable === descriptorB.enumerable
545
+ && descriptorA.writable === descriptorB.writable
546
+ ? 1
547
+ : 0, circular);
548
+ };
549
+ }
477
550
  /**
478
551
  * Whether the regexps passed are equal in value with strict property checking.
479
552
  *
@@ -483,23 +556,11 @@ function areRegExpsEqual(a, b) {
483
556
  * writability, or there are other own properties, every own property is compared (including
484
557
  * `lastIndex`, since index properties may be listed before it).
485
558
  */
486
- function areRegExpsEqualStrict(a, b, state) {
487
- if (!areRegExpsEqual(a, b)) {
488
- return false;
489
- }
490
- // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
491
- // it, in which case there is nothing to shortcut and every own property is compared.
492
- const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
493
- const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
494
- return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
495
- && descriptorB
496
- && a.lastIndex === b.lastIndex
497
- && descriptorA.configurable === descriptorB.configurable
498
- && descriptorA.enumerable === descriptorB.enumerable
499
- && descriptorA.writable === descriptorB.writable
500
- ? 1
501
- : 0);
502
- }
559
+ const areRegExpsEqualStrict = createStrictComparatorRegExp(false);
560
+ /**
561
+ * Same as `areRegExpsEqualStrict`, tracking circular references in its own properties.
562
+ */
563
+ const areRegExpsEqualStrictCircular = createStrictComparatorRegExp(true);
503
564
  /**
504
565
  * Whether the search params passed are equal in value.
505
566
  *
@@ -525,9 +586,11 @@ function areUrlSearchParamsEqual(a, b) {
525
586
  /**
526
587
  * Whether the search params passed are equal in value with strict property checking.
527
588
  */
528
- function areUrlSearchParamsEqualStrict(a, b, state) {
529
- return areUrlSearchParamsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
530
- }
589
+ const areUrlSearchParamsEqualStrict = createStrictComparatorDefault(areUrlSearchParamsEqual, false);
590
+ /**
591
+ * Same as `areUrlSearchParamsEqualStrict`, tracking circular references in its own properties.
592
+ */
593
+ const areUrlSearchParamsEqualStrictCircular = createStrictComparatorDefault(areUrlSearchParamsEqual, true);
531
594
  /**
532
595
  * Whether the `Set`s are equal in value.
533
596
  */
@@ -611,6 +674,15 @@ function areTypedArraysEqual(a, b) {
611
674
  }
612
675
  return true;
613
676
  }
677
+ /**
678
+ * Create the strict comparator for TypedArrays, with or without circular tracking of the own
679
+ * properties. See `createStrictComparatorDefault` for how the two are used.
680
+ */
681
+ function createStrictComparatorTypedArray(circular) {
682
+ return function (a, b, state) {
683
+ return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length, circular);
684
+ };
685
+ }
614
686
  /**
615
687
  * Whether the TypedArray instances are equal in value with strict property checking.
616
688
  *
@@ -619,9 +691,11 @@ function areTypedArraysEqual(a, b) {
619
691
  * spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
620
692
  * (including symbols) are compared with strict property checking.
621
693
  */
622
- function areTypedArraysEqualStrict(a, b, state) {
623
- return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length);
624
- }
694
+ const areTypedArraysEqualStrict = createStrictComparatorTypedArray(false);
695
+ /**
696
+ * Same as `areTypedArraysEqualStrict`, tracking circular references in its own properties.
697
+ */
698
+ const areTypedArraysEqualStrictCircular = createStrictComparatorTypedArray(true);
625
699
  /**
626
700
  * Whether the URL instances are equal in value.
627
701
  */
@@ -667,9 +741,11 @@ function areUrlsEqual(a, b) {
667
741
  /**
668
742
  * Whether the URL instances are equal in value with strict property checking.
669
743
  */
670
- function areUrlsEqualStrict(a, b, state) {
671
- return areUrlsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
672
- }
744
+ const areUrlsEqualStrict = createStrictComparatorDefault(areUrlsEqual, false);
745
+ /**
746
+ * Same as `areUrlsEqualStrict`, tracking circular references in its own properties.
747
+ */
748
+ const areUrlsEqualStrictCircular = createStrictComparatorDefault(areUrlsEqual, true);
673
749
 
674
750
  const { toString } = Object.prototype;
675
751
  const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
@@ -805,8 +881,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
805
881
  // In strict mode, every type compares its own properties in addition to its value, because any
806
882
  // object can have properties assigned to it (`date.foo = 'bar'`). For types that hold their value
807
883
  // internally (`Date`, `RegExp`, TypedArrays, etc.), only the properties after the intrinsic ones
808
- // are compared, and circular references among them are tracked by the comparator itself (see
809
- // `areOwnPropertiesEqualStrict`).
884
+ // are compared. In circular mode, those comparators are wrapped with `createAreCircularItemsEqual` below.
810
885
  let config = {
811
886
  areArrayBuffersEqual: strict ? areArrayBuffersEqualStrict : areArrayBuffersEqual,
812
887
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -834,17 +909,51 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
834
909
  config = Object.assign({}, config, createCustomConfig(config));
835
910
  }
836
911
  if (circular) {
837
- // Only containers are wrapped, since they always compare nested values. Types that hold their
838
- // value internally only do so for own properties in strict mode, and track those themselves.
839
- // Errors are included because comparing `cause` and own properties by value means an error
840
- // that references itself would otherwise recurse without bound.
912
+ // Containers are always wrapped, since they always compare nested values. Types that hold their
913
+ // value internally only compare nested values (their own properties) in strict mode, so they are
914
+ // only wrapped then (see below). Errors are included because comparing `cause` and own properties
915
+ // by value means an error that references itself would otherwise recurse without bound.
841
916
  config = Object.assign({}, config, {
842
- areArraysEqual: createIsCircular(config.areArraysEqual),
843
- areErrorsEqual: createIsCircular(config.areErrorsEqual),
844
- areMapsEqual: createIsCircular(config.areMapsEqual),
845
- areObjectsEqual: createIsCircular(config.areObjectsEqual),
846
- areSetsEqual: createIsCircular(config.areSetsEqual),
917
+ areArraysEqual: createAreCircularItemsEqual(config.areArraysEqual),
918
+ areErrorsEqual: createAreCircularItemsEqual(config.areErrorsEqual),
919
+ areMapsEqual: createAreCircularItemsEqual(config.areMapsEqual),
920
+ areObjectsEqual: createAreCircularItemsEqual(config.areObjectsEqual),
921
+ areSetsEqual: createAreCircularItemsEqual(config.areSetsEqual),
847
922
  });
923
+ if (strict) {
924
+ // Certain objects only have a circular concern in strict mode, as that is the only time that
925
+ // their own properties are compared. These rarely have own properties beyond their intrinsic
926
+ // ones, so when the default is untouched it is swapped for a variant that only tracks the pair
927
+ // once it has some, keeping the cost of tracking off the common case. When replaced, the
928
+ // comparator is wrapped instead. The variants are never handed to `createCustomConfig`, so a
929
+ // replacement that delegates to the default it received cannot track the same pair twice.
930
+ config = Object.assign({}, config, {
931
+ areArrayBuffersEqual: config.areArrayBuffersEqual === areArrayBuffersEqualStrict
932
+ ? areArrayBuffersEqualStrictCircular
933
+ : createAreCircularItemsEqual(config.areArrayBuffersEqual),
934
+ areDataViewsEqual: config.areDataViewsEqual === areDataViewsEqualStrict
935
+ ? areDataViewsEqualStrictCircular
936
+ : createAreCircularItemsEqual(config.areDataViewsEqual),
937
+ areDatesEqual: config.areDatesEqual === areDatesEqualStrict
938
+ ? areDatesEqualStrictCircular
939
+ : createAreCircularItemsEqual(config.areDatesEqual),
940
+ arePrimitiveWrappersEqual: config.arePrimitiveWrappersEqual === arePrimitiveWrappersEqualStrict
941
+ ? arePrimitiveWrappersEqualStrictCircular
942
+ : createAreCircularItemsEqual(config.arePrimitiveWrappersEqual),
943
+ areRegExpsEqual: config.areRegExpsEqual === areRegExpsEqualStrict
944
+ ? areRegExpsEqualStrictCircular
945
+ : createAreCircularItemsEqual(config.areRegExpsEqual),
946
+ areTypedArraysEqual: config.areTypedArraysEqual === areTypedArraysEqualStrict
947
+ ? areTypedArraysEqualStrictCircular
948
+ : createAreCircularItemsEqual(config.areTypedArraysEqual),
949
+ areUrlSearchParamsEqual: config.areUrlSearchParamsEqual === areUrlSearchParamsEqualStrict
950
+ ? areUrlSearchParamsEqualStrictCircular
951
+ : createAreCircularItemsEqual(config.areUrlSearchParamsEqual),
952
+ areUrlsEqual: config.areUrlsEqual === areUrlsEqualStrict
953
+ ? areUrlsEqualStrictCircular
954
+ : createAreCircularItemsEqual(config.areUrlsEqual),
955
+ });
956
+ }
848
957
  }
849
958
  return config;
850
959
  }
package/dist/es/index.mjs CHANGED
@@ -1,5 +1,37 @@
1
1
  const { getOwnPropertyDescriptor: getOwnPropertyDescriptor$1, getOwnPropertyNames, getOwnPropertySymbols } = Object;
2
2
  const { hasOwnProperty, toString: toString$1 } = Object.prototype;
3
+ /**
4
+ * Compare the pair while it is tracked in the cache, so that reaching it again further down returns
5
+ * whether it was reached as the same pair rather than recursing. This is the single implementation
6
+ * of circular tracking, shared by `createIsCircular` and `areOwnPropertiesEqualStrict`.
7
+ */
8
+ function areCircularItemsEqual(a, b, state, areItemsEqual, start) {
9
+ const { cache } = state;
10
+ const cachedA = cache.get(a);
11
+ const cachedB = cache.get(b);
12
+ if (cachedA && cachedB) {
13
+ return cachedA === b && cachedB === a;
14
+ }
15
+ cache.set(a, b);
16
+ cache.set(b, a);
17
+ const result = areItemsEqual(a, b, state, start);
18
+ // Either object may already be paired with another further up the stack (a `Set` / `Map` trying
19
+ // candidates, or a shared reference). Restoring that pairing, rather than deleting it, keeps the
20
+ // outer comparison able to detect its own cycle once this one returns.
21
+ if (cachedA) {
22
+ cache.set(a, cachedA);
23
+ }
24
+ else {
25
+ cache.delete(a);
26
+ }
27
+ if (cachedB) {
28
+ cache.set(b, cachedB);
29
+ }
30
+ else {
31
+ cache.delete(b);
32
+ }
33
+ return result;
34
+ }
3
35
  /**
4
36
  * Whether the own properties from `end` onward are equal with strict property checking, where the
5
37
  * caller has already verified that both objects have the same number of own properties.
@@ -41,60 +73,44 @@ function areStrictPropertiesEqual(a, b, state, properties, end) {
41
73
  * and otherwise every property from `start` is compared.
42
74
  *
43
75
  * @note
44
- * These objects rarely have own properties beyond their intrinsic ones, so that case returns before
45
- * any circular tracking is done. When they do, the properties can reference the object itself, so
46
- * the pair is tracked in the cache (when present) here rather than by wrapping the comparator with
47
- * `createIsCircular`. This keeps the cost of tracking off the common case.
76
+ * When `circular` is `true`, the pair is tracked for circular references, but only once it is known
77
+ * to have own properties beyond the intrinsic ones. These objects rarely have any, so the common case
78
+ * never touches the cache. Only the default strict comparators used in strict circular mode pass
79
+ * `true`; replaced comparators are wrapped with `createIsCircular` instead.
48
80
  */
49
- function areOwnPropertiesEqualStrict(a, b, state, start, intrinsic) {
81
+ function areOwnPropertiesEqualStrict(a, b, state, start, intrinsic, circular) {
50
82
  const properties = getOwnKeys(a);
51
- const length = properties.length;
83
+ const { length } = properties;
52
84
  if (getOwnKeys(b).length !== length) {
53
85
  return false;
54
86
  }
55
87
  if (length === intrinsic) {
56
88
  return true;
57
89
  }
58
- const cache = state.cache;
59
- if (!cache) {
60
- return areStrictPropertiesEqual(a, b, state, properties, start);
61
- }
62
- const cachedA = cache.get(a);
63
- const cachedB = cache.get(b);
64
- if (cachedA && cachedB) {
65
- return cachedA === b && cachedB === a;
66
- }
67
- cache.set(a, b);
68
- cache.set(b, a);
69
- const result = areStrictPropertiesEqual(a, b, state, properties, start);
70
- cache.delete(a);
71
- cache.delete(b);
72
- return result;
90
+ return circular
91
+ ? areCircularItemsEqual(a, b, state, areStrictPropertiesFrom, start)
92
+ : areStrictPropertiesEqual(a, b, state, properties, start);
73
93
  }
74
94
  /**
75
95
  * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
76
96
  * for circular references to be safely included in the comparison without creating
77
97
  * stack overflows.
78
98
  */
79
- function createIsCircular(areItemsEqual) {
80
- return function isCircular(a, b, state) {
81
- if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
82
- return areItemsEqual(a, b, state);
83
- }
84
- const { cache } = state;
85
- const cachedA = cache.get(a);
86
- const cachedB = cache.get(b);
87
- if (cachedA && cachedB) {
88
- return cachedA === b && cachedB === a;
89
- }
90
- cache.set(a, b);
91
- cache.set(b, a);
92
- const result = areItemsEqual(a, b, state);
93
- cache.delete(a);
94
- cache.delete(b);
95
- return result;
99
+ function createAreCircularItemsEqual(areItemsEqual) {
100
+ return function (a, b, state) {
101
+ return a && b && typeof a === 'object' && typeof b === 'object'
102
+ ? areCircularItemsEqual(a, b, state, areItemsEqual, 0)
103
+ : areItemsEqual(a, b, state);
96
104
  };
97
105
  }
106
+ /**
107
+ * Whether the own properties from `start` onward are equal with strict property checking, in the
108
+ * shape `areCircularItemsEqual` calls. The keys are read again, which only happens for objects with own
109
+ * properties beyond their intrinsic ones.
110
+ */
111
+ function areStrictPropertiesFrom(a, b, state, start) {
112
+ return areStrictPropertiesEqual(a, b, state, getOwnKeys(a), start);
113
+ }
98
114
  /**
99
115
  * Get the properties to strictly examine, which include both own properties that are
100
116
  * not enumerable and symbol properties.
@@ -187,6 +203,23 @@ const MAX_SCANNED_PROPERTIES = 64;
187
203
  const { getOwnPropertyDescriptor, keys } = Object;
188
204
  const { propertyIsEnumerable } = Object.prototype;
189
205
  const { valueOf: stringValueOf } = String.prototype;
206
+ /**
207
+ * Create a strict comparator for types whose own properties are all compared after the value
208
+ * (`ArrayBuffer`, `DataView`, `Date`, `URL`, `URLSearchParams`), from the comparator for the value.
209
+ *
210
+ * @note
211
+ * Each type gets two comparators from this: the standard one (`circular` is `false`), which is also
212
+ * the default handed to `createCustomConfig`, and one that tracks circular references in the own
213
+ * properties (`circular` is `true`), used in its place in strict circular mode when the default has
214
+ * not been replaced. The circular ones must never be handed to `createCustomConfig`: a replacement is
215
+ * wrapped with `createIsCircular`, so if it delegated to one, the pair would already be tracked and
216
+ * the own properties would not be compared.
217
+ */
218
+ function createStrictComparatorDefault(areItemsEqual, circular) {
219
+ return function (a, b, state) {
220
+ return areItemsEqual(a, b, state) && areOwnPropertiesEqualStrict(a, b, state, 0, 0, circular);
221
+ };
222
+ }
190
223
  /**
191
224
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
192
225
  * Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
@@ -230,9 +263,11 @@ function areArrayBuffersEqual(a, b) {
230
263
  /**
231
264
  * Whether the array buffers are equal in value with strict property checking.
232
265
  */
233
- function areArrayBuffersEqualStrict(a, b, state) {
234
- return areArrayBuffersEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
235
- }
266
+ const areArrayBuffersEqualStrict = createStrictComparatorDefault(areArrayBuffersEqual, false);
267
+ /**
268
+ * Same as `areArrayBuffersEqualStrict`, tracking circular references in its own properties.
269
+ */
270
+ const areArrayBuffersEqualStrictCircular = createStrictComparatorDefault(areArrayBuffersEqual, true);
236
271
  /**
237
272
  * Whether the arrays are equal in value.
238
273
  */
@@ -258,9 +293,11 @@ function areDataViewsEqual(a, b) {
258
293
  /**
259
294
  * Whether the dataviews are equal in value with strict property checking.
260
295
  */
261
- function areDataViewsEqualStrict(a, b, state) {
262
- return areDataViewsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
263
- }
296
+ const areDataViewsEqualStrict = createStrictComparatorDefault(areDataViewsEqual, false);
297
+ /**
298
+ * Same as `areDataViewsEqualStrict`, tracking circular references in its own properties.
299
+ */
300
+ const areDataViewsEqualStrictCircular = createStrictComparatorDefault(areDataViewsEqual, true);
264
301
  /**
265
302
  * Whether the dates passed are equal in value.
266
303
  */
@@ -270,9 +307,11 @@ function areDatesEqual(a, b) {
270
307
  /**
271
308
  * Whether the dates passed are equal in value with strict property checking.
272
309
  */
273
- function areDatesEqualStrict(a, b, state) {
274
- return areDatesEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
275
- }
310
+ const areDatesEqualStrict = createStrictComparatorDefault(areDatesEqual, false);
311
+ /**
312
+ * Same as `areDatesEqualStrict`, tracking circular references in its own properties.
313
+ */
314
+ const areDatesEqualStrictCircular = createStrictComparatorDefault(areDatesEqual, true);
276
315
  /**
277
316
  * Whether the errors passed are equal in value.
278
317
  *
@@ -447,6 +486,22 @@ function areObjectsEqualStrict(a, b, state) {
447
486
  function arePrimitiveWrappersEqual(a, b) {
448
487
  return sameValueEqual(a.valueOf(), b.valueOf());
449
488
  }
489
+ /**
490
+ * Create the strict comparator for primitive wrappers, with or without circular tracking of the own
491
+ * properties. See `createStrictComparatorDefault` for how the two are used.
492
+ */
493
+ function createStrictComparatorPrimitiveWrappers(circular) {
494
+ return function (a, b, state) {
495
+ const value = a.valueOf();
496
+ if (!sameValueEqual(value, b.valueOf())) {
497
+ return false;
498
+ }
499
+ // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
500
+ return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
501
+ ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1, circular)
502
+ : areOwnPropertiesEqualStrict(a, b, state, 0, 0, circular);
503
+ };
504
+ }
450
505
  /**
451
506
  * Whether the primitive wrappers passed are equal in value with strict property checking.
452
507
  *
@@ -456,22 +511,40 @@ function arePrimitiveWrappersEqual(a, b) {
456
511
  * value comes from the native `valueOf`. Otherwise (overridden or cross-realm `valueOf`) the value
457
512
  * may not reflect the characters, so they are compared as properties.
458
513
  */
459
- function arePrimitiveWrappersEqualStrict(a, b, state) {
460
- const value = a.valueOf();
461
- if (!sameValueEqual(value, b.valueOf())) {
462
- return false;
463
- }
464
- // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
465
- return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
466
- ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1)
467
- : areOwnPropertiesEqualStrict(a, b, state, 0, 0);
468
- }
514
+ const arePrimitiveWrappersEqualStrict = createStrictComparatorPrimitiveWrappers(false);
515
+ /**
516
+ * Same as `arePrimitiveWrappersEqualStrict`, tracking circular references in its own properties.
517
+ */
518
+ const arePrimitiveWrappersEqualStrictCircular = createStrictComparatorPrimitiveWrappers(true);
469
519
  /**
470
520
  * Whether the regexps passed are equal in value.
471
521
  */
472
522
  function areRegExpsEqual(a, b) {
473
523
  return a.source === b.source && a.flags === b.flags;
474
524
  }
525
+ /**
526
+ * Create the strict comparator for regexps, with or without circular tracking of the own properties.
527
+ * See `createStrictComparatorDefault` for how the two are used.
528
+ */
529
+ function createStrictComparatorRegExp(circular) {
530
+ return function (a, b, state) {
531
+ if (!areRegExpsEqual(a, b)) {
532
+ return false;
533
+ }
534
+ // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
535
+ // it, in which case there is nothing to shortcut and every own property is compared.
536
+ const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
537
+ const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
538
+ return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
539
+ && descriptorB
540
+ && a.lastIndex === b.lastIndex
541
+ && descriptorA.configurable === descriptorB.configurable
542
+ && descriptorA.enumerable === descriptorB.enumerable
543
+ && descriptorA.writable === descriptorB.writable
544
+ ? 1
545
+ : 0, circular);
546
+ };
547
+ }
475
548
  /**
476
549
  * Whether the regexps passed are equal in value with strict property checking.
477
550
  *
@@ -481,23 +554,11 @@ function areRegExpsEqual(a, b) {
481
554
  * writability, or there are other own properties, every own property is compared (including
482
555
  * `lastIndex`, since index properties may be listed before it).
483
556
  */
484
- function areRegExpsEqualStrict(a, b, state) {
485
- if (!areRegExpsEqual(a, b)) {
486
- return false;
487
- }
488
- // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
489
- // it, in which case there is nothing to shortcut and every own property is compared.
490
- const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
491
- const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
492
- return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
493
- && descriptorB
494
- && a.lastIndex === b.lastIndex
495
- && descriptorA.configurable === descriptorB.configurable
496
- && descriptorA.enumerable === descriptorB.enumerable
497
- && descriptorA.writable === descriptorB.writable
498
- ? 1
499
- : 0);
500
- }
557
+ const areRegExpsEqualStrict = createStrictComparatorRegExp(false);
558
+ /**
559
+ * Same as `areRegExpsEqualStrict`, tracking circular references in its own properties.
560
+ */
561
+ const areRegExpsEqualStrictCircular = createStrictComparatorRegExp(true);
501
562
  /**
502
563
  * Whether the search params passed are equal in value.
503
564
  *
@@ -523,9 +584,11 @@ function areUrlSearchParamsEqual(a, b) {
523
584
  /**
524
585
  * Whether the search params passed are equal in value with strict property checking.
525
586
  */
526
- function areUrlSearchParamsEqualStrict(a, b, state) {
527
- return areUrlSearchParamsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
528
- }
587
+ const areUrlSearchParamsEqualStrict = createStrictComparatorDefault(areUrlSearchParamsEqual, false);
588
+ /**
589
+ * Same as `areUrlSearchParamsEqualStrict`, tracking circular references in its own properties.
590
+ */
591
+ const areUrlSearchParamsEqualStrictCircular = createStrictComparatorDefault(areUrlSearchParamsEqual, true);
529
592
  /**
530
593
  * Whether the `Set`s are equal in value.
531
594
  */
@@ -609,6 +672,15 @@ function areTypedArraysEqual(a, b) {
609
672
  }
610
673
  return true;
611
674
  }
675
+ /**
676
+ * Create the strict comparator for TypedArrays, with or without circular tracking of the own
677
+ * properties. See `createStrictComparatorDefault` for how the two are used.
678
+ */
679
+ function createStrictComparatorTypedArray(circular) {
680
+ return function (a, b, state) {
681
+ return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length, circular);
682
+ };
683
+ }
612
684
  /**
613
685
  * Whether the TypedArray instances are equal in value with strict property checking.
614
686
  *
@@ -617,9 +689,11 @@ function areTypedArraysEqual(a, b) {
617
689
  * spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
618
690
  * (including symbols) are compared with strict property checking.
619
691
  */
620
- function areTypedArraysEqualStrict(a, b, state) {
621
- return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length);
622
- }
692
+ const areTypedArraysEqualStrict = createStrictComparatorTypedArray(false);
693
+ /**
694
+ * Same as `areTypedArraysEqualStrict`, tracking circular references in its own properties.
695
+ */
696
+ const areTypedArraysEqualStrictCircular = createStrictComparatorTypedArray(true);
623
697
  /**
624
698
  * Whether the URL instances are equal in value.
625
699
  */
@@ -665,9 +739,11 @@ function areUrlsEqual(a, b) {
665
739
  /**
666
740
  * Whether the URL instances are equal in value with strict property checking.
667
741
  */
668
- function areUrlsEqualStrict(a, b, state) {
669
- return areUrlsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
670
- }
742
+ const areUrlsEqualStrict = createStrictComparatorDefault(areUrlsEqual, false);
743
+ /**
744
+ * Same as `areUrlsEqualStrict`, tracking circular references in its own properties.
745
+ */
746
+ const areUrlsEqualStrictCircular = createStrictComparatorDefault(areUrlsEqual, true);
671
747
 
672
748
  const { toString } = Object.prototype;
673
749
  const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
@@ -803,8 +879,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
803
879
  // In strict mode, every type compares its own properties in addition to its value, because any
804
880
  // object can have properties assigned to it (`date.foo = 'bar'`). For types that hold their value
805
881
  // internally (`Date`, `RegExp`, TypedArrays, etc.), only the properties after the intrinsic ones
806
- // are compared, and circular references among them are tracked by the comparator itself (see
807
- // `areOwnPropertiesEqualStrict`).
882
+ // are compared. In circular mode, those comparators are wrapped with `createAreCircularItemsEqual` below.
808
883
  let config = {
809
884
  areArrayBuffersEqual: strict ? areArrayBuffersEqualStrict : areArrayBuffersEqual,
810
885
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -832,17 +907,51 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
832
907
  config = Object.assign({}, config, createCustomConfig(config));
833
908
  }
834
909
  if (circular) {
835
- // Only containers are wrapped, since they always compare nested values. Types that hold their
836
- // value internally only do so for own properties in strict mode, and track those themselves.
837
- // Errors are included because comparing `cause` and own properties by value means an error
838
- // that references itself would otherwise recurse without bound.
910
+ // Containers are always wrapped, since they always compare nested values. Types that hold their
911
+ // value internally only compare nested values (their own properties) in strict mode, so they are
912
+ // only wrapped then (see below). Errors are included because comparing `cause` and own properties
913
+ // by value means an error that references itself would otherwise recurse without bound.
839
914
  config = Object.assign({}, config, {
840
- areArraysEqual: createIsCircular(config.areArraysEqual),
841
- areErrorsEqual: createIsCircular(config.areErrorsEqual),
842
- areMapsEqual: createIsCircular(config.areMapsEqual),
843
- areObjectsEqual: createIsCircular(config.areObjectsEqual),
844
- areSetsEqual: createIsCircular(config.areSetsEqual),
915
+ areArraysEqual: createAreCircularItemsEqual(config.areArraysEqual),
916
+ areErrorsEqual: createAreCircularItemsEqual(config.areErrorsEqual),
917
+ areMapsEqual: createAreCircularItemsEqual(config.areMapsEqual),
918
+ areObjectsEqual: createAreCircularItemsEqual(config.areObjectsEqual),
919
+ areSetsEqual: createAreCircularItemsEqual(config.areSetsEqual),
845
920
  });
921
+ if (strict) {
922
+ // Certain objects only have a circular concern in strict mode, as that is the only time that
923
+ // their own properties are compared. These rarely have own properties beyond their intrinsic
924
+ // ones, so when the default is untouched it is swapped for a variant that only tracks the pair
925
+ // once it has some, keeping the cost of tracking off the common case. When replaced, the
926
+ // comparator is wrapped instead. The variants are never handed to `createCustomConfig`, so a
927
+ // replacement that delegates to the default it received cannot track the same pair twice.
928
+ config = Object.assign({}, config, {
929
+ areArrayBuffersEqual: config.areArrayBuffersEqual === areArrayBuffersEqualStrict
930
+ ? areArrayBuffersEqualStrictCircular
931
+ : createAreCircularItemsEqual(config.areArrayBuffersEqual),
932
+ areDataViewsEqual: config.areDataViewsEqual === areDataViewsEqualStrict
933
+ ? areDataViewsEqualStrictCircular
934
+ : createAreCircularItemsEqual(config.areDataViewsEqual),
935
+ areDatesEqual: config.areDatesEqual === areDatesEqualStrict
936
+ ? areDatesEqualStrictCircular
937
+ : createAreCircularItemsEqual(config.areDatesEqual),
938
+ arePrimitiveWrappersEqual: config.arePrimitiveWrappersEqual === arePrimitiveWrappersEqualStrict
939
+ ? arePrimitiveWrappersEqualStrictCircular
940
+ : createAreCircularItemsEqual(config.arePrimitiveWrappersEqual),
941
+ areRegExpsEqual: config.areRegExpsEqual === areRegExpsEqualStrict
942
+ ? areRegExpsEqualStrictCircular
943
+ : createAreCircularItemsEqual(config.areRegExpsEqual),
944
+ areTypedArraysEqual: config.areTypedArraysEqual === areTypedArraysEqualStrict
945
+ ? areTypedArraysEqualStrictCircular
946
+ : createAreCircularItemsEqual(config.areTypedArraysEqual),
947
+ areUrlSearchParamsEqual: config.areUrlSearchParamsEqual === areUrlSearchParamsEqualStrict
948
+ ? areUrlSearchParamsEqualStrictCircular
949
+ : createAreCircularItemsEqual(config.areUrlSearchParamsEqual),
950
+ areUrlsEqual: config.areUrlsEqual === areUrlsEqualStrict
951
+ ? areUrlsEqualStrictCircular
952
+ : createAreCircularItemsEqual(config.areUrlsEqual),
953
+ });
954
+ }
846
955
  }
847
956
  return config;
848
957
  }
package/package.json CHANGED
@@ -111,5 +111,5 @@
111
111
  "sideEffects": false,
112
112
  "type": "module",
113
113
  "types": "./index.d.ts",
114
- "version": "6.4.2"
114
+ "version": "6.4.3"
115
115
  }