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
@@ -1,4 +1,4 @@
1
- import type { Dictionary, State, TypedArray } from './internalTypes';
1
+ import type { Dictionary, PrimitiveWrapper, State, TypedArray } from './internalTypes';
2
2
  /**
3
3
  * Whether the arrays are equal in value.
4
4
  */
@@ -22,7 +22,7 @@ export declare function areObjectsEqualStrict(a: Dictionary, b: Dictionary, stat
22
22
  /**
23
23
  * Whether the primitive wrappers passed are equal in value.
24
24
  */
25
- export declare function arePrimitiveWrappersEqual(a: Date, b: Date): boolean;
25
+ export declare function arePrimitiveWrappersEqual(a: PrimitiveWrapper, b: PrimitiveWrapper): boolean;
26
26
  /**
27
27
  * Whether the regexps passed are equal in value.
28
28
  */
@@ -31,4 +31,7 @@ export declare function areRegExpsEqual(a: RegExp, b: RegExp): boolean;
31
31
  * Whether the `Set`s are equal in value.
32
32
  */
33
33
  export declare function areSetsEqual(a: Set<any>, b: Set<any>, state: State<any>): boolean;
34
+ /**
35
+ * Whether the TypedArray instances are equal in value.
36
+ */
34
37
  export declare function areTypedArraysEqual(a: TypedArray, b: TypedArray): boolean;
@@ -5,37 +5,37 @@ export * from './internalTypes';
5
5
  /**
6
6
  * Whether the items passed are deeply-equal in value.
7
7
  */
8
- export declare const deepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
8
+ export declare const deepEqual: <A, B>(a: A, b: B) => boolean;
9
9
  /**
10
10
  * Whether the items passed are deeply-equal in value based on strict comparison.
11
11
  */
12
- export declare const strictDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
12
+ export declare const strictDeepEqual: <A, B>(a: A, b: B) => boolean;
13
13
  /**
14
14
  * Whether the items passed are deeply-equal in value, including circular references.
15
15
  */
16
- export declare const circularDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
16
+ export declare const circularDeepEqual: <A, B>(a: A, b: B) => boolean;
17
17
  /**
18
18
  * Whether the items passed are deeply-equal in value, including circular references,
19
19
  * based on strict comparison.
20
20
  */
21
- export declare const strictCircularDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
21
+ export declare const strictCircularDeepEqual: <A, B>(a: A, b: B) => boolean;
22
22
  /**
23
23
  * Whether the items passed are shallowly-equal in value.
24
24
  */
25
- export declare const shallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
25
+ export declare const shallowEqual: <A, B>(a: A, b: B) => boolean;
26
26
  /**
27
27
  * Whether the items passed are shallowly-equal in value based on strict comparison
28
28
  */
29
- export declare const strictShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
29
+ export declare const strictShallowEqual: <A, B>(a: A, b: B) => boolean;
30
30
  /**
31
31
  * Whether the items passed are shallowly-equal in value, including circular references.
32
32
  */
33
- export declare const circularShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
33
+ export declare const circularShallowEqual: <A, B>(a: A, b: B) => boolean;
34
34
  /**
35
35
  * Whether the items passed are shallowly-equal in value, including circular references,
36
36
  * based on strict comparison.
37
37
  */
38
- export declare const strictCircularShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
38
+ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
39
39
  /**
40
40
  * Create a custom equality comparison method.
41
41
  *
@@ -44,4 +44,4 @@ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B, metaOverride
44
44
  * support for legacy environments that do not support expected features like
45
45
  * `RegExp.prototype.flags` out of the box.
46
46
  */
47
- export declare function createCustomEqual<Meta>(options?: CustomEqualCreatorOptions<Meta>): <A, B>(a: A, b: B, metaOverride?: Meta | undefined) => boolean;
47
+ export declare function createCustomEqual<Meta = undefined>(options?: CustomEqualCreatorOptions<Meta>): <A, B>(a: A, b: B) => boolean;
@@ -1,46 +1,134 @@
1
- export interface BaseCircular extends Pick<WeakMap<any, any>, 'delete' | 'get'> {
2
- 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;
3
9
  }
4
- export type State<Meta> = CircularState<Meta> | DefaultState<Meta>;
5
- export interface CircularState<Meta> {
6
- readonly cache: BaseCircular;
10
+ export interface State<Meta> {
11
+ /**
12
+ * Cache used to identify circular references
13
+ */
14
+ readonly cache: Cache<any, any> | undefined;
15
+ /**
16
+ * Method used to determine equality of nested value.
17
+ */
7
18
  readonly equals: InternalEqualityComparator<Meta>;
19
+ /**
20
+ * Additional value that can be used for comparisons.
21
+ */
8
22
  meta: Meta;
23
+ /**
24
+ * Whether the equality comparison is strict, meaning it matches
25
+ * all properties (including symbols and non-enumerable properties)
26
+ * with equal shape of descriptors.
27
+ */
9
28
  readonly strict: boolean;
10
29
  }
11
- export interface DefaultState<Meta> {
30
+ export interface CircularState<Meta> extends State<Meta> {
31
+ readonly cache: Cache<any, any>;
32
+ }
33
+ export interface DefaultState<Meta> extends State<Meta> {
12
34
  readonly cache: undefined;
13
- readonly equals: InternalEqualityComparator<Meta>;
14
- meta: Meta;
15
- readonly strict: boolean;
16
35
  }
17
36
  export interface Dictionary<Value = any> {
18
37
  [key: string | symbol]: Value;
19
38
  $$typeof?: any;
20
39
  }
21
40
  export interface ComparatorConfig<Meta> {
41
+ /**
42
+ * Whether the arrays passed are equal in value. In strict mode, this includes
43
+ * additional properties added to the array.
44
+ */
22
45
  areArraysEqual: TypeEqualityComparator<any, Meta>;
46
+ /**
47
+ * Whether the dates passed are equal in value.
48
+ */
23
49
  areDatesEqual: TypeEqualityComparator<any, Meta>;
50
+ /**
51
+ * Whether the maps passed are equal in value. In strict mode, this includes
52
+ * additional properties added to the map.
53
+ */
24
54
  areMapsEqual: TypeEqualityComparator<any, Meta>;
55
+ /**
56
+ * Whether the objects passed are equal in value. In strict mode, this includes
57
+ * non-enumerable properties added to the map, as well as symbol properties.
58
+ */
25
59
  areObjectsEqual: TypeEqualityComparator<any, Meta>;
60
+ /**
61
+ * Whether the primitive wrappers passed are equal in value.
62
+ */
26
63
  arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
64
+ /**
65
+ * Whether the regexps passed are equal in value.
66
+ */
27
67
  areRegExpsEqual: TypeEqualityComparator<any, Meta>;
68
+ /**
69
+ * Whether the sets passed are equal in value. In strict mode, this includes
70
+ * additional properties added to the set.
71
+ */
28
72
  areSetsEqual: TypeEqualityComparator<any, Meta>;
73
+ /**
74
+ * Whether the typed arrays passed are equal in value. In strict mode, this includes
75
+ * additional properties added to the typed array.
76
+ */
29
77
  areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
30
78
  }
31
79
  export type CreateCustomComparatorConfig<Meta> = (config: ComparatorConfig<Meta>) => Partial<ComparatorConfig<Meta>>;
32
- export type CreateState<Meta> = (comparator: EqualityComparator<Meta>) => Partial<State<Meta>>;
80
+ export type CreateState<Meta> = () => {
81
+ cache?: Cache<any, any> | undefined;
82
+ meta?: Meta;
83
+ };
33
84
  export type EqualityComparator<Meta> = <A, B>(a: A, b: B, state: State<Meta>) => boolean;
34
85
  export type AnyEqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
35
86
  export type EqualityComparatorCreator<Meta> = (fn: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
36
87
  export type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
88
+ export type PrimitiveWrapper = Boolean | Number | String;
89
+ /**
90
+ * Type which encompasses possible instances of TypedArray
91
+ * classes.
92
+ *
93
+ * **NOTE**: This does not include `BigInt64Array` and
94
+ * `BitUint64Array` because those are part of ES2020 and
95
+ * not supported by certain TS configurations. If using
96
+ * either in `areTypedArraysEqual`, you can cast the
97
+ * instance as `TypedArray` and it will work as expected,
98
+ * because runtime checks will still work for those classes.
99
+ */
37
100
  export type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint16Array | Uint32Array | Uint8Array | Uint8ClampedArray;
38
101
  export type TypeEqualityComparator<Type, Meta = undefined> = (a: Type, b: Type, state: State<Meta>) => boolean;
39
102
  export interface CustomEqualCreatorOptions<Meta> {
103
+ /**
104
+ * Whether circular references should be supported. It causes the
105
+ * comparison to be slower, but for objects that have circular references
106
+ * it is required to avoid stack overflows.
107
+ */
40
108
  circular?: boolean;
41
- comparator?: EqualityComparator<Meta>;
109
+ /**
110
+ * Create a custom configuration of type-specific equality comparators.
111
+ * This receives the default configuration, which allows either replacement
112
+ * or supersetting of the default methods.
113
+ */
42
114
  createCustomConfig?: CreateCustomComparatorConfig<Meta>;
43
- createInternalComparator?: <Meta>(compare: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
115
+ /**
116
+ * Create a custom internal comparator, which is used as an override to the
117
+ * default entry point for nested value equality comparisons. This is often
118
+ * used for doing custom logic for specific types (such as handling a specific
119
+ * class instance differently than other objects) or to incorporate `meta` in
120
+ * the comparison. See the recipes for examples.
121
+ */
122
+ createInternalComparator?: (compare: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
123
+ /**
124
+ * Create a custom `state` object passed between the methods. This allows for
125
+ * custom `cache` and/or `meta` values to be used.
126
+ */
44
127
  createState?: CreateState<Meta>;
128
+ /**
129
+ * Whether the equality comparison is strict, meaning it matches
130
+ * all properties (including symbols and non-enumerable properties)
131
+ * with equal shape of descriptors.
132
+ */
45
133
  strict?: boolean;
46
134
  }
@@ -1,13 +1,8 @@
1
- import { AnyEqualityComparator, Dictionary, EqualityComparator, InternalEqualityComparator, State, TypeEqualityComparator } from './internalTypes';
1
+ import { AnyEqualityComparator, Dictionary, State, TypeEqualityComparator } from './internalTypes';
2
2
  /**
3
3
  * Combine two comparators into a single comparators.
4
4
  */
5
5
  export declare function combineComparators<Meta>(comparatorA: AnyEqualityComparator<Meta>, comparatorB: AnyEqualityComparator<Meta>): <A, B>(a: A, b: B, state: State<Meta>) => boolean;
6
- /**
7
- * Default equality comparator pass-through, used as the standard `isEqual` creator for
8
- * use inside the built comparator.
9
- */
10
- export declare function createInternalComparator<Meta>(compare: EqualityComparator<Meta>): InternalEqualityComparator<Meta>;
11
6
  /**
12
7
  * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
13
8
  * for circular references to be safely included in the comparison without creating
package/dist/min/index.js CHANGED
@@ -1 +1 @@
1
- !function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self)["fast-equals"]={})}(this,(function(r){"use strict";var e=Object.getOwnPropertyNames,t=Object.getOwnPropertySymbols,a=Object.prototype.hasOwnProperty;function u(r,e){return function(t,a,u){return r(t,a,u)&&e(t,a,u)}}function n(r){return function(e,t,a){if(!e||!t||"object"!=typeof e||"object"!=typeof t)return r(e,t,a);var u=a.cache,n=u.get(e),o=u.get(t);if(n&&o)return n===t&&o===e;u.set(e,t),u.set(t,e);var c=r(e,t,a);return u.delete(e),u.delete(t),c}}function o(r){return e(r).concat(t(r))}var c=Object.hasOwn||function(r,e){return a.call(r,e)};function i(r,e){return r||e?r===e:r===e||r!=r&&e!=e}var f="_owner",l=Object.getOwnPropertyDescriptor,s=Object.keys;function p(r,e,t){var a=r.length;if(e.length!==a)return!1;for(;a-- >0;)if(!t.equals(r[a],e[a],a,a,r,e,t))return!1;return!0}function q(r,e){return i(r.getTime(),e.getTime())}function b(r,e,t){var a=r.size===e.size;if(a&&r.size){var u={},n=0;r.forEach((function(o,c){if(a){var i=!1,f=0;e.forEach((function(a,l){i||u[f]||!(i=t.equals(c,l,n,f,r,e,t)&&t.equals(o,a,c,l,r,e,t))||(u[f]=!0),f++})),n++,a=i}}))}return a}function y(r,e,t){var a,u=s(r),n=u.length;if(s(e).length!==n)return!1;for(;n-- >0;){if((a=u[n])===f&&(r.$$typeof||e.$$typeof)&&r.$$typeof!==e.$$typeof)return!1;if(!c(e,a)||!t.equals(r[a],e[a],a,a,r,e,t))return!1}return!0}function E(r,e,t){var a,u,n,i=o(r),s=i.length;if(o(e).length!==s)return!1;for(;s-- >0;){if((a=i[s])===f&&(r.$$typeof||e.$$typeof)&&r.$$typeof!==e.$$typeof)return!1;if(!c(e,a))return!1;if(!t.equals(r[a],e[a],a,a,r,e,t))return!1;if(u=l(r,a),n=l(e,a),(u||n)&&(!u||!n||u.configurable!==n.configurable||u.enumerable!==n.enumerable||u.writable!==n.writable))return!1}return!0}function v(r,e){return i(r.valueOf(),e.valueOf())}function j(r,e){return r.source===e.source&&r.flags===e.flags}function g(r,e,t){var a=r.size===e.size;if(a&&r.size){var u={};r.forEach((function(n,o){if(a){var c=!1,i=0;e.forEach((function(a,f){c||u[i]||!(c=t.equals(n,a,o,f,r,e,t))||(u[i]=!0),i++})),a=c}}))}return a}function h(r,e){var t=r.length;if(e.length!==t)return!1;for(;t-- >0;)if(r[t]!==e[t])return!1;return!0}var d="[object Arguments]",m="[object Boolean]",O="[object Date]",w="[object Map]",$="[object Number]",S="[object Object]",A="[object RegExp]",z="[object Set]",D="[object String]",M=Array.isArray,x="function"==typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView:null,C=Object.assign,P=Object.prototype.toString.call.bind(Object.prototype.toString);var T=Z(),B=Z({strict:!0}),W=Z({circular:!0}),k=Z({circular:!0,strict:!0}),R=Z({comparator:i}),V=Z({comparator:i,strict:!0}),N=Z({comparator:i,circular:!0}),I=Z({comparator:i,circular:!0,strict:!0});function Z(r){void 0===r&&(r={});var e,t=r.circular,a=r.comparator,o=r.createInternalComparator,c=r.createState,i=r.strict,f=void 0!==i&&i,l=function(r){var e=r.circular,t=r.createCustomConfig,a=r.strict,o={areArraysEqual:a?E:p,areDatesEqual:q,areMapsEqual:a?u(b,E):b,areObjectsEqual:a?E:y,arePrimitiveWrappersEqual:v,areRegExpsEqual:j,areSetsEqual:a?u(g,E):g,areTypedArraysEqual:a?u(h,E):h};if(t&&(o=C({},o,t(o))),e){var c=n(o.areArraysEqual),i=n(o.areMapsEqual),f=n(o.areObjectsEqual),l=n(o.areSetsEqual);o=C({},o,{areArraysEqual:c,areMapsEqual:i,areObjectsEqual:f,areSetsEqual:l})}return o}(r),s=function(r){var e=r.areArraysEqual,t=r.areDatesEqual,a=r.areMapsEqual,u=r.areObjectsEqual,n=r.arePrimitiveWrappersEqual,o=r.areRegExpsEqual,c=r.areSetsEqual,i=r.areTypedArraysEqual;return function(r,f,l){if(r===f)return!0;if(!r||!f||"object"!=typeof r||"object"!=typeof f)return r!=r&&f!=f;if(r.constructor===Object&&f.constructor===Object)return u(r,f,l);if(l.strict&&r.constructor!==f.constructor)return!1;var s=M(r),p=M(f);if(s||p)return s===p&&e(r,f,l);if(x&&(s=x(r),p=x(f),s||p))return s===p&&i(r,f,l);var q=P(r);return q===P(f)&&(q===O?t(r,f,l):q===A?o(r,f,l):q===w?a(r,f,l):q===z?c(r,f,l):q===S?"function"!=typeof r.then&&"function"!=typeof f.then&&u(r,f,l):q===d?u(r,f,l):(q===m||q===$||q===D)&&n(r,f,l))}}(l),T=a||(o?o(s):(e=s,function(r,t,a,u,n,o,c){return e(r,t,c)}));if(c)return function(r,e,a){var u=c(s),n=u.cache,o=void 0===n?t?new WeakMap:void 0:n,i=u.equals,l=void 0===i?T:i,p=u.meta,q=u.strict;return s(r,e,{cache:o,equals:l,meta:void 0!==a?a:p,strict:void 0===q?f:q})};if(t)return function(r,e){return s(r,e,{cache:new WeakMap,equals:T,meta:void 0,strict:f})};var B=Object.freeze({cache:void 0,equals:T,meta:void 0,strict:f});return function(r,e){return s(r,e,B)}}r.circularDeepEqual=W,r.circularShallowEqual=N,r.createCustomEqual=Z,r.deepEqual=T,r.sameValueZeroEqual=i,r.shallowEqual=R,r.strictCircularDeepEqual=k,r.strictCircularShallowEqual=I,r.strictDeepEqual=B,r.strictShallowEqual=V}));
1
+ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self)["fast-equals"]={})}(this,(function(e){"use strict";var r=Object.getOwnPropertyNames,t=Object.getOwnPropertySymbols,a=Object.prototype.hasOwnProperty;function n(e,r){return function(t,a,n){return e(t,a,n)&&r(t,a,n)}}function u(e){return function(r,t,a){if(!r||!t||"object"!=typeof r||"object"!=typeof t)return e(r,t,a);var n=a.cache,u=n.get(r),o=n.get(t);if(u&&o)return u===t&&o===r;n.set(r,t),n.set(t,r);var i=e(r,t,a);return n.delete(r),n.delete(t),i}}function o(e){return r(e).concat(t(e))}var i=Object.hasOwn||function(e,r){return a.call(e,r)};function c(e,r){return e||r?e===r:e===r||e!=e&&r!=r}var l="_owner",f=Object.getOwnPropertyDescriptor,s=Object.keys;function p(e,r,t){var a=e.length;if(r.length!==a)return!1;for(;a-- >0;)if(!t.equals(e[a],r[a],a,a,e,r,t))return!1;return!0}function q(e,r){return c(e.getTime(),r.getTime())}function v(e,r,t){if(e.size!==r.size)return!1;for(var a,n,u={},o=e.entries(),i=0;(a=o.next())&&!a.done;){for(var c=r.entries(),l=!1,f=0;(n=c.next())&&!n.done;){var s=a.value,p=s[0],q=s[1],v=n.value,y=v[0],b=v[1];l||u[f]||!(l=t.equals(p,y,i,f,e,r,t)&&t.equals(q,b,p,y,e,r,t))||(u[f]=!0),f++}if(!l)return!1;i++}return!0}function y(e,r,t){var a,n=s(e),u=n.length;if(s(r).length!==u)return!1;for(;u-- >0;){if((a=n[u])===l&&(e.$$typeof||r.$$typeof)&&e.$$typeof!==r.$$typeof)return!1;if(!i(r,a)||!t.equals(e[a],r[a],a,a,e,r,t))return!1}return!0}function b(e,r,t){var a,n,u,c=o(e),s=c.length;if(o(r).length!==s)return!1;for(;s-- >0;){if((a=c[s])===l&&(e.$$typeof||r.$$typeof)&&e.$$typeof!==r.$$typeof)return!1;if(!i(r,a))return!1;if(!t.equals(e[a],r[a],a,a,e,r,t))return!1;if(n=f(e,a),u=f(r,a),(n||u)&&(!n||!u||n.configurable!==u.configurable||n.enumerable!==u.enumerable||n.writable!==u.writable))return!1}return!0}function E(e,r){return c(e.valueOf(),r.valueOf())}function g(e,r){return e.source===r.source&&e.flags===r.flags}function j(e,r,t){if(e.size!==r.size)return!1;for(var a,n,u={},o=e.values();(a=o.next())&&!a.done;){for(var i=r.values(),c=!1,l=0;(n=i.next())&&!n.done;)c||u[l]||!(c=t.equals(a.value,n.value,a.value,n.value,e,r,t))||(u[l]=!0),l++;if(!c)return!1}return!0}function d(e,r){var t=e.length;if(r.length!==t)return!1;for(;t-- >0;)if(e[t]!==r[t])return!1;return!0}var m="[object Arguments]",h="[object Boolean]",O="[object Date]",w="[object Map]",S="[object Number]",$="[object Object]",A="[object RegExp]",x="[object Set]",C="[object String]",D=Array.isArray,M="function"==typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView:null,P=Object.assign,T=Object.prototype.toString.call.bind(Object.prototype.toString);var I=Z(),z=Z({strict:!0}),B=Z({circular:!0}),R=Z({circular:!0,strict:!0}),W=Z({createInternalComparator:function(){return c}}),k=Z({strict:!0,createInternalComparator:function(){return c}}),V=Z({circular:!0,createInternalComparator:function(){return c}}),N=Z({circular:!0,createInternalComparator:function(){return c},strict:!0});function Z(e){void 0===e&&(e={});var r,t=e.circular,a=void 0!==t&&t,o=e.createInternalComparator,i=e.createState,c=e.strict,l=void 0!==c&&c,f=function(e){var r=e.circular,t=e.createCustomConfig,a=e.strict,o={areArraysEqual:a?b:p,areDatesEqual:q,areMapsEqual:a?n(v,b):v,areObjectsEqual:a?b:y,arePrimitiveWrappersEqual:E,areRegExpsEqual:g,areSetsEqual:a?n(j,b):j,areTypedArraysEqual:a?b:d};if(t&&(o=P({},o,t(o))),r){var i=u(o.areArraysEqual),c=u(o.areMapsEqual),l=u(o.areObjectsEqual),f=u(o.areSetsEqual);o=P({},o,{areArraysEqual:i,areMapsEqual:c,areObjectsEqual:l,areSetsEqual:f})}return o}(e),s=function(e){var r=e.areArraysEqual,t=e.areDatesEqual,a=e.areMapsEqual,n=e.areObjectsEqual,u=e.arePrimitiveWrappersEqual,o=e.areRegExpsEqual,i=e.areSetsEqual,c=e.areTypedArraysEqual;return function(e,l,f){if(e===l)return!0;if(null==e||null==l||"object"!=typeof e||"object"!=typeof l)return e!=e&&l!=l;var s=e.constructor;if(s!==l.constructor)return!1;if(s===Object)return n(e,l,f);if(D(e))return r(e,l,f);if(null!=M&&M(e))return c(e,l,f);if(s===Date)return t(e,l,f);if(s===RegExp)return o(e,l,f);if(s===Map)return a(e,l,f);if(s===Set)return i(e,l,f);var p=T(e);return p===O?t(e,l,f):p===A?o(e,l,f):p===w?a(e,l,f):p===x?i(e,l,f):p===$?"function"!=typeof e.then&&"function"!=typeof l.then&&n(e,l,f):p===m?n(e,l,f):(p===h||p===S||p===C)&&u(e,l,f)}}(f);return function(e){var r=e.circular,t=e.comparator,a=e.createState,n=e.equals,u=e.strict;if(a)return function(e,o){var i=a(),c=i.cache,l=void 0===c?r?new WeakMap:void 0:c,f=i.meta;return t(e,o,{cache:l,equals:n,meta:f,strict:u})};if(r)return function(e,r){return t(e,r,{cache:new WeakMap,equals:n,meta:void 0,strict:u})};var o={cache:void 0,equals:n,meta:void 0,strict:u};return function(e,r){return t(e,r,o)}}({circular:a,comparator:s,createState:i,equals:o?o(s):(r=s,function(e,t,a,n,u,o,i){return r(e,t,i)}),strict:l})}e.circularDeepEqual=B,e.circularShallowEqual=V,e.createCustomEqual=Z,e.deepEqual=I,e.sameValueZeroEqual=c,e.shallowEqual=W,e.strictCircularDeepEqual=R,e.strictCircularShallowEqual=N,e.strictDeepEqual=z,e.strictShallowEqual=k}));
@@ -1,9 +1,26 @@
1
- import type { ComparatorConfig, CustomEqualCreatorOptions, EqualityComparator } from './internalTypes';
1
+ import type { ComparatorConfig, CreateState, CustomEqualCreatorOptions, EqualityComparator, InternalEqualityComparator } from './internalTypes';
2
+ interface CreateIsEqualOptions<Meta> {
3
+ circular: boolean;
4
+ comparator: EqualityComparator<Meta>;
5
+ createState: CreateState<Meta> | undefined;
6
+ equals: InternalEqualityComparator<Meta>;
7
+ strict: boolean;
8
+ }
2
9
  /**
3
10
  * Create a comparator method based on the type-specific equality comparators passed.
4
11
  */
5
- export declare function createComparator<Meta>({ areArraysEqual, areDatesEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, }: ComparatorConfig<Meta>): EqualityComparator<Meta>;
12
+ export declare function createEqualityComparator<Meta>({ areArraysEqual, areDatesEqual, areMapsEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, }: ComparatorConfig<Meta>): EqualityComparator<Meta>;
6
13
  /**
7
14
  * Create the configuration object used for building comparators.
8
15
  */
9
- export declare function createComparatorConfig<Meta>({ circular, createCustomConfig, strict, }: CustomEqualCreatorOptions<Meta>): ComparatorConfig<Meta>;
16
+ export declare function createEqualityComparatorConfig<Meta>({ circular, createCustomConfig, strict, }: CustomEqualCreatorOptions<Meta>): ComparatorConfig<Meta>;
17
+ /**
18
+ * Default equality comparator pass-through, used as the standard `isEqual` creator for
19
+ * use inside the built comparator.
20
+ */
21
+ export declare function createInternalEqualityComparator<Meta>(compare: EqualityComparator<Meta>): InternalEqualityComparator<Meta>;
22
+ /**
23
+ * Create the `isEqual` function used by the consuming application.
24
+ */
25
+ export declare function createIsEqual<Meta>({ circular, comparator, createState, equals, strict, }: CreateIsEqualOptions<Meta>): <A, B>(a: A, b: B) => boolean;
26
+ export {};
@@ -1,4 +1,4 @@
1
- import type { Dictionary, State, TypedArray } from './internalTypes';
1
+ import type { Dictionary, PrimitiveWrapper, State, TypedArray } from './internalTypes';
2
2
  /**
3
3
  * Whether the arrays are equal in value.
4
4
  */
@@ -22,7 +22,7 @@ export declare function areObjectsEqualStrict(a: Dictionary, b: Dictionary, stat
22
22
  /**
23
23
  * Whether the primitive wrappers passed are equal in value.
24
24
  */
25
- export declare function arePrimitiveWrappersEqual(a: Date, b: Date): boolean;
25
+ export declare function arePrimitiveWrappersEqual(a: PrimitiveWrapper, b: PrimitiveWrapper): boolean;
26
26
  /**
27
27
  * Whether the regexps passed are equal in value.
28
28
  */
@@ -31,4 +31,7 @@ export declare function areRegExpsEqual(a: RegExp, b: RegExp): boolean;
31
31
  * Whether the `Set`s are equal in value.
32
32
  */
33
33
  export declare function areSetsEqual(a: Set<any>, b: Set<any>, state: State<any>): boolean;
34
+ /**
35
+ * Whether the TypedArray instances are equal in value.
36
+ */
34
37
  export declare function areTypedArraysEqual(a: TypedArray, b: TypedArray): boolean;
@@ -5,37 +5,37 @@ export * from './internalTypes';
5
5
  /**
6
6
  * Whether the items passed are deeply-equal in value.
7
7
  */
8
- export declare const deepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
8
+ export declare const deepEqual: <A, B>(a: A, b: B) => boolean;
9
9
  /**
10
10
  * Whether the items passed are deeply-equal in value based on strict comparison.
11
11
  */
12
- export declare const strictDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
12
+ export declare const strictDeepEqual: <A, B>(a: A, b: B) => boolean;
13
13
  /**
14
14
  * Whether the items passed are deeply-equal in value, including circular references.
15
15
  */
16
- export declare const circularDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
16
+ export declare const circularDeepEqual: <A, B>(a: A, b: B) => boolean;
17
17
  /**
18
18
  * Whether the items passed are deeply-equal in value, including circular references,
19
19
  * based on strict comparison.
20
20
  */
21
- export declare const strictCircularDeepEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
21
+ export declare const strictCircularDeepEqual: <A, B>(a: A, b: B) => boolean;
22
22
  /**
23
23
  * Whether the items passed are shallowly-equal in value.
24
24
  */
25
- export declare const shallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
25
+ export declare const shallowEqual: <A, B>(a: A, b: B) => boolean;
26
26
  /**
27
27
  * Whether the items passed are shallowly-equal in value based on strict comparison
28
28
  */
29
- export declare const strictShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
29
+ export declare const strictShallowEqual: <A, B>(a: A, b: B) => boolean;
30
30
  /**
31
31
  * Whether the items passed are shallowly-equal in value, including circular references.
32
32
  */
33
- export declare const circularShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
33
+ export declare const circularShallowEqual: <A, B>(a: A, b: B) => boolean;
34
34
  /**
35
35
  * Whether the items passed are shallowly-equal in value, including circular references,
36
36
  * based on strict comparison.
37
37
  */
38
- export declare const strictCircularShallowEqual: <A, B>(a: A, b: B, metaOverride?: unknown) => boolean;
38
+ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
39
39
  /**
40
40
  * Create a custom equality comparison method.
41
41
  *
@@ -44,4 +44,4 @@ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B, metaOverride
44
44
  * support for legacy environments that do not support expected features like
45
45
  * `RegExp.prototype.flags` out of the box.
46
46
  */
47
- export declare function createCustomEqual<Meta>(options?: CustomEqualCreatorOptions<Meta>): <A, B>(a: A, b: B, metaOverride?: Meta | undefined) => boolean;
47
+ export declare function createCustomEqual<Meta = undefined>(options?: CustomEqualCreatorOptions<Meta>): <A, B>(a: A, b: B) => boolean;
@@ -1,46 +1,134 @@
1
- export interface BaseCircular extends Pick<WeakMap<any, any>, 'delete' | 'get'> {
2
- 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;
3
9
  }
4
- export type State<Meta> = CircularState<Meta> | DefaultState<Meta>;
5
- export interface CircularState<Meta> {
6
- readonly cache: BaseCircular;
10
+ export interface State<Meta> {
11
+ /**
12
+ * Cache used to identify circular references
13
+ */
14
+ readonly cache: Cache<any, any> | undefined;
15
+ /**
16
+ * Method used to determine equality of nested value.
17
+ */
7
18
  readonly equals: InternalEqualityComparator<Meta>;
19
+ /**
20
+ * Additional value that can be used for comparisons.
21
+ */
8
22
  meta: Meta;
23
+ /**
24
+ * Whether the equality comparison is strict, meaning it matches
25
+ * all properties (including symbols and non-enumerable properties)
26
+ * with equal shape of descriptors.
27
+ */
9
28
  readonly strict: boolean;
10
29
  }
11
- export interface DefaultState<Meta> {
30
+ export interface CircularState<Meta> extends State<Meta> {
31
+ readonly cache: Cache<any, any>;
32
+ }
33
+ export interface DefaultState<Meta> extends State<Meta> {
12
34
  readonly cache: undefined;
13
- readonly equals: InternalEqualityComparator<Meta>;
14
- meta: Meta;
15
- readonly strict: boolean;
16
35
  }
17
36
  export interface Dictionary<Value = any> {
18
37
  [key: string | symbol]: Value;
19
38
  $$typeof?: any;
20
39
  }
21
40
  export interface ComparatorConfig<Meta> {
41
+ /**
42
+ * Whether the arrays passed are equal in value. In strict mode, this includes
43
+ * additional properties added to the array.
44
+ */
22
45
  areArraysEqual: TypeEqualityComparator<any, Meta>;
46
+ /**
47
+ * Whether the dates passed are equal in value.
48
+ */
23
49
  areDatesEqual: TypeEqualityComparator<any, Meta>;
50
+ /**
51
+ * Whether the maps passed are equal in value. In strict mode, this includes
52
+ * additional properties added to the map.
53
+ */
24
54
  areMapsEqual: TypeEqualityComparator<any, Meta>;
55
+ /**
56
+ * Whether the objects passed are equal in value. In strict mode, this includes
57
+ * non-enumerable properties added to the map, as well as symbol properties.
58
+ */
25
59
  areObjectsEqual: TypeEqualityComparator<any, Meta>;
60
+ /**
61
+ * Whether the primitive wrappers passed are equal in value.
62
+ */
26
63
  arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
64
+ /**
65
+ * Whether the regexps passed are equal in value.
66
+ */
27
67
  areRegExpsEqual: TypeEqualityComparator<any, Meta>;
68
+ /**
69
+ * Whether the sets passed are equal in value. In strict mode, this includes
70
+ * additional properties added to the set.
71
+ */
28
72
  areSetsEqual: TypeEqualityComparator<any, Meta>;
73
+ /**
74
+ * Whether the typed arrays passed are equal in value. In strict mode, this includes
75
+ * additional properties added to the typed array.
76
+ */
29
77
  areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
30
78
  }
31
79
  export type CreateCustomComparatorConfig<Meta> = (config: ComparatorConfig<Meta>) => Partial<ComparatorConfig<Meta>>;
32
- export type CreateState<Meta> = (comparator: EqualityComparator<Meta>) => Partial<State<Meta>>;
80
+ export type CreateState<Meta> = () => {
81
+ cache?: Cache<any, any> | undefined;
82
+ meta?: Meta;
83
+ };
33
84
  export type EqualityComparator<Meta> = <A, B>(a: A, b: B, state: State<Meta>) => boolean;
34
85
  export type AnyEqualityComparator<Meta> = (a: any, b: any, state: State<Meta>) => boolean;
35
86
  export type EqualityComparatorCreator<Meta> = (fn: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
36
87
  export type InternalEqualityComparator<Meta> = (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
88
+ export type PrimitiveWrapper = Boolean | Number | String;
89
+ /**
90
+ * Type which encompasses possible instances of TypedArray
91
+ * classes.
92
+ *
93
+ * **NOTE**: This does not include `BigInt64Array` and
94
+ * `BitUint64Array` because those are part of ES2020 and
95
+ * not supported by certain TS configurations. If using
96
+ * either in `areTypedArraysEqual`, you can cast the
97
+ * instance as `TypedArray` and it will work as expected,
98
+ * because runtime checks will still work for those classes.
99
+ */
37
100
  export type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint16Array | Uint32Array | Uint8Array | Uint8ClampedArray;
38
101
  export type TypeEqualityComparator<Type, Meta = undefined> = (a: Type, b: Type, state: State<Meta>) => boolean;
39
102
  export interface CustomEqualCreatorOptions<Meta> {
103
+ /**
104
+ * Whether circular references should be supported. It causes the
105
+ * comparison to be slower, but for objects that have circular references
106
+ * it is required to avoid stack overflows.
107
+ */
40
108
  circular?: boolean;
41
- comparator?: EqualityComparator<Meta>;
109
+ /**
110
+ * Create a custom configuration of type-specific equality comparators.
111
+ * This receives the default configuration, which allows either replacement
112
+ * or supersetting of the default methods.
113
+ */
42
114
  createCustomConfig?: CreateCustomComparatorConfig<Meta>;
43
- createInternalComparator?: <Meta>(compare: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
115
+ /**
116
+ * Create a custom internal comparator, which is used as an override to the
117
+ * default entry point for nested value equality comparisons. This is often
118
+ * used for doing custom logic for specific types (such as handling a specific
119
+ * class instance differently than other objects) or to incorporate `meta` in
120
+ * the comparison. See the recipes for examples.
121
+ */
122
+ createInternalComparator?: (compare: EqualityComparator<Meta>) => InternalEqualityComparator<Meta>;
123
+ /**
124
+ * Create a custom `state` object passed between the methods. This allows for
125
+ * custom `cache` and/or `meta` values to be used.
126
+ */
44
127
  createState?: CreateState<Meta>;
128
+ /**
129
+ * Whether the equality comparison is strict, meaning it matches
130
+ * all properties (including symbols and non-enumerable properties)
131
+ * with equal shape of descriptors.
132
+ */
45
133
  strict?: boolean;
46
134
  }
@@ -1,13 +1,8 @@
1
- import { AnyEqualityComparator, Dictionary, EqualityComparator, InternalEqualityComparator, State, TypeEqualityComparator } from './internalTypes';
1
+ import { AnyEqualityComparator, Dictionary, State, TypeEqualityComparator } from './internalTypes';
2
2
  /**
3
3
  * Combine two comparators into a single comparators.
4
4
  */
5
5
  export declare function combineComparators<Meta>(comparatorA: AnyEqualityComparator<Meta>, comparatorB: AnyEqualityComparator<Meta>): <A, B>(a: A, b: B, state: State<Meta>) => boolean;
6
- /**
7
- * Default equality comparator pass-through, used as the standard `isEqual` creator for
8
- * use inside the built comparator.
9
- */
10
- export declare function createInternalComparator<Meta>(compare: EqualityComparator<Meta>): InternalEqualityComparator<Meta>;
11
6
  /**
12
7
  * Wrap the provided `areItemsEqual` method to manage the circular state, allowing
13
8
  * for circular references to be safely included in the comparison without creating