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.
- package/CHANGELOG.md +17 -1
- package/README.md +68 -48
- package/dist/cjs/index.cjs +165 -140
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/types/comparator.d.ts +20 -3
- package/dist/cjs/types/equals.d.ts +5 -2
- package/dist/cjs/types/index.d.ts +9 -9
- package/dist/cjs/types/internalTypes.d.ts +100 -12
- package/dist/cjs/types/utils.d.ts +1 -6
- package/dist/esm/index.mjs +165 -140
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/types/comparator.d.ts +20 -3
- package/dist/esm/types/equals.d.ts +5 -2
- package/dist/esm/types/index.d.ts +9 -9
- package/dist/esm/types/internalTypes.d.ts +100 -12
- package/dist/esm/types/utils.d.ts +1 -6
- package/dist/min/index.js +1 -1
- package/dist/min/types/comparator.d.ts +20 -3
- package/dist/min/types/equals.d.ts +5 -2
- package/dist/min/types/index.d.ts +9 -9
- package/dist/min/types/internalTypes.d.ts +100 -12
- package/dist/min/types/utils.d.ts +1 -6
- package/dist/umd/index.js +165 -140
- package/dist/umd/index.js.map +1 -1
- package/dist/umd/types/comparator.d.ts +20 -3
- package/dist/umd/types/equals.d.ts +5 -2
- package/dist/umd/types/index.d.ts +9 -9
- package/dist/umd/types/internalTypes.d.ts +100 -12
- package/dist/umd/types/utils.d.ts +1 -6
- package/package.json +7 -7
- package/recipes/legacy-circular-equal-support.md +3 -2
- package/recipes/legacy-regexp-support.md +2 -3
- package/recipes/non-standard-properties.md +4 -2
- package/src/comparator.ts +126 -38
- package/src/equals.ts +97 -63
- package/src/index.ts +21 -65
- package/src/internalTypes.ts +104 -16
- 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 {
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
while ((aResult = aIterable.next())) {
|
|
59
|
+
if (aResult.done) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const bIterable = b.entries();
|
|
50
64
|
|
|
51
|
-
|
|
65
|
+
let hasMatch = false;
|
|
66
|
+
let matchIndex = 0;
|
|
52
67
|
|
|
53
|
-
|
|
68
|
+
while ((bResult = bIterable.next())) {
|
|
69
|
+
if (bResult.done) {
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
54
72
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
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
|
|
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(
|
|
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
|
-
|
|
231
|
+
if (a.size !== b.size) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
215
234
|
|
|
216
|
-
|
|
217
|
-
|
|
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
|
-
|
|
238
|
+
let aResult: IteratorResult<any>;
|
|
239
|
+
let bResult: IteratorResult<any>;
|
|
224
240
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
241
|
+
while ((aResult = aIterable.next())) {
|
|
242
|
+
if (aResult.done) {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
229
245
|
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
243
|
-
|
|
272
|
+
matchIndex++;
|
|
273
|
+
}
|
|
244
274
|
|
|
245
|
-
|
|
246
|
-
|
|
275
|
+
if (!hasMatch) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
247
278
|
}
|
|
248
279
|
|
|
249
|
-
return
|
|
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 {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from './
|
|
7
|
-
import {
|
|
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
|
-
|
|
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
|
|
85
|
+
strict = false,
|
|
86
86
|
} = options;
|
|
87
87
|
|
|
88
|
-
const config =
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
comparator
|
|
92
|
-
(
|
|
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
|
-
|
|
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
|
}
|
package/src/internalTypes.ts
CHANGED
|
@@ -1,22 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
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
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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<
|
|
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);
|