fast-equals 5.0.0-beta.2 → 5.0.0-beta.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.
@@ -1,110 +1,9 @@
1
- var ARGUMENTS_TAG = '[object Arguments]';
2
- var BOOLEAN_TAG = '[object Boolean]';
3
- var DATE_TAG = '[object Date]';
4
- var MAP_TAG = '[object Map]';
5
- var NUMBER_TAG = '[object Number]';
6
- var OBJECT_TAG = '[object Object]';
7
- var REG_EXP_TAG = '[object RegExp]';
8
- var SET_TAG = '[object Set]';
9
- var STRING_TAG = '[object String]';
10
- var isArray = Array.isArray;
11
- var getTag = Object.prototype.toString.call.bind(Object.prototype.toString);
12
- function createComparator(_a) {
13
- var areArraysEqual = _a.areArraysEqual, areDatesEqual = _a.areDatesEqual, areMapsEqual = _a.areMapsEqual, areObjectsEqual = _a.areObjectsEqual, arePrimitiveWrappersEqual = _a.arePrimitiveWrappersEqual, areRegExpsEqual = _a.areRegExpsEqual, areSetsEqual = _a.areSetsEqual;
14
- /**
15
- * compare the value of the two objects and return true if they are equivalent in values
16
- */
17
- return function comparator(a, b, state) {
18
- // If the items are strictly equal, no need to do a value comparison.
19
- if (a === b) {
20
- return true;
21
- }
22
- // If the items are not non-nullish objects, then the only possibility
23
- // of them being equal but not strictly is if they are both `NaN`. Since
24
- // `NaN` is uniquely not equal to itself, we can use self-comparison of
25
- // both objects, which is faster than `isNaN()`.
26
- if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
27
- return a !== a && b !== b;
28
- }
29
- // Checks are listed in order of commonality of use-case:
30
- // 1. Common complex object types (plain object, array)
31
- // 2. Common data values (date, regexp)
32
- // 3. Less-common complex object types (map, set)
33
- // 4. Less-common data values (promise, primitive wrappers)
34
- // Inherently this is both subjective and assumptive, however
35
- // when reviewing comparable libraries in the wild this order
36
- // appears to be generally consistent.
37
- // `isPlainObject` only checks against the object's own realm. Cross-realm
38
- // comparisons are rare, and will be handled in the ultimate fallback, so
39
- // we can avoid the `toString.call()` cost unless necessary.
40
- if (a.constructor === Object && b.constructor === Object) {
41
- return areObjectsEqual(a, b, state);
42
- }
43
- // `isArray()` works on subclasses and is cross-realm, so we can again avoid
44
- // the `toString.call()` cost unless necessary by just checking if either
45
- // and then both are arrays.
46
- var aArray = isArray(a);
47
- var bArray = isArray(b);
48
- if (aArray || bArray) {
49
- return aArray === bArray && areArraysEqual(a, b, state);
50
- }
51
- // Since this is a custom object, use the classic `toString.call()` to get its
52
- // type. This is reasonably performant in modern environments like v8 and
53
- // SpiderMonkey, and allows for cross-realm comparison when other checks like
54
- // `instanceof` do not.
55
- var tag = getTag(a);
56
- if (tag !== getTag(b)) {
57
- return false;
58
- }
59
- if (tag === DATE_TAG) {
60
- return areDatesEqual(a, b, state);
61
- }
62
- if (tag === REG_EXP_TAG) {
63
- return areRegExpsEqual(a, b, state);
64
- }
65
- if (tag === MAP_TAG) {
66
- return areMapsEqual(a, b, state);
67
- }
68
- if (tag === SET_TAG) {
69
- return areSetsEqual(a, b, state);
70
- }
71
- // If a simple object tag, then we can prioritize a simple object comparison because
72
- // it is likely a custom class.
73
- if (tag === OBJECT_TAG) {
74
- // The exception for value comparison is `Promise`-like contracts. These should be
75
- // treated the same as standard `Promise` objects, which means strict equality, and if
76
- // it reaches this point then that strict equality comparison has already failed.
77
- return (typeof a.then !== 'function' &&
78
- typeof b.then !== 'function' &&
79
- areObjectsEqual(a, b, state));
80
- }
81
- // If an arguments tag, it should be treated as a standard object.
82
- if (tag === ARGUMENTS_TAG) {
83
- return areObjectsEqual(a, b, state);
84
- }
85
- // As the penultimate fallback, check if the values passed are primitive wrappers. This
86
- // is very rare in modern JS, which is why it is deprioritized compared to all other object
87
- // types.
88
- if (tag === BOOLEAN_TAG || tag === NUMBER_TAG || tag === STRING_TAG) {
89
- return arePrimitiveWrappersEqual(a, b, state);
90
- }
91
- // If not matching any tags that require a specific type of comparison, then we hard-code false because
92
- // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
93
- // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
94
- // comparison that can be made.
95
- // - For types that can be introspected, but rarely have requirements to be compared
96
- // (`ArrayBuffer`, `DataView`, etc.), the cost is avoided to prioritize the common
97
- // use-cases (may be included in a future release, if requested enough).
98
- // - For types that can be introspected but do not have an objective definition of what
99
- // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
100
- // In all cases, these decisions should be reevaluated based on changes to the language and
101
- // common development practices.
102
- return false;
103
- };
104
- }
105
-
106
1
  var getOwnPropertyNames = Object.getOwnPropertyNames, getOwnPropertySymbols = Object.getOwnPropertySymbols;
