fast-equals 6.4.1 → 6.4.2

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.06kB 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
@@ -153,15 +153,32 @@ function isReactProperty(a, b, property) {
153
153
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
154
154
  }
155
155
  /**
156
- * Reorder a serialized query string so that params holding the same pairs compare as equal
157
- * regardless of the order they appear in.
156
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
158
157
  *
159
158
  * @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.
159
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
160
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
161
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
162
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
163
+ *
164
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
162
165
  */
163
- function sortSearchParams(serialized) {
164
- return serialized.split('&').sort().join('&');
166
+ function areSortedPairsEqual(serializedA, serializedB) {
167
+ const pairsA = serializedA.split('&');
168
+ const pairsB = serializedB.split('&');
169
+ const length = pairsA.length;
170
+ if (length !== pairsB.length) {
171
+ return false;
172
+ }
173
+ pairsA.sort();
174
+ pairsB.sort();
175
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
176
+ for (let index = 0; index < length; ++index) {
177
+ if (pairsA[index] !== pairsB[index]) {
178
+ return false;
179
+ }
180
+ }
181
+ return true;
165
182
  }
166
183
 
167
184
  /**
@@ -490,13 +507,20 @@ function areRegExpsEqualStrict(a, b, state) {
490
507
  * Order is not significant, matching how the other unordered collections in the library are
491
508
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
492
509
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
510
+ *
511
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
512
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
513
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
514
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
493
515
  */
494
516
  function areUrlSearchParamsEqual(a, b) {
495
517
  const serializedA = a.toString();
496
518
  const serializedB = b.toString();
497
519
  // 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);
520
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
521
+ // mismatch rules out equality without sorting either.
522
+ return (serializedA === serializedB
523
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
500
524
  }
501
525
  /**
502
526
  * Whether the search params passed are equal in value with strict property checking.
@@ -602,19 +626,43 @@ function areTypedArraysEqualStrict(a, b, state) {
602
626
  * Whether the URL instances are equal in value.
603
627
  */
604
628
  function areUrlsEqual(a, b) {
629
+ const hrefA = a.href;
630
+ const hrefB = b.href;
605
631
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
606
632
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
607
- if (a.href === b.href) {
633
+ if (hrefA === hrefB) {
608
634
  return true;
609
635
  }
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));
636
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
637
+ const length = hrefA.length;
638
+ if (length !== hrefB.length) {
639
+ return false;
640
+ }
641
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
642
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
643
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
644
+ // query, or the fragment when there is no query.
645
+ const start = hrefA.indexOf('?');
646
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
647
+ return false;
648
+ }
649
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
650
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
651
+ // `#` is in both hrefs.
652
+ let end = hrefA.indexOf('#');
653
+ if (end === -1) {
654
+ if (hrefB.includes('#', start)) {
655
+ return false;
656
+ }
657
+ end = length;
658
+ }
659
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
660
+ return false;
661
+ }
662
+ const queryA = hrefA.slice(start + 1, end);
663
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
664
+ // with a single pair cannot be reordered.
665
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
618
666
  }
619
667
  /**
620
668
  * Whether the URL instances are equal in value with strict property checking.
package/dist/es/index.mjs CHANGED
@@ -151,15 +151,32 @@ function isReactProperty(a, b, property) {
151
151
  (property === '_owner' || property === '__o' || property === '__v') && !!(a.$$typeof || b.$$typeof));
152
152
  }
153
153
  /**
154
- * Reorder a serialized query string so that params holding the same pairs compare as equal
155
- * regardless of the order they appear in.
154
+ * Whether two serialized query strings hold the same pairs, regardless of the order they appear in.
156
155
  *
157
156
  * @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.
157
+ * Every `&` separates pairs, so splitting on it recovers exactly the pairs and nothing else. This
158
+ * holds for both inputs this receives: `URLSearchParams` serialization percent-encodes `&` inside
159
+ * a name or a value, and in a URL's query as written the parser treats every `&` as a separator.
160
+ * Empty pairs (from `a=1&` or `a=1&&b=2`) are kept, so they count like any other pair.
161
+ *
162
+ * Callers only reach this once they know the strings differ, so it does not check `===` first.
160
163
  */
