fast-equals 5.3.3-beta.1 → 5.3.4

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 (41) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +128 -77
  3. package/dist/cjs/comparator.d.cts +58 -0
  4. package/dist/cjs/index.cjs +133 -156
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/{types/index.d.cts → index.d.cts} +21 -2
  7. package/{src/internalTypes.ts → dist/cjs/internalTypes.d.cts} +7 -43
  8. package/dist/cjs/{types/utils.d.cts → utils.d.cts} +7 -2
  9. package/dist/es/comparator.d.mts +58 -0
  10. package/dist/{esm/types → es}/index.d.mts +21 -2
  11. package/dist/{esm → es}/index.mjs +133 -156
  12. package/dist/es/index.mjs.map +1 -0
  13. package/dist/es/internalTypes.d.mts +174 -0
  14. package/dist/{esm/types → es}/utils.d.mts +7 -2
  15. package/dist/{min/types → umd}/comparator.d.ts +2 -2
  16. package/dist/{min/types → umd}/equals.d.ts +1 -1
  17. package/dist/umd/{types/index.d.ts → index.d.ts} +2 -2
  18. package/dist/umd/index.js +133 -156
  19. package/dist/umd/index.js.map +1 -1
  20. package/dist/umd/{types/utils.d.ts → utils.d.ts} +1 -1
  21. package/index.d.ts +62 -68
  22. package/package.json +46 -54
  23. package/CHANGELOG.md +0 -364
  24. package/dist/cjs/types/comparator.d.cts +0 -26
  25. package/dist/cjs/types/internalTypes.d.cts +0 -157
  26. package/dist/esm/index.mjs.map +0 -1
  27. package/dist/esm/types/comparator.d.mts +0 -26
  28. package/dist/esm/types/internalTypes.d.mts +0 -157
  29. package/dist/min/index.js +0 -1
  30. package/dist/min/types/index.d.ts +0 -47
  31. package/dist/min/types/utils.d.ts +0 -28
  32. package/dist/umd/types/comparator.d.ts +0 -26
  33. package/dist/umd/types/equals.d.ts +0 -54
  34. package/dist/umd/types/internalTypes.d.ts +0 -157
  35. package/src/comparator.ts +0 -376
  36. package/src/equals.ts +0 -355
  37. package/src/index.ts +0 -112
  38. package/src/utils.ts +0 -96
  39. package/dist/cjs/{types/equals.d.cts → equals.d.cts} +1 -1
  40. package/dist/{esm/types → es}/equals.d.mts +1 -1
  41. /package/dist/{min/types → umd}/internalTypes.d.ts +0 -0
