fast-equals 5.2.0 → 5.2.1-beta.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.
package/index.d.ts CHANGED
@@ -1,23 +1,205 @@
1
- import type { CustomEqualCreatorOptions } from './src/internalTypes';
2
-
3
- export type {
4
- AnyEqualityComparator,
5
- Cache,
6
- CircularState,
7
- ComparatorConfig,
8
- CreateCustomComparatorConfig,
9
- CreateState,
10
- CustomEqualCreatorOptions,
11
- DefaultState,
12
- Dictionary,
13
- EqualityComparator,
14
- EqualityComparatorCreator,
15
- InternalEqualityComparator,
16
- PrimitiveWrapper,
17
- State,
18
- TypeEqualityComparator,
19
- TypedArray,
20
- } from './src/internalTypes';
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;
9
+ }
10
+
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
+ */
19
+ readonly equals: InternalEqualityComparator<Meta>;
20
+ /**
21
+ * Additional value that can be used for comparisons.
22
+ */
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
+ */
29
+ readonly strict: boolean;
30
+ }
31
+
32
+ export interface CircularState<Meta> extends State<Meta> {
33
+ readonly cache: Cache<any, any>;
34
+ }
35
+
36
+ export interface DefaultState<Meta> extends State<Meta> {
37
+ readonly cache: undefined;
38
+ }
39
+
40
+ export interface Dictionary<Value = any> {
41
+ [key: string | symbol]: Value;
42
+ $$typeof?: any;
43
+ }
44
+
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
+ */
50
+ areArraysEqual: TypeEqualityComparator<any, Meta>;
51
+ /**
52
+ * Whether the dates passed are equal in value.
53
+ */
54
+ areDatesEqual: TypeEqualityComparator<any, Meta>;
55
+ /**
56
+ * Whether the errors passed are equal in value.
57
+ */
58
+ areErrorsEqual: TypeEqualityComparator<any, Meta>;
59
+ /**
60
+ * Whether the functions passed are equal in value.
61
+ */
62
+ areFunctionsEqual: TypeEqualityComparator<any, Meta>;
63
+ /**
64
+ * Whether the maps passed are equal in value. In strict mode, this includes
65
+ * additional properties added to the map.
66
+ */
67
+ areMapsEqual: TypeEqualityComparator<any, Meta>;
68
+ /**
69
+ * Whether the numbers passed are equal in value.
70
+ */
71
+ areNumbersEqual: TypeEqualityComparator<any, Meta>;
72
+ /**
73
+ * Whether the objects passed are equal in value. In strict mode, this includes
74
+ * non-enumerable properties added to the map, as well as symbol properties.
75
+ */
76
+ areObjectsEqual: TypeEqualityComparator<any, Meta>;
77
+ /**
78
+ * Whether the primitive wrappers passed are equal in value.
79
+ */
80
+ arePrimitiveWrappersEqual: TypeEqualityComparator<any, Meta>;
81
+ /**
82
+ * Whether the regexps passed are equal in value.
83
+ */
84
+ areRegExpsEqual: TypeEqualityComparator<any, Meta>;
85
+ /**
86
+ * Whether the sets passed are equal in value. In strict mode, this includes
87
+ * additional properties added to the set.
88
+ */
89
+ areSetsEqual: TypeEqualityComparator<any, Meta>;
90
+ /**
91
+ * Whether the typed arrays passed are equal in value. In strict mode, this includes
92
+ * additional properties added to the typed array.
93
+ */
94
+ areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
95
+ /**
96
+ * Whether the URLs passed are equal in value.
97
+ */
98
+ areUrlsEqual: TypeEqualityComparator<any, Meta>;
99
+ }
100
+
101
+ export type CreateCustomComparatorConfig<Meta> = (
102
+ config: ComparatorConfig<Meta>,
103
+ ) => Partial<ComparatorConfig<Meta>>;
104
+
105
+ export type CreateState<Meta> = () => {
106
+ cache?: Cache<any, any> | undefined;
107
+ meta?: Meta;
108
+ };
109
+
110
+ export type EqualityComparator<Meta> = <A, B>(
111
+ a: A,
112
+ b: B,
113
+ state: State<Meta>,
114
+ ) => boolean;
115
+ export type AnyEqualityComparator<Meta> = (
116
+ a: any,
117
+ b: any,
118
+ state: State<Meta>,
119
+ ) => boolean;
120
+
121
+ export type EqualityComparatorCreator<Meta> = (
122
+ fn: EqualityComparator<Meta>,
123
+ ) => InternalEqualityComparator<Meta>;
124
+
125
+ export type InternalEqualityComparator<Meta> = (
126
+ a: any,
127
+ b: any,
128
+ indexOrKeyA: any,
129
+ indexOrKeyB: any,
130
+ parentA: any,
131
+ parentB: any,
132
+ state: State<Meta>,
133
+ ) => boolean;
134
+
135
+ // We explicitly check for primitive wrapper types
136
+ // eslint-disable-next-line @typescript-eslint/ban-types
137
+ export type PrimitiveWrapper = Boolean | Number | String;
138
+
139
+ /**
140
+ * Type which encompasses possible instances of TypedArray
141
+ * classes.
142
+ *
143
+ * **NOTE**: This does not include `BigInt64Array` and
144
+ * `BitUint64Array` because those are part of ES2020 and
145
+ * not supported by certain TS configurations. If using
146
+ * either in `areTypedArraysEqual`, you can cast the
147
+ * instance as `TypedArray` and it will work as expected,
148
+ * because runtime checks will still work for those classes.
149
+ */
150
+ export type TypedArray =
151
+ | Float32Array
152
+ | Float64Array
153
+ | Int8Array
154
+ | Int16Array
155
+ | Int32Array
156
+ | Uint16Array
157
+ | Uint32Array
158
+ | Uint8Array
159
+ | Uint8ClampedArray;
160
+
161
+ export type TypeEqualityComparator<Type, Meta = undefined> = (
162
+ a: Type,
163
+ b: Type,
164
+ state: State<Meta>,
165
+ ) => boolean;
166
+
167
+ export interface CustomEqualCreatorOptions<Meta> {
168
+ /**
169
+ * Whether circular references should be supported. It causes the
170
+ * comparison to be slower, but for objects that have circular references
171
+ * it is required to avoid stack overflows.
172
+ */
173
+ circular?: boolean;
174
+ /**
175
+ * Create a custom configuration of type-specific equality comparators.
176
+ * This receives the default configuration, which allows either replacement
177
+ * or supersetting of the default methods.
178
+ */
179
+ createCustomConfig?: CreateCustomComparatorConfig<Meta>;
180
+ /**
181
+ * Create a custom internal comparator, which is used as an override to the
182
+ * default entry point for nested value equality comparisons. This is often
183
+ * used for doing custom logic for specific types (such as handling a specific
184
+ * class instance differently than other objects) or to incorporate `meta` in
185
+ * the comparison. See the recipes for examples.
186
+ */
187
+ createInternalComparator?: (
188
+ compare: EqualityComparator<Meta>,
189
+ ) => InternalEqualityComparator<Meta>;
190
+ /**
191
+ * Create a custom `state` object passed between the methods. This allows for
192
+ * custom `cache` and/or `meta` values to be used.
193
+ */
194
+ createState?: CreateState<Meta>;
195
+ /**
196
+ * Whether the equality comparison is strict, meaning it matches
197
+ * all properties (including symbols and non-enumerable properties)
198
+ * with equal shape of descriptors.
199
+ */
200
+ strict?: boolean;
201
+ }
202
+
21
203
 