161
- function sortSearchParams(serialized) {
162
- return serialized.split('&').sort().join('&');
164
+ function areSortedPairsEqual(serializedA, serializedB) {
165
+ const pairsA = serializedA.split('&');
166
+ const pairsB = serializedB.split('&');
167
+ const length = pairsA.length;
168
+ if (length !== pairsB.length) {
169
+ return false;
170
+ }
171
+ pairsA.sort();
172
+ pairsB.sort();
173
+ // Comparing pair by pair stops at the first difference, rather than joining both back together.
174
+ for (let index = 0; index < length; ++index) {
175
+ if (pairsA[index] !== pairsB[index]) {
176
+ return false;
177
+ }
178
+ }
179
+ return true;
163
180
  }
164
181
 
165
182
  /**
@@ -488,13 +505,20 @@ function areRegExpsEqualStrict(a, b, state) {
488
505
  * Order is not significant, matching how the other unordered collections in the library are
489
506
  * compared. Repeated keys are, so this is a comparison of multisets rather than of sets:
490
507
  * `a=1&a=2` is equal to `a=2&a=1`, but not to `a=1&a=1`.
508
+ *
509
+ * This is the same rule `areUrlsEqual` applies to a URL's query, applied to the params'
510
+ * serialization. `URLSearchParams` keeps only the decoded pairs, so differences in how a query was
511
+ * written (`a=%41` vs `a=A`, `a` vs `a=`, a trailing `&`) are gone before they reach here. As a
512
+ * result, URLs that are equal always have equal `searchParams`, but not the reverse.
491
513
  */
492
514
  function areUrlSearchParamsEqual(a, b) {
493
515
  const serializedA = a.toString();
494
516
  const serializedB = b.toString();
495
517
  // 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);
518
+ // it is worth checking before sorting anything. Reordering pairs preserves length, so a length
519
+ // mismatch rules out equality without sorting either.
520
+ return (serializedA === serializedB
521
+ || (serializedA.length === serializedB.length && areSortedPairsEqual(serializedA, serializedB)));
498
522
  }
499
523
  /**
500
524
  * Whether the search params passed are equal in value with strict property checking.
@@ -600,19 +624,43 @@ function areTypedArraysEqualStrict(a, b, state) {
600
624
  * Whether the URL instances are equal in value.
601
625
  */
602
626
  function areUrlsEqual(a, b) {
627
+ const hrefA = a.href;
628
+ const hrefB = b.href;
603
629
  // `href` is the normalized serialization of every component, so matching hrefs are equal without
604
630
  // any further work. Only a difference in query parameter ordering can survive a mismatch here.
605
- if (a.href === b.href) {
631
+ if (hrefA === hrefB) {
606
632
  return true;
607
633
  }
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));
634
+ // Reordering the query's pairs preserves length, so equal URLs always have hrefs of equal length.
635
+ const length = hrefA.length;
636
+ if (length !== hrefB.length) {
637
+ return false;
638
+ }
639
+ // The rest of the URL is compared as serialized, rather than through the component getters, which
640
+ // hide some differences (`search` and `hash` are `''` for both `?` / `#` and a missing query /
641
+ // fragment). Every component before the query percent-encodes `?`, so the first one starts the
642
+ // query, or the fragment when there is no query.
643
+ const start = hrefA.indexOf('?');
644
+ if (start === -1 || start !== hrefB.indexOf('?') || hrefA.slice(0, start) !== hrefB.slice(0, start)) {
645
+ return false;
646
+ }
647
+ // The query percent-encodes `#`, so the first one starts the fragment. One before the `?` means
648
+ // that `?` is inside the fragment, so there is no query to reorder. The prefixes match, so that
649
+ // `#` is in both hrefs.
650
+ let end = hrefA.indexOf('#');
651
+ if (end === -1) {
652
+ if (hrefB.includes('#', start)) {
653
+ return false;
654
+ }
655
+ end = length;
656
+ }
657
+ else if (end < start || hrefB.indexOf('#', start) !== end || hrefA.slice(end) !== hrefB.slice(end)) {
658
+ return false;
659
+ }
660
+ const queryA = hrefA.slice(start + 1, end);
661
+ // Everything outside the query matches and the hrefs differ, so the queries differ, and a query
662
+ // with a single pair cannot be reordered.
663
+ return queryA.includes('&') && areSortedPairsEqual(queryA, hrefB.slice(start + 1, end));
616
664
  }
617
665
  /**
618
666
  * Whether the URL instances are equal in value with strict property checking.
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.2"
115
115
  }