package/src/comparator.ts DELETED
@@ -1,376 +0,0 @@
1
- import {
2
- areArraysEqual as areArraysEqualDefault,
3
- areDatesEqual as areDatesEqualDefault,
4
- areErrorsEqual as areErrorsEqualDefault,
5
- areFunctionsEqual as areFunctionsEqualDefault,
6
- areMapsEqual as areMapsEqualDefault,
7
- areNumbersEqual as areNumbersEqualDefault,
8
- areObjectsEqual as areObjectsEqualDefault,
9
- areObjectsEqualStrict as areObjectsEqualStrictDefault,
10
- arePrimitiveWrappersEqual as arePrimitiveWrappersEqualDefault,
11
- areRegExpsEqual as areRegExpsEqualDefault,
12
- areSetsEqual as areSetsEqualDefault,
13
- areTypedArraysEqual as areTypedArraysEqualDefault,
14
- areUrlsEqual as areUrlsEqualDefault,
15
- } from './equals.js';
16
- import { combineComparators, createIsCircular, getShortTag } from './utils.js';
17
- import type {
18
- ComparatorConfig,
19
- CreateState,
20
- CustomEqualCreatorOptions,
21
- EqualityComparator,
22
- InternalEqualityComparator,
23
- State,
24
- } from './internalTypes.ts';
25
-
26
- const ARGUMENTS_TAG = '[object Arguments]';
27
- const BOOLEAN_TAG = '[object Boolean]';
28
- const DATE_TAG = '[object Date]';
29
- const ERROR_TAG = '[object Error]';
30
- const MAP_TAG = '[object Map]';
31
- const NUMBER_TAG = '[object Number]';
32
- const OBJECT_TAG = '[object Object]';
33
- const REG_EXP_TAG = '[object RegExp]';
34
- const SET_TAG = '[object Set]';
35
- const STRING_TAG = '[object String]';
36
- const URL_TAG = '[object URL]';
37
-
38
- const { isArray } = Array;
39
- const isTypedArray =
40
- typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function'
41
- ? ArrayBuffer.isView
42
- : null;
43
- const { assign } = Object;
44
- const getTag = Object.prototype.toString.call.bind(
45
- Object.prototype.toString,
46
- ) as (a: object) => string;
47
-
48
- interface CreateIsEqualOptions<Meta> {
49
- circular: boolean;
50
- comparator: EqualityComparator<Meta>;
51
- createState: CreateState<Meta> | undefined;
52
- equals: InternalEqualityComparator<Meta>;
53
- strict: boolean;
54
- }
55
-
56
- /**
57
- * Create a comparator method based on the type-specific equality comparators passed.
58
- */
59
- export function createEqualityComparator<Meta>({
60
- areArraysEqual,
61
- areDatesEqual,
62
- areErrorsEqual,
63
- areFunctionsEqual,
64
- areMapsEqual,
65
- areNumbersEqual,
66
- areObjectsEqual,
67
- arePrimitiveWrappersEqual,
68
- areRegExpsEqual,
69
- areSetsEqual,
70
- areTypedArraysEqual,
71
- areUrlsEqual,
72
- unknownTagComparators,
73
- }: ComparatorConfig<Meta>): EqualityComparator<Meta> {
74
- /**
75
- * compare the value of the two objects and return true if they are equivalent in values
76
- */
77
- return function comparator(a: any, b: any, state: State<Meta>): boolean {
78
- // If the items are strictly equal, no need to do a value comparison.
79
- if (a === b) {
80
- return true;
81
- }
82
-
83
- // If either of the items are nullish and fail the strictly equal check
84
- // above, then they must be unequal.
85
- if (a == null || b == null) {
86
- return false;
87
- }
88
-
89
- const type = typeof a;
90
-
91
- if (type !== typeof b) {
92
- return false;
93
- }
94
-
95
- if (type !== 'object') {
96
- if (type === 'number') {
97
- return areNumbersEqual(a, b, state);
98
- }
99
-
100
- if (type === 'function') {
101
- return areFunctionsEqual(a, b, state);
102
- }
103
-
104
- // If a primitive value that is not strictly equal, it must be unequal.
105
- return false;
106
- }
107
-
108
- const constructor = a.constructor;
109
-
110
- // Checks are listed in order of commonality of use-case:
111
- // 1. Common complex object types (plain object, array)
112
- // 2. Common data values (date, regexp)
113
- // 3. Less-common complex object types (map, set)
114
- // 4. Less-common data values (promise, primitive wrappers)
115
- // Inherently this is both subjective and assumptive, however
116
- // when reviewing comparable libraries in the wild this order
117
- // appears to be generally consistent.
118
-
119
- // Constructors should match, otherwise there is potential for false positives
120
- // between class and subclass or custom object and POJO.
121
- if (constructor !== b.constructor) {
122
- return false;
123
- }
124
-
125
- // `isPlainObject` only checks against the object's own realm. Cross-realm
126
- // comparisons are rare, and will be handled in the ultimate fallback, so
127
- // we can avoid capturing the string tag.
128
- if (constructor === Object) {
129
- return areObjectsEqual(a, b, state);
130
- }
131
-
132
- // `isArray()` works on subclasses and is cross-realm, so we can avoid capturing
133
- // the string tag or doing an `instanceof` check.
134
- if (isArray(a)) {
135
- return areArraysEqual(a, b, state);
136
- }
137
-
138
- // `isTypedArray()` works on all possible TypedArray classes, so we can avoid
139
- // capturing the string tag or comparing against all possible constructors.
140
- if (isTypedArray?.(a)) {
141
- return areTypedArraysEqual(a, b, state);
142
- }
143
-
144
- // Try to fast-path equality checks for other complex object types in the
145
- // same realm to avoid capturing the string tag. Strict equality is used
146
- // instead of `instanceof` because it is more performant for the common
147
- // use-case. If someone is subclassing a native class, it will be handled
148
- // with the string tag comparison.
149
-
150
- if (constructor === Date) {
151
- return areDatesEqual(a, b, state);
152
- }
153
-
154
- if (constructor === RegExp) {
155
- return areRegExpsEqual(a, b, state);
156
- }
157
-
158
- if (constructor === Map) {
159
- return areMapsEqual(a, b, state);
160
- }
161
-
162
- if (constructor === Set) {
163
- return areSetsEqual(a, b, state);
164
- }
165
-
166
- // Since this is a custom object, capture the string tag to determing its type.
167
- // This is reasonably performant in modern environments like v8 and SpiderMonkey.
168
- const tag = getTag(a as object);
169
-
170
- if (tag === DATE_TAG) {
171
- return areDatesEqual(a, b, state);
172
- }
173
-
174
- // For RegExp, the properties are not enumerable, and therefore will give false positives if
175
- // tested like a standard object.
176
- if (tag === REG_EXP_TAG) {
177
- return areRegExpsEqual(a, b, state);
178
- }
179
-
180
- if (tag === MAP_TAG) {
181
- return areMapsEqual(a, b, state);
182
- }
183
-
184
- if (tag === SET_TAG) {
185
- return areSetsEqual(a, b, state);
186
- }
187
-
188
- if (tag === OBJECT_TAG) {
189
- // The exception for value comparison is custom `Promise`-like class instances. These should
190
- // be treated the same as standard `Promise` objects, which means strict equality, and if
191
- // it reaches this point then that strict equality comparison has already failed.
192
- return (
193
- typeof a.then !== 'function' &&
194
- typeof b.then !== 'function' &&
195
- areObjectsEqual(a, b, state)
196
- );
197
- }
198
-
199
- // If a URL tag, it should be tested explicitly. Like RegExp, the properties are not
200
- // enumerable, and therefore will give false positives if tested like a standard object.
201
- if (tag === URL_TAG) {
202
- return areUrlsEqual(a, b, state);
203
- }
204
-
205
- // If an error tag, it should be tested explicitly. Like RegExp, the properties are not
206
- // enumerable, and therefore will give false positives if tested like a standard object.
207
- if (tag === ERROR_TAG) {
208
- return areErrorsEqual(a, b, state);
209
- }
210
-
211
- // If an arguments tag, it should be treated as a standard object.
212
- if (tag === ARGUMENTS_TAG) {
213
- return areObjectsEqual(a, b, state);
214
- }
215
-
216
- // As the penultimate fallback, check if the values passed are primitive wrappers. This
217
- // is very rare in modern JS, which is why it is deprioritized compared to all other object
218
- // types.
219
- if (tag === BOOLEAN_TAG || tag === NUMBER_TAG || tag === STRING_TAG) {
220
- return arePrimitiveWrappersEqual(a, b, state);
221
- }
222
-
223
- if (unknownTagComparators) {
224
- let unknownTagComparator = unknownTagComparators[tag];
225
-
226
- if (!unknownTagComparator) {
227
- const shortTag = getShortTag(a);
228
-
229
- if (shortTag) {
230
- unknownTagComparator = unknownTagComparators[shortTag];
231
- }
232
- }
233
-
234
- // If the custom config has an unknown tag comparator that matches the captured tag or the
235
- // @@toStringTag, it is the source of truth for whether the values are equal.
236
- if (unknownTagComparator) {
237
- return unknownTagComparator(a, b, state);
238
- }
239
- }
240
-
241
- // If not matching any tags that require a specific type of comparison, then we hard-code false because
242
- // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
243
- // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
244
- // comparison that can be made.
245
- // - For types that can be introspected, but rarely have requirements to be compared
246
- // (`ArrayBuffer`, `DataView`, etc.), the cost is avoided to prioritize the common
247
- // use-cases (may be included in a future release, if requested enough).
248
- // - For types that can be introspected but do not have an objective definition of what
249
- // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
250
- // In all cases, these decisions should be reevaluated based on changes to the language and
251
- // common development practices.
252
- return false;
253
- };
254
- }
255
-
256
- /**
257
- * Create the configuration object used for building comparators.
258
- */
259
- export function createEqualityComparatorConfig<Meta>({
260
- circular,
261
- createCustomConfig,
262
- strict,
263
- }: CustomEqualCreatorOptions<Meta>): ComparatorConfig<Meta> {
264
- let config = {
265
- areArraysEqual: strict
266
- ? areObjectsEqualStrictDefault
267
- : areArraysEqualDefault,
268
- areDatesEqual: areDatesEqualDefault,
269
- areErrorsEqual: areErrorsEqualDefault,
270
- areFunctionsEqual: areFunctionsEqualDefault,
271
- areMapsEqual: strict
272
- ? combineComparators(areMapsEqualDefault, areObjectsEqualStrictDefault)
273
- : areMapsEqualDefault,
274
- areNumbersEqual: areNumbersEqualDefault,
275
- areObjectsEqual: strict
276
- ? areObjectsEqualStrictDefault
277
- : areObjectsEqualDefault,
278
- arePrimitiveWrappersEqual: arePrimitiveWrappersEqualDefault,
279
- areRegExpsEqual: areRegExpsEqualDefault,
280
- areSetsEqual: strict
281
- ? combineComparators(areSetsEqualDefault, areObjectsEqualStrictDefault)
282
- : areSetsEqualDefault,
283
- areTypedArraysEqual: strict
284
- ? areObjectsEqualStrictDefault
285
- : areTypedArraysEqualDefault,
286
- areUrlsEqual: areUrlsEqualDefault,
287
- unknownTagComparators: undefined,
288
- };
289
-
290
- if (createCustomConfig) {
291
- config = assign({}, config, createCustomConfig(config));
292
- }
293
-
294
- if (circular) {
295
- const areArraysEqual = createIsCircular(config.areArraysEqual);
296
- const areMapsEqual = createIsCircular(config.areMapsEqual);
297
- const areObjectsEqual = createIsCircular(config.areObjectsEqual);
298
- const areSetsEqual = createIsCircular(config.areSetsEqual);
299
-
300
- config = assign({}, config, {
301
- areArraysEqual,
302
- areMapsEqual,
303
- areObjectsEqual,
304
- areSetsEqual,
305
- });
306
- }
307
-
308
- return config;
309
- }
310
-
311
- /**
312
- * Default equality comparator pass-through, used as the standard `isEqual` creator for
313
- * use inside the built comparator.
314
- */
315
- export function createInternalEqualityComparator<Meta>(
316
- compare: EqualityComparator<Meta>,
317
- ): InternalEqualityComparator<Meta> {
318
- return function (
319
- a: any,
320
- b: any,
321
- _indexOrKeyA: any,
322
- _indexOrKeyB: any,
323
- _parentA: any,
324
- _parentB: any,
325
- state: State<Meta>,
326
- ) {
327
- return compare(a, b, state);
328
- };
329
- }
330
-
331
- /**
332
- * Create the `isEqual` function used by the consuming application.
333
- */
334
- export function createIsEqual<Meta>({
335
- circular,
336
- comparator,
337
- createState,
338
- equals,
339
- strict,
340
- }: CreateIsEqualOptions<Meta>) {
341
- if (createState) {
342
- return function isEqual<A, B>(a: A, b: B): boolean {
343
- const { cache = circular ? new WeakMap() : undefined, meta } =
344
- createState();
345
-
346
- return comparator(a, b, {
347
- cache,
348
- equals,
349
- meta,
350
- strict,
351
- } as State<Meta>);
352
- };
353
- }
354
-
355
- if (circular) {
356
- return function isEqual<A, B>(a: A, b: B): boolean {
357
- return comparator(a, b, {
358
- cache: new WeakMap(),
359
- equals,
360
- meta: undefined as Meta,
361
- strict,
362
- } as State<Meta>);
363
- };
364
- }
365
-
366
- const state = {
367
- cache: undefined,
368
- equals,
369
- meta: undefined,
370
- strict,
371
- } as State<Meta>;
372
-
373
- return function isEqual<A, B>(a: A, b: B): boolean {
374
- return comparator(a, b, state);
375
- };
376
- }