22
204
  /**
23
205
  * Whether the values passed are strictly equal or both NaN.
@@ -68,4 +250,4 @@ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
68
250
  */
69
251
  export declare function createCustomEqual<Meta = undefined>(
70
252
  options?: CustomEqualCreatorOptions<Meta>,
71
- ): <A, B>(a: A, b: B) => boolean;
253
+ ): <A, B>(a: A, b: B) => boolean;
package/package.json CHANGED
@@ -87,10 +87,11 @@
87
87
  },
88
88
  "scripts": {
89
89
  "benchmark": "npm run build:esm && node benchmark/index.js",
90
- "build": "npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:min",
90
+ "build": "npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:min && npm run build:types",
91
91
  "build:cjs": "rm -rf dist/cjs && NODE_ENV=production rollup -c config/rollup/config.cjs.js && tsc -p ./config/tsconfig/cjs.json",
92
92
  "build:esm": "rm -rf dist/esm && NODE_ENV=production rollup -c config/rollup/config.esm.js && tsc -p ./config/tsconfig/esm.json",
93
93
  "build:min": "rm -rf dist/min && NODE_ENV=production rollup -c config/rollup/config.min.js && tsc -p ./config/tsconfig/min.json",
94
+ "build:types": "node scripts/fallback-types.mjs",
94
95
  "build:umd": "rm -rf dist/umd && NODE_ENV=production rollup -c config/rollup/config.umd.js && tsc -p ./config/tsconfig/umd.json",
95
96
  "dev": "NODE_ENV=development webpack serve --progress --config=config/webpack.config.js",
96
97
  "format": "prettier **/*.ts --write",
@@ -108,5 +109,5 @@
108
109
  "sideEffects": false,
109
110
  "type": "module",
110
111
  "types": "./index.d.ts",
111
- "version": "5.2.0"
112
+ "version": "5.2.1-beta.0"
112
113
  }
