fast-equals 6.4.1 → 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 ~2.90kB 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
 
@@ -36,6 +36,7 @@ If you are having any problems, want to request a new feature, or have any quest
36
36
  - [Available methods](#available-methods)
37
37
  - [deepEqual](#deepequal)
38
38
  - [Comparing `Map`s](#comparing-maps)
39
+ - [Comparing `URL`s and `URLSearchParams`](#comparing-urls-and-urlsearchparams)
39
40
  - [shallowEqual](#shallowequal)
40
41
  - [sameValueEqual](#samevalueequal)
41
42
  - [sameValueZeroEqual](#samevaluezeroequal)
@@ -93,6 +94,20 @@ deepEqual(mapA, mapB);
93
94
  To support true deep equality of all contents, `fast-equals` will perform a deep equality comparison for key and value
94
95
  pairs. Therefore, the above would be `true`.
95
96
 
97
+ #### Comparing `URL`s and `URLSearchParams`
98
+
99
+ `URL` objects are equal when their `href` matches, as in the
100
+ [URL Standard](https://url.spec.whatwg.org/#url-equivalence), except that query pair order is not significant (repeated
101
+ keys are counted). Everything else is compared as written:
102
+
103
+ ```ts
104
+ deepEqual(new URL('https://foo.com/?a=1&b=2'), new URL('https://foo.com/?b=2&a=1')); // true
105
+ deepEqual(new URL('https://foo.com/?a=%41'), new URL('https://foo.com/?a=A')); // false
106
+ ```
107
+
108
+ `URLSearchParams` objects follow the same rule, but they only keep the decoded pairs, so the `searchParams` of the two
109
+ URLs above are equal. Equal `URL`s always have equal `searchParams`, but not the reverse.
110
+
96
111
  ### shallowEqual
97
112
 
98
113
  Performs a shallow equality comparison on the two objects passed and returns a boolean representing the value
@@ -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.
@@ -153,15 +169,32 @@ function isReactProperty(a, b, property) {
153
169
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
154
170
  }
155
171
  /**
156
- * Reorder a serialized query string so that params holding the same pairs compare as equal
157
- * regardless of the order they appear in.
172
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
158
173
  *
159
174
  * @note
160
- * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
161
- * splitting on `&` recovers exactly the pairs and nothing else.
175
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
176
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
177
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
178
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
179
+ *
180
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
162
181
  */
163
- function sortSearchParams(serialized) {
164
- return serialized.split('&').sort().join('&');
182
+ function areSortedPairsEqual(serializedA, serializedB) {
183
+ const pairsA = serializedA.split('&');
184
+ const pairsB = serializedB.split('&');
185
+ const length = pairsA.length;
186
+ if (length !== pairsB.length) {
187
+ return false;
188
+ }
189
+ pairsA.sort();
190
+ pairsB.sort();
191
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
192
+ for (let index = 0; index < length; ++index) {
193
+ if (pairsA[index] !== pairsB[index]) {
194
+ return false;
195
+ }
196
+ }
197
+ return true;
165
198
  }
166
199
 
167
200
  /**
@@ -172,6 +205,23 @@ const MAX_SCANNED_PROPERTIES = 64;
172
205
  const { getOwnPropertyDescriptor, keys } = Object;
173
206
  const { propertyIsEnumerable } = Object.prototype;
174
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
+ }
175
225
  /**
176
226
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
177
227
  * Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
@@ -215,9 +265,11 @@ function areArrayBuffersEqual(a, b) {
215
265
  /**
216
266
  * Whether the array buffers are equal in value with strict property checking.
217
267
  */
218
- function areArrayBuffersEqualStrict(a, b, state) {
219
- return areArrayBuffersEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
220
- }
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);
221
273
  /**
222
274
  * Whether the arrays are equal in value.
223
275
  */
@@ -243,9 +295,11 @@ function areDataViewsEqual(a, b) {
243
295
  /**
244
296
  * Whether the dataviews are equal in value with strict property checking.
245
297
  */
246
- function areDataViewsEqualStrict(a, b, state) {
247
- return areDataViewsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
248
- }
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);
249
303
  /**
250
304
  * Whether the dates passed are equal in value.
251
305
  */
@@ -255,9 +309,11 @@ function areDatesEqual(a, b) {
255
309
  /**
256
310
  * Whether the dates passed are equal in value with strict property checking.
257
311
  */
258
- function areDatesEqualStrict(a, b, state) {
259
- return areDatesEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
260
- }
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);
261
317
  /**
262
318
  * Whether the errors passed are equal in value.
263
319
  *
@@ -432,6 +488,22 @@ function areObjectsEqualStrict(a, b, state) {
432
488
  function arePrimitiveWrappersEqual(a, b) {
433
489
  return sameValueEqual(a.valueOf(), b.valueOf());
434
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
+ }
435
507
  /**
436
508
  * Whether the primitive wrappers passed are equal in value with strict property checking.
437
509
  *
@@ -441,22 +513,40 @@ function arePrimitiveWrappersEqual(a, b) {
441
513
  * value comes from the native `valueOf`. Otherwise (overridden or cross-realm `valueOf`) the value
442
514
  * may not reflect the characters, so they are compared as properties.
443
515
  */
444
- function arePrimitiveWrappersEqualStrict(a, b, state) {
445
- const value = a.valueOf();
446
- if (!sameValueEqual(value, b.valueOf())) {
447
- return false;
448
- }
449
- // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
450
- return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
451
- ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1)
452
- : areOwnPropertiesEqualStrict(a, b, state, 0, 0);
453
- }
516
+ const arePrimitiveWrappersEqualStrict = createStrictComparatorPrimitiveWrappers(false);
517
+ /**
518
+ * Same as `arePrimitiveWrappersEqualStrict`, tracking circular references in its own properties.
519
+ */
520
+ const arePrimitiveWrappersEqualStrictCircular = createStrictComparatorPrimitiveWrappers(true);
454
521
  /**
455
522
  * Whether the regexps passed are equal in value.
456
523
  */
457
524
  function areRegExpsEqual(a, b) {
458
525
  return a.source === b.source && a.flags === b.flags;
459
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
+ }
460
550
  /**
461
551
  * Whether the regexps passed are equal in value with strict property checking.
462
552
  *
@@ -466,23 +556,11 @@ function areRegExpsEqual(a, b) {
466
556
  * writability, or there are other own properties, every own property is compared (including
467
557
  * `lastIndex`, since index properties may be listed before it).
468
558
  */
469
- function areRegExpsEqualStrict(a, b, state) {
470
- if (!areRegExpsEqual(a, b)) {
471
- return false;
472
- }
473
- // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
474
- // it, in which case there is nothing to shortcut and every own property is compared.
475
- const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
476
- const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
477
- return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
478
- && descriptorB
479
- && a.lastIndex === b.lastIndex
480
- && descriptorA.configurable === descriptorB.configurable
481
- && descriptorA.enumerable === descriptorB.enumerable
482
- && descriptorA.writable === descriptorB.writable
483
- ? 1
484
- : 0);
485
- }
559
+ const areRegExpsEqualStrict = createStrictComparatorRegExp(false);
560
+ /**
561
+ * Same as `areRegExpsEqualStrict`, tracking circular references in its own properties.
562
+ */
563
+ const areRegExpsEqualStrictCircular = createStrictComparatorRegExp(true);
486
564
  /**
487
565
  * Whether the search params passed are equal in value.
488
566
  *
@@ -490,20 +568,29 @@ function areRegExpsEqualStrict(a, b, state) {
490
568
  * Order is not significant, matching how the other unordered collections in the library are
491
569
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
492
570
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
571
+ *
572
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
573
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
574
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
575
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
493
576
  */
494
577
  function areUrlSearchParamsEqual(a, b) {
495
578
  const serializedA = a.toString();
496
579
  const serializedB = b.toString();
497
580
  // Identical serializations are equal under any ordering, and this is by far the common case, so
498
- // it is worth checking before sorting anything.
499
- return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
581
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
582
+ // mismatch rules out equality without sorting either.
583
+ return (serializedA === serializedB
584
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
500
585
  }
501
586
  /**
502
587
  * Whether the search params passed are equal in value with strict property checking.
503
588
  */
504
- function areUrlSearchParamsEqualStrict(a, b, state) {
505
- return areUrlSearchParamsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
506
- }
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);
507
594
  /**
508
595
  * Whether the `Set`s are equal in value.
509
596
  */
@@ -587,6 +674,15 @@ function areTypedArraysEqual(a, b) {
587
674
  }
588
675
  return true;
589
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
+ }
590
686
  /**
591
687
  * Whether the TypedArray instances are equal in value with strict property checking.
592
688
  *
@@ -595,33 +691,61 @@ function areTypedArraysEqual(a, b) {
595
691
  * spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
596
692
  * (including symbols) are compared with strict property checking.
597
693
  */
598
- function areTypedArraysEqualStrict(a, b, state) {
599
- return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length);
600
- }
694
+ const areTypedArraysEqualStrict = createStrictComparatorTypedArray(false);
695
+ /**
696
+ * Same as `areTypedArraysEqualStrict`, tracking circular references in its own properties.
697
+ */
698
+ const areTypedArraysEqualStrictCircular = createStrictComparatorTypedArray(true);
601
699
  /**
602
700
  * Whether the URL instances are equal in value.
603
701
  */
604
702
  function areUrlsEqual(a, b) {
703
+ const hrefA = a.href;
704
+ const hrefB = b.href;
605
705
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
606
706
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
607
- if (a.href === b.href) {
707
+ if (hrefA === hrefB) {
608
708
  return true;
609
709
  }
610
- return (a.protocol === b.protocol
611
- && a.username === b.username
612
- && a.password === b.password
613
- // `host` covers both the hostname and the port.
614
- && a.host === b.host
615
- && a.pathname === b.pathname
616
- && a.hash === b.hash
617
- && areUrlSearchParamsEqual(a.searchParams, b.searchParams));
710
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
711
+ const length = hrefA.length;
712
+ if (length !== hrefB.length) {
713
+ return false;
714
+ }
715
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
716
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
717
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
718
+ // query, or the fragment when there is no query.
719
+ const start = hrefA.indexOf('?');
720
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
721
+ return false;
722
+ }
723
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
724
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
725
+ // `#` is in both hrefs.
726
+ let end = hrefA.indexOf('#');
727
+ if (end === -1) {
728
+ if (hrefB.includes('#', start)) {
729
+ return false;
730
+ }
731
+ end = length;
732
+ }
733
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
734
+ return false;
735
+ }
736
+ const queryA = hrefA.slice(start + 1, end);
737
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
738
+ // with a single pair cannot be reordered.
739
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
618
740
  }
619
741
  /**
620
742
  * Whether the URL instances are equal in value with strict property checking.
621
743
  */
622
- function areUrlsEqualStrict(a, b, state) {
623
- return areUrlsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
624
- }
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);
625
749
 
626
750
  const { toString } = Object.prototype;
627
751
  const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
@@ -757,8 +881,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
757
881
  // In strict mode, every type compares its own properties in addition to its value, because any
758
882
  // object can have properties assigned to it (`date.foo = 'bar'`). For types that hold their value
759
883
  // internally (`Date`, `RegExp`, TypedArrays, etc.), only the properties after the intrinsic ones
760
- // are compared, and circular references among them are tracked by the comparator itself (see
761
- // `areOwnPropertiesEqualStrict`).
884
+ // are compared. In circular mode, those comparators are wrapped with `createAreCircularItemsEqual` below.
762
885
  let config = {
763
886
  areArrayBuffersEqual: strict ? areArrayBuffersEqualStrict : areArrayBuffersEqual,
764
887
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -786,17 +909,51 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
786
909
  config = Object.assign({}, config, createCustomConfig(config));
787
910
  }
788
911
  if (circular) {
789
- // Only containers are wrapped, since they always compare nested values. Types that hold their
790
- // value internally only do so for own properties in strict mode, and track those themselves.
791
- // Errors are included because comparing `cause` and own properties by value means an error
792
- // 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.
793
916
  config = Object.assign({}, config, {
794
- areArraysEqual: createIsCircular(config.areArraysEqual),
795
- areErrorsEqual: createIsCircular(config.areErrorsEqual),
796
- areMapsEqual: createIsCircular(config.areMapsEqual),
797
- areObjectsEqual: createIsCircular(config.areObjectsEqual),
798
- 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),
799
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
+ }
800
957
  }
801
958
  return config;
802
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.
@@ -151,15 +167,32 @@ function isReactProperty(a, b, property) {
151
167
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
152
168
  }
153
169
  /**
154
- * Reorder a serialized query string so that params holding the same pairs compare as equal
155
- * regardless of the order they appear in.
170
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
156
171
  *
157
172
  * @note
158
- * The serializer percent-encodes `&` and `=` wherever they appear inside a name or a value, so
159
- * splitting on `&` recovers exactly the pairs and nothing else.
173
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
174
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
175
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
176
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
177
+ *
178
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
160
179
  */
161
- function sortSearchParams(serialized) {
162
- return serialized.split('&').sort().join('&');
180
+ function areSortedPairsEqual(serializedA, serializedB) {
181
+ const pairsA = serializedA.split('&');
182
+ const pairsB = serializedB.split('&');
183
+ const length = pairsA.length;
184
+ if (length !== pairsB.length) {
185
+ return false;
186
+ }
187
+ pairsA.sort();
188
+ pairsB.sort();
189
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
190
+ for (let index = 0; index < length; ++index) {
191
+ if (pairsA[index] !== pairsB[index]) {
192
+ return false;
193
+ }
194
+ }
195
+ return true;
163
196
  }
164
197
 
165
198
  /**
@@ -170,6 +203,23 @@ const MAX_SCANNED_PROPERTIES = 64;
170
203
  const { getOwnPropertyDescriptor, keys } = Object;
171
204
  const { propertyIsEnumerable } = Object.prototype;
172
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
+ }
173
223
  /**
174
224
  * Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
175
225
  * Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
@@ -213,9 +263,11 @@ function areArrayBuffersEqual(a, b) {
213
263
  /**
214
264
  * Whether the array buffers are equal in value with strict property checking.
215
265
  */
216
- function areArrayBuffersEqualStrict(a, b, state) {
217
- return areArrayBuffersEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
218
- }
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);
219
271
  /**
220
272
  * Whether the arrays are equal in value.
221
273
  */
@@ -241,9 +293,11 @@ function areDataViewsEqual(a, b) {
241
293
  /**
242
294
  * Whether the dataviews are equal in value with strict property checking.
243
295
  */
244
- function areDataViewsEqualStrict(a, b, state) {
245
- return areDataViewsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
246
- }
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);
247
301
  /**
248
302
  * Whether the dates passed are equal in value.
249
303
  */
@@ -253,9 +307,11 @@ function areDatesEqual(a, b) {
253
307
  /**
254
308
  * Whether the dates passed are equal in value with strict property checking.
255
309
  */
256
- function areDatesEqualStrict(a, b, state) {
257
- return areDatesEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
258
- }
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);
259
315
  /**
260
316
  * Whether the errors passed are equal in value.
261
317
  *
@@ -430,6 +486,22 @@ function areObjectsEqualStrict(a, b, state) {
430
486
  function arePrimitiveWrappersEqual(a, b) {
431
487
  return sameValueEqual(a.valueOf(), b.valueOf());
432
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
+ }
433
505
  /**
434
506
  * Whether the primitive wrappers passed are equal in value with strict property checking.
435
507
  *
@@ -439,22 +511,40 @@ function arePrimitiveWrappersEqual(a, b) {
439
511
  * value comes from the native `valueOf`. Otherwise (overridden or cross-realm `valueOf`) the value
440
512
  * may not reflect the characters, so they are compared as properties.
441
513
  */
442
- function arePrimitiveWrappersEqualStrict(a, b, state) {
443
- const value = a.valueOf();
444
- if (!sameValueEqual(value, b.valueOf())) {
445
- return false;
446
- }
447
- // `length` follows the characters, so a native `String` wrapper without other own properties has one more.
448
- return typeof value === 'string' && a.valueOf === stringValueOf && b.valueOf === stringValueOf
449
- ? areOwnPropertiesEqualStrict(a, b, state, value.length, value.length + 1)
450
- : areOwnPropertiesEqualStrict(a, b, state, 0, 0);
451
- }
514
+ const arePrimitiveWrappersEqualStrict = createStrictComparatorPrimitiveWrappers(false);
515
+ /**
516
+ * Same as `arePrimitiveWrappersEqualStrict`, tracking circular references in its own properties.
517
+ */
518
+ const arePrimitiveWrappersEqualStrictCircular = createStrictComparatorPrimitiveWrappers(true);
452
519
  /**
453
520
  * Whether the regexps passed are equal in value.
454
521
  */
455
522
  function areRegExpsEqual(a, b) {
456
523
  return a.source === b.source && a.flags === b.flags;
457
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
+ }
458
548
  /**
459
549
  * Whether the regexps passed are equal in value with strict property checking.
460
550
  *
@@ -464,23 +554,11 @@ function areRegExpsEqual(a, b) {
464
554
  * writability, or there are other own properties, every own property is compared (including
465
555
  * `lastIndex`, since index properties may be listed before it).
466
556
  */
467
- function areRegExpsEqualStrict(a, b, state) {
468
- if (!areRegExpsEqual(a, b)) {
469
- return false;
470
- }
471
- // Environments that predate (or polyfill around) `lastIndex` being an own property may not have
472
- // it, in which case there is nothing to shortcut and every own property is compared.
473
- const descriptorA = getOwnPropertyDescriptor(a, 'lastIndex');
474
- const descriptorB = getOwnPropertyDescriptor(b, 'lastIndex');
475
- return areOwnPropertiesEqualStrict(a, b, state, 0, descriptorA
476
- && descriptorB
477
- && a.lastIndex === b.lastIndex
478
- && descriptorA.configurable === descriptorB.configurable
479
- && descriptorA.enumerable === descriptorB.enumerable
480
- && descriptorA.writable === descriptorB.writable
481
- ? 1
482
- : 0);
483
- }
557
+ const areRegExpsEqualStrict = createStrictComparatorRegExp(false);
558
+ /**
559
+ * Same as `areRegExpsEqualStrict`, tracking circular references in its own properties.
560
+ */
561
+ const areRegExpsEqualStrictCircular = createStrictComparatorRegExp(true);
484
562
  /**
485
563
  * Whether the search params passed are equal in value.
486
564
  *
@@ -488,20 +566,29 @@ function areRegExpsEqualStrict(a, b, state) {
488
566
  * Order is not significant, matching how the other unordered collections in the library are
489
567
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
490
568
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
569
+ *
570
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
571
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
572
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
573
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
491
574
  */
492
575
  function areUrlSearchParamsEqual(a, b) {
493
576
  const serializedA = a.toString();
494
577
  const serializedB = b.toString();
495
578
  // Identical serializations are equal under any ordering, and this is by far the common case, so
496
- // it is worth checking before sorting anything.
497
- return serializedA === serializedB || sortSearchParams(serializedA) === sortSearchParams(serializedB);
579
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
580
+ // mismatch rules out equality without sorting either.
581
+ return (serializedA === serializedB
582
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
498
583
  }
499
584
  /**
500
585
  * Whether the search params passed are equal in value with strict property checking.
501
586
  */
502
- function areUrlSearchParamsEqualStrict(a, b, state) {
503
- return areUrlSearchParamsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
504
- }
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);
505
592
  /**
506
593
  * Whether the `Set`s are equal in value.
507
594
  */
@@ -585,6 +672,15 @@ function areTypedArraysEqual(a, b) {
585
672
  }
586
673
  return true;
587
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
+ }
588
684
  /**
589
685
  * Whether the TypedArray instances are equal in value with strict property checking.
590
686
  *
@@ -593,33 +689,61 @@ function areTypedArraysEqual(a, b) {
593
689
  * spec. They are compared once by `areTypedArraysEqual`, so only the own properties after them
594
690
  * (including symbols) are compared with strict property checking.
595
691
  */
