fast-equals 5.0.0-beta.5 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/README.md +68 -48
  3. package/dist/cjs/index.cjs +165 -140
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs/types/comparator.d.ts +20 -3
  6. package/dist/cjs/types/equals.d.ts +5 -2
  7. package/dist/cjs/types/index.d.ts +9 -9
  8. package/dist/cjs/types/internalTypes.d.ts +100 -12
  9. package/dist/cjs/types/utils.d.ts +1 -6
  10. package/dist/esm/index.mjs +165 -140
  11. package/dist/esm/index.mjs.map +1 -1
  12. package/dist/esm/types/comparator.d.ts +20 -3
  13. package/dist/esm/types/equals.d.ts +5 -2
  14. package/dist/esm/types/index.d.ts +9 -9
  15. package/dist/esm/types/internalTypes.d.ts +100 -12
  16. package/dist/esm/types/utils.d.ts +1 -6
  17. package/dist/min/index.js +1 -1
  18. package/dist/min/types/comparator.d.ts +20 -3
  19. package/dist/min/types/equals.d.ts +5 -2
  20. package/dist/min/types/index.d.ts +9 -9
  21. package/dist/min/types/internalTypes.d.ts +100 -12
  22. package/dist/min/types/utils.d.ts +1 -6
  23. package/dist/umd/index.js +165 -140
  24. package/dist/umd/index.js.map +1 -1
  25. package/dist/umd/types/comparator.d.ts +20 -3
  26. package/dist/umd/types/equals.d.ts +5 -2
  27. package/dist/umd/types/index.d.ts +9 -9
  28. package/dist/umd/types/internalTypes.d.ts +100 -12
  29. package/dist/umd/types/utils.d.ts +1 -6
  30. package/package.json +7 -7
  31. package/recipes/legacy-circular-equal-support.md +3 -2
  32. package/recipes/legacy-regexp-support.md +2 -3
  33. package/recipes/non-standard-properties.md +4 -2
  34. package/src/comparator.ts +126 -38
  35. package/src/equals.ts +97 -63
  36. package/src/index.ts +21 -65
  37. package/src/internalTypes.ts +104 -16
  38. package/src/utils.ts +2 -24
package/src/equals.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { getStrictProperties, hasOwn, sameValueZeroEqual } from './utils';
2
- import type { Dictionary, State, TypedArray } from './internalTypes';
2
+ import type {
3
+ Dictionary,
4
+ PrimitiveWrapper,
5
+ State,
6
+ TypedArray,
7
+ } from './internalTypes';
3
8
 
4
9
  const OWNER = '_owner';
5
10
 
