fast-equals 5.0.1 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/.release-it.beta.json +6 -1
  2. package/.release-it.json +6 -1
  3. package/README.md +3 -0
  4. package/dist/cjs/index.cjs +69 -11
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/types/comparator.d.ts +1 -1
  7. package/dist/cjs/types/equals.d.ts +17 -0
  8. package/dist/cjs/types/index.d.ts +1 -1
  9. package/dist/cjs/types/internalTypes.d.ts +16 -0
  10. package/dist/cjs/types/utils.d.ts +1 -1
  11. package/dist/esm/index.mjs +69 -11
  12. package/dist/esm/index.mjs.map +1 -1
  13. package/dist/esm/types/comparator.d.ts +1 -1
  14. package/dist/esm/types/equals.d.ts +17 -0
  15. package/dist/esm/types/index.d.ts +1 -1
  16. package/dist/esm/types/internalTypes.d.ts +16 -0
  17. package/dist/esm/types/utils.d.ts +1 -1
  18. package/dist/min/index.js +1 -1
  19. package/dist/min/types/comparator.d.ts +1 -1
  20. package/dist/min/types/equals.d.ts +17 -0
  21. package/dist/min/types/index.d.ts +1 -1
  22. package/dist/min/types/internalTypes.d.ts +16 -0
  23. package/dist/min/types/utils.d.ts +1 -1
  24. package/dist/umd/index.js +69 -11
  25. package/dist/umd/index.js.map +1 -1
  26. package/dist/umd/types/comparator.d.ts +1 -1
  27. package/dist/umd/types/equals.d.ts +17 -0
  28. package/dist/umd/types/index.d.ts +1 -1
  29. package/dist/umd/types/internalTypes.d.ts +16 -0
  30. package/dist/umd/types/utils.d.ts +1 -1
  31. package/index.d.ts +18 -1
  32. package/package.json +28 -30
  33. package/src/comparator.ts +53 -13
  34. package/src/equals.ts +42 -0
  35. package/src/index.ts +18 -1
  36. package/src/internalTypes.ts +16 -0
  37. package/src/utils.ts +2 -2
  38. /package/{build → config}/rollup/config.base.js +0 -0
  39. /package/{build → config}/rollup/config.cjs.js +0 -0
  40. /package/{build → config}/rollup/config.esm.js +0 -0
  41. /package/{build → config}/rollup/config.min.js +0 -0
  42. /package/{build → config}/rollup/config.umd.js +0 -0
  43. /package/{build → config}/tsconfig/base.json +0 -0
  44. /package/{build → config}/tsconfig/cjs.json +0 -0
  45. /package/{build → config}/tsconfig/declarations.json +0 -0
  46. /package/{build → config}/tsconfig/esm.json +0 -0
  47. /package/{build → config}/tsconfig/min.json +0 -0
  48. /package/{build → config}/tsconfig/umd.json +0 -0
  49. /package/{build → config}/webpack.config.js +0 -0
package/src/comparator.ts CHANGED
@@ -1,13 +1,17 @@
1
1
  import {
2
2
  areArraysEqual as areArraysEqualDefault,
3
3
  areDatesEqual as areDatesEqualDefault,
4
+ areErrorsEqual as areErrorsEqualDefault,
5
+ areFunctionsEqual as areFunctionsEqualDefault,
4
6
  areMapsEqual as areMapsEqualDefault,
7
+ areNumbersEqual as areNumbersEqualDefault,
5
8
  areObjectsEqual as areObjectsEqualDefault,
6
9
  areObjectsEqualStrict as areObjectsEqualStrictDefault,
7
10
  arePrimitiveWrappersEqual as arePrimitiveWrappersEqualDefault,
8
11
  areRegExpsEqual as areRegExpsEqualDefault,
9
12
  areSetsEqual as areSetsEqualDefault,
10
- areTypedArraysEqual,
13
+ areTypedArraysEqual as areTypedArraysEqualDefault,
14
+ areUrlsEqual as areUrlsEqualDefault,
11
15
  } from './equals';
12
16
  import { combineComparators, createIsCircular } from './utils';