596
- function areTypedArraysEqualStrict(a, b, state) {
597
- return areTypedArraysEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, a.length, a.length);
598
- }
692
+ const areTypedArraysEqualStrict = createStrictComparatorTypedArray(false);
693
+ /**
694
+ * Same as `areTypedArraysEqualStrict`, tracking circular references in its own properties.
695
+ */
696
+ const areTypedArraysEqualStrictCircular = createStrictComparatorTypedArray(true);
599
697
  /**
600
698
  * Whether the URL instances are equal in value.
601
699
  */
602
700
  function areUrlsEqual(a, b) {
701
+ const hrefA = a.href;
702
+ const hrefB = b.href;
603
703
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
604
704
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
605
- if (a.href === b.href) {
705
+ if (hrefA === hrefB) {
606
706
  return true;
607
707
  }
608
- return (a.protocol === b.protocol
609
- && a.username === b.username
610
- && a.password === b.password
611
- // `host` covers both the hostname and the port.
612
- && a.host === b.host
613
- && a.pathname === b.pathname
614
- && a.hash === b.hash
615
- && areUrlSearchParamsEqual(a.searchParams, b.searchParams));
708
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
709
+ const length = hrefA.length;
710
+ if (length !== hrefB.length) {
711
+ return false;
712
+ }
713
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
714
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
715
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
716
+ // query, or the fragment when there is no query.
717
+ const start = hrefA.indexOf('?');
718
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
719
+ return false;
720
+ }
721
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
722
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
723
+ // `#` is in both hrefs.
724
+ let end = hrefA.indexOf('#');
725
+ if (end === -1) {
726
+ if (hrefB.includes('#', start)) {
727
+ return false;
728
+ }
729
+ end = length;
730
+ }
731
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
732
+ return false;
733
+ }
734
+ const queryA = hrefA.slice(start + 1, end);
735
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
736
+ // with a single pair cannot be reordered.
737
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
616
738
  }