@@ -0,0 +1,64 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ const METHODS = `
5
+ /**
6
+ * Whether the values passed are strictly equal or both NaN.
7
+ */
8
+ export declare const sameValueZeroEqual: <A, B>(a: A, b: B) => boolean;
9
+
10
+ /**
11
+ * Whether the items passed are deeply-equal in value.
12
+ */
13
+ export declare const deepEqual: <A, B>(a: A, b: B) => boolean;
14
+ /**
15
+ * Whether the items passed are deeply-equal in value based on strict comparison.
16
+ */
17
+ export declare const strictDeepEqual: <A, B>(a: A, b: B) => boolean;
18
+ /**
19
+ * Whether the items passed are deeply-equal in value, including circular references.
20
+ */
21
+ export declare const circularDeepEqual: <A, B>(a: A, b: B) => boolean;
22
+ /**
23
+ * Whether the items passed are deeply-equal in value, including circular references,
24
+ * based on strict comparison.
25
+ */
26
+ export declare const strictCircularDeepEqual: <A, B>(a: A, b: B) => boolean;
27
+ /**
28
+ * Whether the items passed are shallowly-equal in value.
29
+ */
30
+ export declare const shallowEqual: <A, B>(a: A, b: B) => boolean;
31
+ /**
32
+ * Whether the items passed are shallowly-equal in value based on strict comparison
33
+ */
34
+ export declare const strictShallowEqual: <A, B>(a: A, b: B) => boolean;
35
+ /**
36
+ * Whether the items passed are shallowly-equal in value, including circular references.
37
+ */
38
+ export declare const circularShallowEqual: <A, B>(a: A, b: B) => boolean;
39
+ /**
40
+ * Whether the items passed are shallowly-equal in value, including circular references,
41
+ * based on strict comparison.
42
+ */
43
+ export declare const strictCircularShallowEqual: <A, B>(a: A, b: B) => boolean;
44
+ /**
45
+ * Create a custom equality comparison method.
46
+ *
47
+ * This can be done to create very targeted comparisons in extreme hot-path scenarios
48
+ * where the standard methods are not performant enough, but can also be used to provide
49
+ * support for legacy environments that cannot polyfill for modern features expected by
50
+ * \`fast-equals\`, such as \`WeakMap\` or \`RegExp.prototype.flags\`.
51
+ */
52
+ export declare function createCustomEqual<Meta = undefined>(
53
+ options?: CustomEqualCreatorOptions<Meta>,
54
+ ): <A, B>(a: A, b: B) => boolean;
55
+ `;
56
+
57
+ const INTERNAL_TYPES = fs.readFileSync(
58
+ path.join(import.meta.dirname, '..', 'src', 'internalTypes.ts'),
59
+ );
60
+
61
+ const OUTPUT = `${INTERNAL_TYPES}\n${METHODS}`.trim();
62
+ const OUTPUT_DESTINATION = path.join(import.meta.dirname, '..', 'index.d.ts');
63
+
64
+ fs.writeFileSync(OUTPUT_DESTINATION, OUTPUT, 'utf-8');