@@ -39,47 +44,56 @@ export function areMapsEqual(
39
44
  b: Map<any, any>,
40
45
  state: State<any>,
41
46
  ): boolean {
42
- let isValueEqual = a.size === b.size;
47
+ if (a.size !== b.size) {
48
+ return false;
49
+ }
50
+
51
+ const matchedIndices: Record<number, true> = {};
52
+ const aIterable = a.entries();
53
+
54
+ let index = 0;
55
+ let aResult: IteratorResult<[any, any]>;
56
+ let bResult: IteratorResult<[any, any]>;
43
57
 
44
- if (isValueEqual && a.size) {
45
- // The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
46
- // the inability to control the performance of the resulting code. It also avoids excessive
47
- // iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
48
- // we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
49
- // equality checks themselves.
58
+ while ((aResult = aIterable.next())) {
59
+ if (aResult.done) {
60
+ break;
61
+ }
62
+
63
+ const bIterable = b.entries();
50
64
 
51
- const matchedIndices: Record<number, true> = {};
65
+ let hasMatch = false;
66
+ let matchIndex = 0;
52
67
 
53
- let indexA = 0;
68
+ while ((bResult = bIterable.next())) {
69
+ if (bResult.done) {
70
+ break;
71
+ }
54
72
 
55
- a.forEach((aValue, aKey) => {
56
- if (!isValueEqual) {
57
- return;
73
+ const [aKey, aValue] = aResult.value;
74
+ const [bKey, bValue] = bResult.value;
75
+
76
+ if (
77
+ !hasMatch &&
78
+ !matchedIndices[matchIndex] &&
79
+ (hasMatch =
80
+ state.equals(aKey, bKey, index, matchIndex, a, b, state) &&
81
+ state.equals(aValue, bValue, aKey, bKey, a, b, state))
82
+ ) {
83
+ matchedIndices[matchIndex] = true;
58
84
  }
59
85
 
60
- let hasMatch = false;
61
- let matchIndexB = 0;
62
-
63
- b.forEach((bValue, bKey) => {
64
- if (
65
- !hasMatch &&
66
- !matchedIndices[matchIndexB] &&
67
- (hasMatch =
68
- state.equals(aKey, bKey, indexA, matchIndexB, a, b, state) &&
69
- state.equals(aValue, bValue, aKey, bKey, a, b, state))
70
- ) {
71
- matchedIndices[matchIndexB] = true;
72
- }
73
-
74
- matchIndexB++;
75
- });
76
-
77
- indexA++;
78
- isValueEqual = hasMatch;
79
- });
86
+ matchIndex++;
87
+ }
88
+
89
+ if (!hasMatch) {
90
+ return false;
91
+ }
92
+
93
+ index++;
80
94
  }
81
95
 
82
- return isValueEqual;
96
+ return true;
83
97
  }
84
98
 
85
99
  /**
@@ -98,7 +112,7 @@ export function areObjectsEqual(
98
112
  return false;
99
113
  }
100
114
 
101
- let property: string | symbol;
115
+ let property: string;
102
116
 
103
117
  // Decrementing `while` showed faster results than either incrementing or
104
118
  // decrementing `for` loop and than an incrementing `while` loop. Declarative
@@ -192,7 +206,10 @@ export function areObjectsEqualStrict(
192
206
  /**
193
207
  * Whether the primitive wrappers passed are equal in value.
194
208
  */
195
- export function arePrimitiveWrappersEqual(a: Date, b: Date): boolean {
209
+ export function arePrimitiveWrappersEqual(
210
+ a: PrimitiveWrapper,
211
+ b: PrimitiveWrapper,
212
+ ): boolean {
196
213
  return sameValueZeroEqual(a.valueOf(), b.valueOf());
197
214
  }
198
215
 
@@ -211,44 +228,61 @@ export function areSetsEqual(
211
228
  b: Set<any>,
212
229
  state: State<any>,
213
230
  ): boolean {
214
- let isValueEqual = a.size === b.size;
231
+ if (a.size !== b.size) {
232
+ return false;
233
+ }
215
234
 
216
- if (isValueEqual && a.size) {
217
- // The use of `forEach()` is to avoid the transpilation cost of `for...of` comparisons, and
218
- // the inability to control the performance of the resulting code. It also avoids excessive
219
- // iteration compared to doing comparisons of `keys()` and `values()`. As a result, though,
220
- // we cannot short-circuit the iterations; bookkeeping must be done to short-circuit the
221
- // equality checks themselves.
235
+ const matchedIndices: Record<number, true> = {};
236
+ const aIterable = a.values();
222
237
 
223
- const matchedIndices: Record<number, true> = {};
238
+ let aResult: IteratorResult<any>;
239
+ let bResult: IteratorResult<any>;
224
240
 
225
- a.forEach((aValue, aKey) => {
226
- if (!isValueEqual) {
227
- return;
228
- }
241
+ while ((aResult = aIterable.next())) {
242
+ if (aResult.done) {
243
+ break;
244
+ }
229
245
 
230
- let hasMatch = false;
231
- let matchIndex = 0;
246
+ const bIterable = b.values();
247
+
248
+ let hasMatch = false;
249
+ let matchIndex = 0;
250
+
251
+ while ((bResult = bIterable.next())) {
252
+ if (bResult.done) {
253
+ break;
254
+ }
232
255
 
233
- b.forEach((bValue, bKey) => {
234
- if (
235
- !hasMatch &&
236
- !matchedIndices[matchIndex] &&
237
- (hasMatch = state.equals(aValue, bValue, aKey, bKey, a, b, state))
238
- ) {
239
- matchedIndices[matchIndex] = true;
240
- }
256
+ if (
257
+ !hasMatch &&
258
+ !matchedIndices[matchIndex] &&
259
+ (hasMatch = state.equals(
260
+ aResult.value,
261
+ bResult.value,
262
+ aResult.value,
263
+ bResult.value,
264
+ a,
265
+ b,
266
+ state,
267
+ ))
268
+ ) {
269
+ matchedIndices[matchIndex] = true;
270
+ }
241
271
 
242
- matchIndex++;
243
- });
272
+ matchIndex++;
273
+ }
244
274
 
245
- isValueEqual = hasMatch;
246
- });
275
+ if (!hasMatch) {
276
+ return false;
277
+ }
247
278
  }
248
279
 
249
- return isValueEqual;
280
+ return true;
250
281
  }
251
282
 
283
+ /**
284
+ * Whether the TypedArray instances are equal in value.
285
+ */
252
286
  export function areTypedArraysEqual(a: TypedArray, b: TypedArray) {
253
287
  let index = a.length;
254
288
 
package/src/index.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { createComparator, createComparatorConfig } from './comparator';
2
- import type {
3
- CircularState,
4
- CustomEqualCreatorOptions,
5
- DefaultState,
6
- } from './internalTypes';
7
- import { createInternalComparator, sameValueZeroEqual } from './utils';
1
+ import {
2
+ createEqualityComparatorConfig,
3
+ createEqualityComparator,
4
+ createInternalEqualityComparator,
5
+ createIsEqual,
6
+ } from './comparator';
7
+ import type { CustomEqualCreatorOptions } from './internalTypes';
8
+ import { sameValueZeroEqual } from './utils';
8
9
 
9
10
  export { sameValueZeroEqual };
10
11
  export * from './internalTypes';
@@ -37,23 +38,23 @@ export const strictCircularDeepEqual = createCustomEqual({
37
38
  * Whether the items passed are shallowly-equal in value.
38
39
  */
39
40
  export const shallowEqual = createCustomEqual({
40
- comparator: sameValueZeroEqual,
41
+ createInternalComparator: () => sameValueZeroEqual,
41
42
  });
42
43
 
43
44
  /**
44
45
  * Whether the items passed are shallowly-equal in value based on strict comparison
45
46
  */
46
47
  export const strictShallowEqual = createCustomEqual({
47
- comparator: sameValueZeroEqual,
48
48
  strict: true,
49
+ createInternalComparator: () => sameValueZeroEqual,
49
50
  });
50
51
 
51
52
  /**
52
53
  * Whether the items passed are shallowly-equal in value, including circular references.
53
54
  */
54
55
  export const circularShallowEqual = createCustomEqual({
55
- comparator: sameValueZeroEqual,
56
56
  circular: true,
57
+ createInternalComparator: () => sameValueZeroEqual,
57
58
  });
58
59
 
59
60
  /**
@@ -61,8 +62,8 @@ export const circularShallowEqual = createCustomEqual({
61
62
  * based on strict comparison.
62
63
  */
63
64
  export const strictCircularShallowEqual = createCustomEqual({
64
- comparator: sameValueZeroEqual,
65
65
  circular: true,
66
+ createInternalComparator: () => sameValueZeroEqual,
66
67
  strict: true,
67
68
  });
68
69
 
@@ -74,66 +75,21 @@ export const strictCircularShallowEqual = createCustomEqual({
74
75
  * support for legacy environments that do not support expected features like
75
76
  * `RegExp.prototype.flags` out of the box.
76
77
  */
77
- export function createCustomEqual<Meta>(
78
+ export function createCustomEqual<Meta = undefined>(
78
79
  options: CustomEqualCreatorOptions<Meta> = {},
79
80
  ) {
80
81
  const {
81
- circular,
82
- comparator,
82
+ circular = false,
83
83
  createInternalComparator: createCustomInternalComparator,
84
84
  createState,
85
- strict: baseStrict = false,
85
+ strict = false,
86
86
  } = options;
87
87
 
88
- const config = createComparatorConfig<Meta>(options);
89
- const isEqualCustom = createComparator(config);
90
- const isEqualCustomComparator =
91
- comparator ||
92
- (createCustomInternalComparator
93
- ? createCustomInternalComparator(isEqualCustom)
94
- : createInternalComparator(isEqualCustom));
88
+ const config = createEqualityComparatorConfig<Meta>(options);
89
+ const comparator = createEqualityComparator(config);
90
+ const equals = createCustomInternalComparator
91
+ ? createCustomInternalComparator(comparator)
92
+ : createInternalEqualityComparator(comparator);
95
93
 
96
- if (createState) {
97
- return function isEqual<A, B>(
98
- a: A,
99
- b: B,
100
- metaOverride?: Meta | undefined,
101
- ): boolean {
102
- const {
103
- cache = circular ? new WeakMap() : undefined,
104
- equals = isEqualCustomComparator,
105
- meta,
106
- strict = baseStrict,
107
- } = createState!(isEqualCustom);
108
-
109
- return isEqualCustom(a, b, {
110
- cache,
111
- equals,
112
- meta: metaOverride !== undefined ? metaOverride : meta,
113
- strict,
114
- } as CircularState<Meta>);
115
- };
116
- }
117
-
118
- if (circular) {
119
- return function equals<A, B>(a: A, b: B): boolean {
120
- return isEqualCustom(a, b, {
121
- cache: new WeakMap(),
122
- equals: isEqualCustomComparator,
123
- meta: undefined as Meta,
124
- strict: baseStrict,
125
- } as CircularState<Meta>);
126
- };
127
- }
128
-
129
- const state = Object.freeze({
130
- cache: undefined,
131
- equals: isEqualCustomComparator,
132
- meta: undefined,
133
- strict: baseStrict,
134
- });
135
-
136
- return function equals<A, B>(a: A, b: B): boolean {
137
- return isEqualCustom(a, b, state as DefaultState<Meta>);
138
- };
94
+ return createIsEqual({ circular, comparator, createState, equals, strict });
139
95
  }
@@ -1,22 +1,40 @@
1
- export interface BaseCircular
2
- extends Pick<WeakMap<any, any>, 'delete' | 'get'> {
3
- set(key: object, value: any): any;
1
+ /**
2
+ * Cache used to store references to objects, used for circular
3
+ * reference checks.
4
+ */
5
+ export interface Cache<Key extends object, Value> {
6
+ delete(key: Key): boolean;
7
+ get(key: Key): Value | undefined;
8
+ set(key: Key, value: any): any;
4
9
  }
5
10
 
6
- export type State<Meta> = CircularState<Meta> | DefaultState<Meta>;
7
-
8
- export interface CircularState<Meta> {
9
- readonly cache: BaseCircular;
11
+ export interface State<Meta> {
12
+ /**
13
+ * Cache used to identify circular references
14
+ */
15
+ readonly cache: Cache<any, any> | undefined;
16
+ /**
17
+ * Method used to determine equality of nested value.
18
+ */
10
19
  readonly equals: InternalEqualityComparator<Meta>;
20
+ /**
21
+ * Additional value that can be used for comparisons.
22
+ */
11
23
  meta: Meta;
24
+ /**
25
+ * Whether the equality comparison is strict, meaning it matches
26
+ * all properties (including symbols and non-enumerable properties)
27
+ * with equal shape of descriptors.
28
+ */
12
29
  readonly strict: boolean;
13
30
  }
14
31
 
15
- export interface DefaultState<Meta> {
32
+ export interface CircularState<Meta> extends State<Meta> {
33
+ readonly cache: Cache<any, any>;
34
+ }
35
+
36
+ export interface DefaultState<Meta> extends State<Meta> {
16
37
  readonly cache: undefined;
17
- readonly equals: InternalEqualityComparator<Meta>;
18
- meta: Meta;
19
- readonly strict: boolean;
20
38
  }
21
39
 
22
40
  export interface Dictionary<Value = any> {
@@ -25,13 +43,42 @@ export interface Dictionary<Value = any> {
25
43
  }
26
44
 
27
45
  export interface ComparatorConfig<Meta> {
46
+ /**
47
+ * Whether the arrays passed are equal in value. In strict mode, this includes
48
+ * additional properties added to the array.
49
+ */
28
50
  areArraysEqual: TypeEqualityComparator<any, Meta>;
51
+ /**
52
+ * Whether the dates passed are equal in value.
53
+ */
29
54
  areDatesEqual: TypeEqualityComparator<any, Meta>;
55
+ /**
56
+ * Whether the maps passed are equal in value. In strict mode, this includes
57
+ * additional properties added to the map.
58
+ */
30
59
  areMapsEqual: TypeEqualityComparator<any, Meta>;
60
+ /**
61
+ * Whether the objects passed are equal in value. In strict mode, this includes
62
+ * non-enumerable properties added to the map, as well as symbol properties.
63
+ */
31
64
  areObjectsEqual: TypeEqualityComparator<any, Meta>;
65
+ /**
66
+ * Whether the primitive wrappers passed are equal in value.
67
+ */
32
68
  arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
69
+ /**
70
+ * Whether the regexps passed are equal in value.
71
+ */
33
72
  areRegExpsEqual: TypeEqualityComparator<any, Meta>;
73
+ /**
74
+ * Whether the sets passed are equal in value. In strict mode, this includes
75
+ * additional properties added to the set.
76
+ */
34
77
  areSetsEqual: TypeEqualityComparator<any, Meta>;
78
+ /**
79
+ * Whether the typed arrays passed are equal in value. In strict mode, this includes
80
+ * additional properties added to the typed array.
81
+ */
35
82
  areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
36
83
  }
37
84
 
@@ -39,9 +86,10 @@ export type CreateCustomComparatorConfig<Meta> = (
39
86
  config: ComparatorConfig<Meta>,
40
87
  ) => Partial<ComparatorConfig<Meta>>;
41
88
 
42
- export type CreateState<Meta> = (
43
- comparator: EqualityComparator<Meta>,
44
- ) => Partial<State<Meta>>;
89
+ export type CreateState<Meta> = () => {
90
+ cache?: Cache<any, any> | undefined;
91
+ meta?: Meta;
92
+ };
45
93
 
46
94
  export type EqualityComparator<Meta> = <A, B>(
47
95
  a: A,
@@ -68,6 +116,21 @@ export type InternalEqualityComparator<Meta> = (
68
116
  state: State<Meta>,
69
117
  ) => boolean;
70
118
 
119
+ // We explicitly check for primitive wrapper types
120
+ // eslint-disable-next-line @typescript-eslint/ban-types
121
+ export type PrimitiveWrapper = Boolean | Number | String;
122
+
123
+ /**
124
+ * Type which encompasses possible instances of TypedArray
125
+ * classes.
126
+ *
127
+ * **NOTE**: This does not include `BigInt64Array` and
128
+ * `BitUint64Array` because those are part of ES2020 and
129
+ * not supported by certain TS configurations. If using
130
+ * either in `areTypedArraysEqual`, you can cast the
131
+ * instance as `TypedArray` and it will work as expected,
132
+ * because runtime checks will still work for those classes.
133
+ */
71
134
  export type TypedArray =
72
135
  | Float32Array
73
136
  | Float64Array
@@ -86,12 +149,37 @@ export type TypeEqualityComparator<Type, Meta = undefined> = (
86
149
  ) => boolean;
87
150
 
88
151
  export interface CustomEqualCreatorOptions<Meta> {
152
+ /**
153
+ * Whether circular references should be supported. It causes the
154
+ * comparison to be slower, but for objects that have circular references
155
+ * it is required to avoid stack overflows.
156
+ */
89
157
  circular?: boolean;
90
- comparator?: EqualityComparator<Meta>;
158
+ /**
159
+ * Create a custom configuration of type-specific equality comparators.
160
+ * This receives the default configuration, which allows either replacement
161
+ * or supersetting of the default methods.
162
+ */
91
163
  createCustomConfig?: CreateCustomComparatorConfig<Meta>;
92
- createInternalComparator?: <Meta>(
164
+ /**
165
+ * Create a custom internal comparator, which is used as an override to the
166
+ * default entry point for nested value equality comparisons. This is often
167
+ * used for doing custom logic for specific types (such as handling a specific
168
+ * class instance differently than other objects) or to incorporate `meta` in
169
+ * the comparison. See the recipes for examples.
170
+ */
171
+ createInternalComparator?: (
93
172
  compare: EqualityComparator<Meta>,
94
173
  ) => InternalEqualityComparator<Meta>;
174
+ /**
175
+ * Create a custom `state` object passed between the methods. This allows for
176
+ * custom `cache` and/or `meta` values to be used.
177
+ */
95
178
  createState?: CreateState<Meta>;
179
+ /**
180
+ * Whether the equality comparison is strict, meaning it matches
181
+ * all properties (including symbols and non-enumerable properties)
182
+ * with equal shape of descriptors.
183
+ */
96
184
  strict?: boolean;
97
185
  }
package/src/utils.ts CHANGED
@@ -1,10 +1,8 @@
1
1
  import {
2
2
  AnyEqualityComparator,
3
- BaseCircular,
3
+ Cache,
4
4
  CircularState,
5
5
  Dictionary,
6
- EqualityComparator,
7
- InternalEqualityComparator,
8
6
  State,
9
7
  TypeEqualityComparator,
10
8
  } from './internalTypes';
@@ -24,26 +22,6 @@ export function combineComparators<Meta>(
24
22
  };
25
23
  }
26
24
 
27
- /**
28
- * Default equality comparator pass-through, used as the standard `isEqual` creator for
29
- * use inside the built comparator.
30
- */
31
- export function createInternalComparator<Meta>(
32
- compare: EqualityComparator<Meta>,
33
- ): InternalEqualityComparator<Meta> {
34
- return function (
35
- a: any,
36
- b: any,
37
- _indexOrKeyA: any,
38
- _indexOrKeyB: any,
39
- _parentA: any,
40
- _parentB: any,
41
- state: State<Meta>,
42
- ) {
43
- return compare(a, b, state);
44
- };
45
- }
46
-
47
25
  /**
48
26
  * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
49
27
  * for circular references to be safely included in the comparison without creating
@@ -55,7 +33,7 @@ export function createIsCircular<
55
33
  return function isCircular(
56
34
  a: any,
57
35
  b: any,
58
- state: CircularState<BaseCircular>,
36
+ state: CircularState<Cache<any, any>>,
59
37
  ) {
60
38
  if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
61
39
  return areItemsEqual(a, b, state);