617
739
  /**
618
740
  * Whether the URL instances are equal in value with strict property checking.
619
741
  */
620
- function areUrlsEqualStrict(a, b, state) {
621
- return areUrlsEqual(a, b) && areOwnPropertiesEqualStrict(a, b, state, 0, 0);
622
- }
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);
623
747
 
624
748
  const { toString } = Object.prototype;
625
749
  const toStringTag = typeof Symbol !== 'undefined' && Symbol.toStringTag;
@@ -755,8 +879,7 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
755
879
  // In strict mode, every type compares its own properties in addition to its value, because any
756
880
  // object can have properties assigned to it (`date.foo = 'bar'`). For types that hold their value
757
881
  // internally (`Date`, `RegExp`, TypedArrays, etc.), only the properties after the intrinsic ones
758
- // are compared, and circular references among them are tracked by the comparator itself (see
759
- // `areOwnPropertiesEqualStrict`).
882
+ // are compared. In circular mode, those comparators are wrapped with `createAreCircularItemsEqual` below.
760
883
  let config = {
761
884
  areArrayBuffersEqual: strict ? areArrayBuffersEqualStrict : areArrayBuffersEqual,
762
885
  areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
@@ -784,17 +907,51 @@ function createEqualityComparatorConfig({ circular, createCustomConfig, strict,
784
907
  config = Object.assign({}, config, createCustomConfig(config));
785
908
  }
786
909
  if (circular) {
787
- // Only containers are wrapped, since they always compare nested values. Types that hold their
788
- // value internally only do so for own properties in strict mode, and track those themselves.
789
- // Errors are included because comparing `cause` and own properties by value means an error
790
- // 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.
791
914
  config = Object.assign({}, config, {
792
- areArraysEqual: createIsCircular(config.areArraysEqual),
793
- areErrorsEqual: createIsCircular(config.areErrorsEqual),
794
- areMapsEqual: createIsCircular(config.areMapsEqual),
795
- areObjectsEqual: createIsCircular(config.areObjectsEqual),
796
- 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),
797
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
+ }
798
955
  }
799
956
  return config;
800
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.1"
114
+ "version": "6.4.3"
115
115
  }