107
2
  var hasOwnProperty = Object.prototype.hasOwnProperty;
3
+ var SUPPORTS_ARRAY_BUFFER = typeof ArrayBuffer === 'function' && Boolean(ArrayBuffer.isView);
4
+ /**
5
+ * Combine two comparators into a single comparators.
6
+ */
108
7
  function combineComparators(comparatorA, comparatorB) {
109
8
  return function isEqual(a, b, state) {
110
9
  return comparatorA(a, b, state) && comparatorB(a, b, state);
@@ -143,13 +42,23 @@ function createIsCircular(areItemsEqual) {
143
42
  return result;
144
43
  };
145
44
  }
45
+ /**
46
+ * Get the properties to strictly examine, which include both own properties that are
47
+ * not enumerable and symbol properties.
48
+ */
146
49
  function getStrictProperties(object) {
147
50
  return getOwnPropertyNames(object).concat(getOwnPropertySymbols(object));
148
51
  }
52
+ /**
53
+ * Whether the object contains the property passed as an own property.
54
+ */
149
55
  var hasOwn = Object.hasOwn ||
150
56
  (function (object, property) {
151
57
  return hasOwnProperty.call(object, property);
152
58
  });
59
+ function isArrayBuffer(value) {
60
+ return SUPPORTS_ARRAY_BUFFER && ArrayBuffer.isView(value);
61
+ }
153
62
  /**
154
63
  * Whether the values passed are strictly equal or both NaN.
155
64
  */
@@ -176,10 +85,6 @@ function areArraysEqual(a, b, state) {
176
85
  }
177
86
  /**
178
87
  * Whether the dates passed are equal in value.
179
- *
180
- * @NOTE
181
- * This is a standalone function instead of done inline in the comparator
182
- * to allow for overrides.
183
88
  */
184
89
  function areDatesEqual(a, b) {
185
90
  return sameValueZeroEqual(a.getTime(), b.getTime());
@@ -291,21 +196,12 @@ function areObjectsEqualStrict(a, b, state) {
291
196
  }
292
197
  /**
293
198
  * Whether the primitive wrappers passed are equal in value.
294
- *
295
- * @NOTE
296
- * This is a standalone function instead of done inline in the comparator
297
- * to allow for overrides.
298
199
  */
299
200
  function arePrimitiveWrappersEqual(a, b) {
300
201
  return sameValueZeroEqual(a.valueOf(), b.valueOf());
301
202
  }
302
203
  /**
303
204
  * Whether the regexps passed are equal in value.
304
- *
305
- * @NOTE
306
- * This is a standalone function instead of done inline in the comparator
307
- * to allow for overrides. An example of this would be supporting a
308
- * pre-ES2015 environment where the `flags` property is not available.
309
205
  */
310
206
  function areRegExpsEqual(a, b) {
311
207
  return a.source === b.source && a.flags === b.flags;
@@ -341,95 +237,218 @@ function areSetsEqual(a, b, state) {
341
237
  }
342
238
  return isValueEqual;
343
239
  }
240
+ function areTypedArraysEqual(a, b) {
241
+ var index = a.length;
242
+ if (b.length !== index) {
243
+ return false;
244
+ }
245
+ while (index-- > 0) {
246
+ if (a[index] !== b[index]) {
247
+ return false;
248
+ }
249
+ }
250
+ return true;
251
+ }
344
252
 
253
+ var ARGUMENTS_TAG = '[object Arguments]';
254
+ var BOOLEAN_TAG = '[object Boolean]';
255
+ var DATE_TAG = '[object Date]';
256
+ var MAP_TAG = '[object Map]';
257
+ var NUMBER_TAG = '[object Number]';
258
+ var OBJECT_TAG = '[object Object]';
259
+ var REG_EXP_TAG = '[object RegExp]';
260
+ var SET_TAG = '[object Set]';
261
+ var STRING_TAG = '[object String]';
262
+ var isArray = Array.isArray;
263
+ var assign = Object.assign;
264
+ var getTag = Object.prototype.toString.call.bind(Object.prototype.toString);
265
+ /**
266
+ * Create a comparator method based on the type-specific equality comparators passed.
267
+ */
268
+ function createComparator(_a) {
269
+ var areArraysEqual = _a.areArraysEqual, areDatesEqual = _a.areDatesEqual, areMapsEqual = _a.areMapsEqual, areObjectsEqual = _a.areObjectsEqual, arePrimitiveWrappersEqual = _a.arePrimitiveWrappersEqual, areRegExpsEqual = _a.areRegExpsEqual, areSetsEqual = _a.areSetsEqual, areTypedArraysEqual = _a.areTypedArraysEqual;
270
+ /**
271
+ * compare the value of the two objects and return true if they are equivalent in values
272
+ */
273
+ return function comparator(a, b, state) {
274
+ // If the items are strictly equal, no need to do a value comparison.
275
+ if (a === b) {
276
+ return true;
277
+ }
278
+ // If the items are not non-nullish objects, then the only possibility
279
+ // of them being equal but not strictly is if they are both `NaN`. Since
280
+ // `NaN` is uniquely not equal to itself, we can use self-comparison of
281
+ // both objects, which is faster than `isNaN()`.
282
+ if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
283
+ return a !== a && b !== b;
284
+ }
285
+ // Checks are listed in order of commonality of use-case:
286
+ // 1. Common complex object types (plain object, array)
287
+ // 2. Common data values (date, regexp)
288
+ // 3. Less-common complex object types (map, set)
289
+ // 4. Less-common data values (promise, primitive wrappers)
290
+ // Inherently this is both subjective and assumptive, however
291
+ // when reviewing comparable libraries in the wild this order
292
+ // appears to be generally consistent.
293
+ // `isPlainObject` only checks against the object's own realm. Cross-realm
294
+ // comparisons are rare, and will be handled in the ultimate fallback, so
295
+ // we can avoid the `toString.call()` cost unless necessary.
296
+ if (a.constructor === Object && b.constructor === Object) {
297
+ return areObjectsEqual(a, b, state);
298
+ }
299
+ // In strict mode, constructors should match. We placed this after the plain object check
300
+ // because the constructors must match to meet plain object requirements (must both be `Object`
301
+ // in the given Realm), so it slightly improves performance on a very common use-case.
302
+ if (state.strict && a.constructor !== b.constructor) {
303
+ return false;
304
+ }
305
+ // `isArray()` works on subclasses and is cross-realm, so we can again avoid
306
+ // the `toString.call()` cost unless necessary by just checking if either
307
+ // and then both are arrays.
308
+ var aArray = isArray(a);
309
+ var bArray = isArray(b);
310
+ if (aArray || bArray) {
311
+ return aArray === bArray && areArraysEqual(a, b, state);
312
+ }
313
+ aArray = isArrayBuffer(a);
314
+ bArray = isArrayBuffer(b);
315
+ if (aArray || bArray) {
316
+ return aArray === bArray && areTypedArraysEqual(a, b, state);
317
+ }
318
+ // Since this is a custom object, use the classic `toString.call()` to get its
319
+ // type. This is reasonably performant in modern environments like v8 and
320
+ // SpiderMonkey, and allows for cross-realm comparison when other checks like
321
+ // `instanceof` do not.
322
+ var tag = getTag(a);
323
+ if (tag !== getTag(b)) {
324
+ return false;
325
+ }
326
+ if (tag === DATE_TAG) {
327
+ return areDatesEqual(a, b, state);
328
+ }
329
+ if (tag === REG_EXP_TAG) {
330
+ return areRegExpsEqual(a, b, state);
331
+ }
332
+ if (tag === MAP_TAG) {
333
+ return areMapsEqual(a, b, state);
334
+ }
335
+ if (tag === SET_TAG) {
336
+ return areSetsEqual(a, b, state);
337
+ }
338
+ // If a simple object tag, then we can prioritize a simple object comparison because
339
+ // it is likely a custom class.
340
+ if (tag === OBJECT_TAG) {
341
+ // The exception for value comparison is `Promise`-like contracts. These should be
342
+ // treated the same as standard `Promise` objects, which means strict equality, and if
343
+ // it reaches this point then that strict equality comparison has already failed.
344
+ return (typeof a.then !== 'function' &&
345
+ typeof b.then !== 'function' &&
346
+ areObjectsEqual(a, b, state));
347
+ }
348
+ // If an arguments tag, it should be treated as a standard object.
349
+ if (tag === ARGUMENTS_TAG) {
350
+ return areObjectsEqual(a, b, state);
351
+ }
352
+ // As the penultimate fallback, check if the values passed are primitive wrappers. This
353
+ // is very rare in modern JS, which is why it is deprioritized compared to all other object
354
+ // types.
355
+ if (tag === BOOLEAN_TAG || tag === NUMBER_TAG || tag === STRING_TAG) {
356
+ return arePrimitiveWrappersEqual(a, b, state);
357
+ }
358
+ // If not matching any tags that require a specific type of comparison, then we hard-code false because
359
+ // the only thing remaining is strict equality, which has already been compared. This is for a few reasons:
360
+ // - Certain types that cannot be introspected (e.g., `WeakMap`). For these types, this is the only
361
+ // comparison that can be made.
362
+ // - For types that can be introspected, but rarely have requirements to be compared
363
+ // (`ArrayBuffer`, `DataView`, etc.), the cost is avoided to prioritize the common
364
+ // use-cases (may be included in a future release, if requested enough).
365
+ // - For types that can be introspected but do not have an objective definition of what
366
+ // equality is (`Error`, etc.), the subjective decision is to be conservative and strictly compare.
367
+ // In all cases, these decisions should be reevaluated based on changes to the language and
368
+ // common development practices.
369
+ return false;
370
+ };
371
+ }
372
+ /**
373
+ * Create the configuration object used for building comparators.
374
+ */
345
375
  function createComparatorConfig(_a) {
346
- var circular = _a.circular, strict = _a.strict;
376
+ var circular = _a.circular, createCustomConfig = _a.createCustomConfig, strict = _a.strict;
347
377
  var config = {
348
- areArraysEqual: areArraysEqual,
378
+ areArraysEqual: strict
379
+ ? areObjectsEqualStrict
380
+ : areArraysEqual,
349
381
  areDatesEqual: areDatesEqual,
350
- areMapsEqual: areMapsEqual,
351
- areObjectsEqual: areObjectsEqual,
382
+ areMapsEqual: strict
383
+ ? combineComparators(areMapsEqual, areObjectsEqualStrict)
384
+ : areMapsEqual,
385
+ areObjectsEqual: strict
386
+ ? areObjectsEqualStrict
387
+ : areObjectsEqual,
352
388
  arePrimitiveWrappersEqual: arePrimitiveWrappersEqual,
353
389
  areRegExpsEqual: areRegExpsEqual,
354
- areSetsEqual: areSetsEqual,
390
+ areSetsEqual: strict
391
+ ? combineComparators(areSetsEqual, areObjectsEqualStrict)
392
+ : areSetsEqual,
393
+ areTypedArraysEqual: strict
394
+ ? combineComparators(areTypedArraysEqual, areObjectsEqualStrict)
395
+ : areTypedArraysEqual,
355
396
  };
356
- if (strict) {
357
- config.areArraysEqual = areObjectsEqual;
358
- config.areMapsEqual = combineComparators(areMapsEqual, areObjectsEqual);
359
- config.areObjectsEqual = areObjectsEqualStrict;
360
- config.areSetsEqual = combineComparators(areSetsEqual, areObjectsEqual);
397
+ if (createCustomConfig) {
398
+ config = assign({}, config, createCustomConfig(config));
361
399
  }
362
400
  if (circular) {
363
- config.areArraysEqual = createIsCircular(config.areArraysEqual);
364
- config.areMapsEqual = createIsCircular(config.areMapsEqual);
365
- config.areObjectsEqual = createIsCircular(config.areObjectsEqual);
366
- config.areSetsEqual = createIsCircular(config.areSetsEqual);
401
+ var areArraysEqual$1 = createIsCircular(config.areArraysEqual);
402
+ var areMapsEqual$1 = createIsCircular(config.areMapsEqual);
403
+ var areObjectsEqual$1 = createIsCircular(config.areObjectsEqual);
404
+ var areSetsEqual$1 = createIsCircular(config.areSetsEqual);
405
+ config = assign({}, config, {
406
+ areArraysEqual: areArraysEqual$1,
407
+ areMapsEqual: areMapsEqual$1,
408
+ areObjectsEqual: areObjectsEqual$1,
409
+ areSetsEqual: areSetsEqual$1,
410
+ });
367
411
  }
368
412
  return config;
369
413
  }
370
- function createDefaultEqualCreator(options) {
371
- if (options === void 0) { options = {}; }
372
- var config = createComparatorConfig(options);
373
- var isEqual = createComparator(config);
374
- var isEqualComparator = options.comparator || createInternalComparator(isEqual);
375
- var strict = !!options.strict;
376
- if (options.circular) {
377
- return function equals(a, b) {
378
- return isEqual(a, b, {
379
- cache: new WeakMap(),
380
- equals: isEqualComparator,
381
- meta: undefined,
382
- strict: strict,
383
- });
384
- };
385
- }
386
- var state = Object.freeze({
387
- cache: undefined,
388
- equals: isEqualComparator,
389
- meta: undefined,
390
- strict: strict,
391
- });
392
- return function equals(a, b) {
393
- return isEqual(a, b, state);
394
- };
395
- }
414
+
396
415
  /**
397
416
  * Whether the items passed are deeply-equal in value.
398
417
  */
399
- var deepEqual = createDefaultEqualCreator();
418
+ var deepEqual = createCustomEqual();
400
419
  /**
401
420
  * Whether the items passed are deeply-equal in value based on strict comparison.
402
421
  */
403
- var strictDeepEqual = createDefaultEqualCreator({ strict: true });
422
+ var strictDeepEqual = createCustomEqual({ strict: true });
404
423
  /**
405
424
  * Whether the items passed are deeply-equal in value, including circular references.
406
425
  */
407
- var circularDeepEqual = createDefaultEqualCreator({ circular: true });
426
+ var circularDeepEqual = createCustomEqual({ circular: true });
408
427
  /**
409
428
  * Whether the items passed are deeply-equal in value, including circular references,
410
429
  * based on strict comparison.
411
430
  */
412
- var strictCircularDeepEqual = createDefaultEqualCreator({
431
+ var strictCircularDeepEqual = createCustomEqual({
413
432
  circular: true,
414
433
  strict: true,
415
434
  });
416
435
  /**
417
436
  * Whether the items passed are shallowly-equal in value.
418
437
  */
419
- var shallowEqual = createDefaultEqualCreator({
438
+ var shallowEqual = createCustomEqual({
420
439
  comparator: sameValueZeroEqual,
421
440
  });
422
441
  /**
423
442
  * Whether the items passed are shallowly-equal in value based on strict comparison
424
443
  */
425
- var strictShallowEqual = createDefaultEqualCreator({
444
+ var strictShallowEqual = createCustomEqual({
426
445
  comparator: sameValueZeroEqual,
427
446
  strict: true,
428
447
  });
429
448
  /**
430
449
  * Whether the items passed are shallowly-equal in value, including circular references.
431
450
  */
432
- var circularShallowEqual = createDefaultEqualCreator({
451
+ var circularShallowEqual = createCustomEqual({
433
452
  comparator: sameValueZeroEqual,
434
453
  circular: true,
435
454
  });
@@ -437,7 +456,7 @@ var circularShallowEqual = createDefaultEqualCreator({
437
456
  * Whether the items passed are shallowly-equal in value, including circular references,
438
457
  * based on strict comparison.
439
458
  */
440
- var strictCircularShallowEqual = createDefaultEqualCreator({
459
+ var strictCircularShallowEqual = createCustomEqual({
441
460
  comparator: sameValueZeroEqual,
442
461
  circular: true,
443
462
  strict: true,
@@ -452,35 +471,31 @@ var strictCircularShallowEqual = createDefaultEqualCreator({
452
471
  */
453
472
  function createCustomEqual(options) {
454
473
  if (options === void 0) { options = {}; }
455
- var comparator = options.comparator, createCustomInternalComparator = options.createInternalComparator, createCustomConfig = options.createCustomConfig, createState = options.createState;
456
- var baseConfig = createComparatorConfig(options);
457
- var config = createCustomConfig
458
- ? Object.assign({}, baseConfig, createCustomConfig(baseConfig))
459
- : baseConfig;
460
- var isEqualCustom = comparator || createComparator(config);
461
- var isEqualCustomComparator = createCustomInternalComparator
462
- ? createCustomInternalComparator(isEqualCustom)
463
- : createInternalComparator(isEqualCustom);
464
- var strict = !!options.strict;
474
+ var circular = options.circular, comparator = options.comparator, createCustomInternalComparator = options.createInternalComparator, createState = options.createState, _a = options.strict, baseStrict = _a === void 0 ? false : _a;
475
+ var config = createComparatorConfig(options);
476
+ var isEqualCustom = createComparator(config);
477
+ var isEqualCustomComparator = comparator ||
478
+ (createCustomInternalComparator
479
+ ? createCustomInternalComparator(isEqualCustom)
480
+ : createInternalComparator(isEqualCustom));
465
481
  if (createState) {
466
482
  return function isEqual(a, b, metaOverride) {
467
- var customState = createState(isEqualCustom);
483
+ var _a = createState(isEqualCustom), _b = _a.cache, cache = _b === void 0 ? circular ? new WeakMap() : undefined : _b, _c = _a.equals, equals = _c === void 0 ? isEqualCustomComparator : _c, meta = _a.meta, _d = _a.strict, strict = _d === void 0 ? baseStrict : _d;
468
484
  return isEqualCustom(a, b, {
469
- cache: customState.cache || new WeakMap(),
470
- equals: customState.equals || isEqualCustomComparator,
471
- // @ts-expect-error - inferred `Meta` may be undefined, which is okay
472
- meta: metaOverride !== undefined ? metaOverride : customState.meta,
473
- strict: customState.strict !== undefined ? customState.strict : strict,
485
+ cache: cache,
486
+ equals: equals,
487
+ meta: metaOverride !== undefined ? metaOverride : meta,
488
+ strict: strict,
474
489
  });
475
490
  };
476
491
  }
477
- if (options.circular) {
492
+ if (circular) {
478
493
  return function equals(a, b) {
479
494
  return isEqualCustom(a, b, {
480
495
  cache: new WeakMap(),
481
496
  equals: isEqualCustomComparator,
482
497
  meta: undefined,
483
- strict: strict,
498
+ strict: baseStrict,
484
499
  });
485
500
  };
486
501
  }
@@ -488,7 +503,7 @@ function createCustomEqual(options) {
488
503
  cache: undefined,
489
504
  equals: isEqualCustomComparator,
490
505
  meta: undefined,
491
- strict: strict,
506
+ strict: baseStrict,
492
507
  });
493
508
  return function equals(a, b) {
494
509
  return isEqualCustom(a, b, state);