13
17
  import type {
@@ -22,12 +26,14 @@ import type {
22
26
  const ARGUMENTS_TAG = '[object Arguments]';
23
27
  const BOOLEAN_TAG = '[object Boolean]';
24
28
  const DATE_TAG = '[object Date]';
29
+ const ERROR_TAG = '[object Error]';
25
30
  const MAP_TAG = '[object Map]';
26
31
  const NUMBER_TAG = '[object Number]';
27
32
  const OBJECT_TAG = '[object Object]';
28
33
  const REG_EXP_TAG = '[object RegExp]';
29
34
  const SET_TAG = '[object Set]';
30
35
  const STRING_TAG = '[object String]';
36
+ const URL_TAG = '[object URL]';
31
37
 
32
38
  const { isArray } = Array;
33
39
  const isTypedArray =
@@ -53,12 +59,16 @@ interface CreateIsEqualOptions<Meta> {
53
59
  export function createEqualityComparator<Meta>({
54
60
  areArraysEqual,
55
61
  areDatesEqual,
62
+ areErrorsEqual,
63
+ areFunctionsEqual,
56
64
  areMapsEqual,
65
+ areNumbersEqual,
57
66
  areObjectsEqual,
58
67
  arePrimitiveWrappersEqual,
59
68
  areRegExpsEqual,
60
69
  areSetsEqual,
61
70
  areTypedArraysEqual,
71
+ areUrlsEqual,
62
72
  }: ComparatorConfig<Meta>): EqualityComparator<Meta> {
63
73
  /**
64
74
  * compare the value of the two objects and return true if they are equivalent in values
@@ -69,17 +79,29 @@ export function createEqualityComparator<Meta>({
69
79
  return true;
70
80
  }
71
81
 
72
- // If the items are not non-nullish objects, then the only possibility
73
- // of them being equal but not strictly is if they are both `NaN`. Since
74
- // `NaN` is uniquely not equal to itself, we can use self-comparison of
75
- // both objects, which is faster than `isNaN()`.
76
- if (
77
- a == null ||
78
- b == null ||
79
- typeof a !== 'object' ||
80
- typeof b !== 'object'
81
- ) {
82
- return a !== a && b !== b;
82
+ // If either of the items are nullish and fail the strictly equal check
83
+ // above, then they must be unequal.
84
+ if (a == null || b == null) {
85
+ return false;
86
+ }
87
+
88
+ const type = typeof a;
89
+
90
+ if (type !== typeof b) {
91
+ return false;
92
+ }
93
+
94
+ if (type !== 'object') {
95
+ if (type === 'number') {
96
+ return areNumbersEqual(a, b, state);
97
+ }
98
+
99
+ if (type === 'function') {
100
+ return areFunctionsEqual(a, b, state);
101
+ }
102
+
103
+ // If a primitive value that is not strictly equal, it must be unequal.
104
+ return false;
83
105
  }
84
106
 
85
107
  const constructor = a.constructor;
@@ -148,6 +170,8 @@ export function createEqualityComparator<Meta>({
148
170
  return areDatesEqual(a, b, state);
149
171
  }
150
172
 
173
+ // For RegExp, the properties are not enumerable, and therefore will give false positives if
174
+ // tested like a standard object.
151
175
  if (tag === REG_EXP_TAG) {
152
176
  return areRegExpsEqual(a, b, state);
153
177
  }
@@ -171,6 +195,18 @@ export function createEqualityComparator<Meta>({
171
195
  );
172
196
  }
173
197
 
198
+ // If a URL tag, it should be tested explicitly. Like RegExp, the properties are not
199
+ // enumerable, and therefore will give false positives if tested like a standard object.
200
+ if (tag === URL_TAG) {
201
+ return areUrlsEqual(a, b, state);
202
+ }
203
+
204
+ // If an error tag, it should be tested explicitly. Like RegExp, the properties are not
205
+ // enumerable, and therefore will give false positives if tested like a standard object.
206
+ if (tag === ERROR_TAG) {
207
+ return areErrorsEqual(a, b, state);
208
+ }
209
+
174
210
  // If an arguments tag, it should be treated as a standard object.
175
211
  if (tag === ARGUMENTS_TAG) {
176
212
  return areObjectsEqual(a, b, state);
@@ -211,9 +247,12 @@ export function createEqualityComparatorConfig<Meta>({
211
247
  ? areObjectsEqualStrictDefault
212
248
  : areArraysEqualDefault,
213
249
  areDatesEqual: areDatesEqualDefault,
250
+ areErrorsEqual: areErrorsEqualDefault,
251
+ areFunctionsEqual: areFunctionsEqualDefault,
214
252
  areMapsEqual: strict
215
253
  ? combineComparators(areMapsEqualDefault, areObjectsEqualStrictDefault)
216
254
  : areMapsEqualDefault,
255
+ areNumbersEqual: areNumbersEqualDefault,
217
256
  areObjectsEqual: strict
218
257
  ? areObjectsEqualStrictDefault
219
258
  : areObjectsEqualDefault,
@@ -224,7 +263,8 @@ export function createEqualityComparatorConfig<Meta>({
224
263
  : areSetsEqualDefault,
225
264
  areTypedArraysEqual: strict
226
265
  ? areObjectsEqualStrictDefault
227
- : areTypedArraysEqual,
266
+ : areTypedArraysEqualDefault,
267
+ areUrlsEqual: areUrlsEqualDefault,
228
268
  };
229
269
 
230
270
  if (createCustomConfig) {
package/src/equals.ts CHANGED
@@ -36,6 +36,28 @@ export function areDatesEqual(a: Date, b: Date): boolean {
36
36
  return sameValueZeroEqual(a.getTime(), b.getTime());
37
37
  }
38
38
 
39
+ /**
40
+ * Whether the errors passed are equal in value.
41
+ */
42
+ export function areErrorsEqual(a: Error, b: Error): boolean {
43
+ return (
44
+ a.name === b.name &&
45
+ a.message === b.message &&
46
+ a.cause === b.cause &&
47
+ a.stack === b.stack
48
+ );
49
+ }
50
+
51
+ /**
52
+ * Whether the functions passed are equal in value.
53
+ */
54
+ export function areFunctionsEqual(
55
+ a: (...args: any[]) => any,
56
+ b: (...args: any[]) => any,
57
+ ): boolean {
58
+ return a === b;
59
+ }
60
+
39
61
  /**
40
62
  * Whether the `Map`s are equal in value.
41
63
  */
@@ -96,6 +118,11 @@ export function areMapsEqual(
96
118
  return true;
97
119
  }
98
120
 
121
+ /**
122
+ * Whether the numbers are equal in value.
123
+ */
124
+ export const areNumbersEqual = sameValueZeroEqual;
125
+
99
126
  /**
100
127
  * Whether the objects are equal in value.
101
128
  */
@@ -298,3 +325,18 @@ export function areTypedArraysEqual(a: TypedArray, b: TypedArray) {
298
325
 
299
326
  return true;
300
327
  }
328
+
329
+ /**
330
+ * Whether the URL instances are equal in value.
331
+ */
332
+ export function areUrlsEqual(a: URL, b: URL): boolean {
333
+ return (
334
+ a.hostname === b.hostname &&
335
+ a.pathname === b.pathname &&
336
+ a.protocol === b.protocol &&
337
+ a.port === b.port &&
338
+ a.hash === b.hash &&
339
+ a.username === b.username &&
340
+ a.password === b.password
341
+ );
342
+ }
package/src/index.ts CHANGED
@@ -8,7 +8,24 @@ import type { CustomEqualCreatorOptions } from './internalTypes';
8
8
  import { sameValueZeroEqual } from './utils';
9
9
 
10
10
  export { sameValueZeroEqual };
11
- export * from './internalTypes';
11
+ export type {
12
+ AnyEqualityComparator,
13
+ Cache,
14
+ CircularState,
15
+ ComparatorConfig,
16
+ CreateCustomComparatorConfig,
17
+ CreateState,
18
+ CustomEqualCreatorOptions,
19
+ DefaultState,
20
+ Dictionary,
21
+ EqualityComparator,
22
+ EqualityComparatorCreator,
23
+ InternalEqualityComparator,
24
+ PrimitiveWrapper,
25
+ State,
26
+ TypeEqualityComparator,
27
+ TypedArray,
28
+ } from './internalTypes';
12
29
 
13
30
  /**
14
31
  * Whether the items passed are deeply-equal in value.
@@ -52,11 +52,23 @@ export interface ComparatorConfig<Meta> {
52
52
  * Whether the dates passed are equal in value.
53
53
  */
54
54
  areDatesEqual: TypeEqualityComparator<any, Meta>;
55
+ /**
56
+ * Whether the errors passed are equal in value.
57
+ */
58
+ areErrorsEqual: TypeEqualityComparator<any, Meta>;
59
+ /**
60
+ * Whether the functions passed are equal in value.
61
+ */
62
+ areFunctionsEqual: TypeEqualityComparator<any, Meta>;
55
63
  /**
56
64
  * Whether the maps passed are equal in value. In strict mode, this includes
57
65
  * additional properties added to the map.
58
66
  */
59
67
  areMapsEqual: TypeEqualityComparator<any, Meta>;
68
+ /**
69
+ * Whether the numbers passed are equal in value.
70
+ */
71
+ areNumbersEqual: TypeEqualityComparator<any, Meta>;
60
72
  /**
61
73
  * Whether the objects passed are equal in value. In strict mode, this includes
62
74
  * non-enumerable properties added to the map, as well as symbol properties.
@@ -80,6 +92,10 @@ export interface ComparatorConfig<Meta> {
80
92
  * additional properties added to the typed array.
81
93
  */
82
94
  areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
95
+ /**
96
+ * Whether the URLs passed are equal in value.
97
+ */
98
+ areUrlsEqual: TypeEqualityComparator<any, Meta>;
83
99
  }
84
100
 
85
101
  export type CreateCustomComparatorConfig<Meta> = (
package/src/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  AnyEqualityComparator,
3
3
  Cache,
4
4
  CircularState,
@@ -84,5 +84,5 @@ export const hasOwn =
84
84
  * Whether the values passed are strictly equal or both NaN.
85
85
  */
86
86
  export function sameValueZeroEqual(a: any, b: any): boolean {
87
- return a || b ? a === b : a === b || (a !== a && b !== b);
87
+ return a === b || (!a && !b && a !== a && b !== b);
88
88
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes