fast-equals 5.0.0 → 5.1.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 +6 -0
- package/README.md +5 -1
- package/dist/cjs/index.cjs +69 -11
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/types/comparator.d.ts +1 -1
- package/dist/cjs/types/equals.d.ts +17 -0
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/internalTypes.d.ts +16 -0
- package/dist/cjs/types/utils.d.ts +1 -1
- package/dist/esm/index.mjs +69 -11
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/types/comparator.d.ts +1 -1
- package/dist/esm/types/equals.d.ts +17 -0
- package/dist/esm/types/index.d.ts +1 -1
- package/dist/esm/types/internalTypes.d.ts +16 -0
- package/dist/esm/types/utils.d.ts +1 -1
- package/dist/min/index.js +1 -1
- package/dist/min/types/comparator.d.ts +1 -1
- package/dist/min/types/equals.d.ts +17 -0
- package/dist/min/types/index.d.ts +1 -1
- package/dist/min/types/internalTypes.d.ts +16 -0
- package/dist/min/types/utils.d.ts +1 -1
- package/dist/umd/index.js +69 -11
- package/dist/umd/index.js.map +1 -1
- package/dist/umd/types/comparator.d.ts +1 -1
- package/dist/umd/types/equals.d.ts +17 -0
- package/dist/umd/types/index.d.ts +1 -1
- package/dist/umd/types/internalTypes.d.ts +16 -0
- package/dist/umd/types/utils.d.ts +1 -1
- package/index.d.ts +22 -5
- package/package.json +26 -26
- package/src/comparator.ts +53 -13
- package/src/equals.ts +42 -0
- package/src/index.ts +18 -1
- package/src/internalTypes.ts +16 -0
- package/src/utils.ts +2 -2
package/src/comparator.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
areArraysEqual as areArraysEqualDefault,
|
|
3
3
|
areDatesEqual as areDatesEqualDefault,
|
|
4
|
+
areErrorsEqual as areErrorsEqualDefault,
|
|
5
|
+
areFunctionsEqual as areFunctionsEqualDefault,
|
|
4
6
|
areMapsEqual as areMapsEqualDefault,
|
|
7
|
+
areNumbersEqual as areNumbersEqualDefault,
|
|
5
8
|
areObjectsEqual as areObjectsEqualDefault,
|
|
6
9
|
areObjectsEqualStrict as areObjectsEqualStrictDefault,
|
|
7
10
|
arePrimitiveWrappersEqual as arePrimitiveWrappersEqualDefault,
|
|
8
11
|
areRegExpsEqual as areRegExpsEqualDefault,
|
|
9
12
|
areSetsEqual as areSetsEqualDefault,
|
|
10
|
-
areTypedArraysEqual,
|
|
13
|
+
areTypedArraysEqual as areTypedArraysEqualDefault,
|
|
14
|
+
areUrlsEqual as areUrlsEqualDefault,
|
|
11
15
|
} from './equals';
|
|
12
16
|
import { combineComparators, createIsCircular } from './utils';
|
|
13
17
|
import type {
|
|
@@ -22,12 +26,14 @@ import type {
|
|
|
22
26
|
const ARGUMENTS_TAG = '[object Arguments]';
|
|
23
27
|
const BOOLEAN_TAG = '[object Boolean]';
|
|
24
28
|
const DATE_TAG = '[object Date]';
|
|
29
|
+
const ERROR_TAG = '[object Error]';
|
|
25
30
|
const MAP_TAG = '[object Map]';
|
|
26
31
|
const NUMBER_TAG = '[object Number]';
|
|
27
32
|
const OBJECT_TAG = '[object Object]';
|
|
28
33
|
const REG_EXP_TAG = '[object RegExp]';
|
|
29
34
|
const SET_TAG = '[object Set]';
|
|
30
35
|
const STRING_TAG = '[object String]';
|
|
36
|
+
const URL_TAG = '[object URL]';
|
|
31
37
|
|
|
32
38
|
const { isArray } = Array;
|
|
33
39
|
const isTypedArray =
|
|
@@ -53,12 +59,16 @@ interface CreateIsEqualOptions<Meta> {
|
|
|
53
59
|
export function createEqualityComparator<Meta>({
|
|
54
60
|
areArraysEqual,
|
|
55
61
|
areDatesEqual,
|
|
62
|
+
areErrorsEqual,
|
|
63
|
+
areFunctionsEqual,
|
|
56
64
|
areMapsEqual,
|
|
65
|
+
areNumbersEqual,
|
|
57
66
|
areObjectsEqual,
|
|
58
67
|
arePrimitiveWrappersEqual,
|
|
59
68
|
areRegExpsEqual,
|
|
60
69
|
areSetsEqual,
|
|
61
70
|
areTypedArraysEqual,
|
|
71
|
+
areUrlsEqual,
|
|
62
72
|
}: ComparatorConfig<Meta>): EqualityComparator<Meta> {
|
|
63
73
|
/**
|
|
64
74
|
* compare the value of the two objects and return true if they are equivalent in values
|
|
@@ -69,17 +79,29 @@ export function createEqualityComparator<Meta>({
|
|
|
69
79
|
return true;
|
|
70
80
|
}
|
|
71
81
|
|
|
72
|
-
// If the items are
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
// If either of the items are nullish and fail the strictly equal check
|
|
83
|
+
// above, then they must be unequal.
|
|
84
|
+
if (a == null || b == null) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const type = typeof a;
|
|
89
|
+
|
|
90
|
+
if (type !== typeof b) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (type !== 'object') {
|
|
95
|
+
if (type === 'number') {
|
|
96
|
+
return areNumbersEqual(a, b, state);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (type === 'function') {
|
|
100
|
+
return areFunctionsEqual(a, b, state);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// If a primitive value that is not strictly equal, it must be unequal.
|
|
104
|
+
return false;
|
|
83
105
|
}
|
|
84
106
|
|
|
85
107
|
const constructor = a.constructor;
|
|
@@ -148,6 +170,8 @@ export function createEqualityComparator<Meta>({
|
|
|
148
170
|
return areDatesEqual(a, b, state);
|
|
149
171
|
}
|
|
150
172
|
|
|
173
|
+
// For RegExp, the properties are not enumerable, and therefore will give false positives if
|
|
174
|
+
// tested like a standard object.
|
|
151
175
|
if (tag === REG_EXP_TAG) {
|
|
152
176
|
return areRegExpsEqual(a, b, state);
|
|
153
177
|
}
|
|
@@ -171,6 +195,18 @@ export function createEqualityComparator<Meta>({
|
|
|
171
195
|
);
|
|
172
196
|
}
|
|
173
197
|
|
|
198
|
+
// If a URL tag, it should be tested explicitly. Like RegExp, the properties are not
|
|
199
|
+
// enumerable, and therefore will give false positives if tested like a standard object.
|
|
200
|
+
if (tag === URL_TAG) {
|
|
201
|
+
return areUrlsEqual(a, b, state);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// If an error tag, it should be tested explicitly. Like RegExp, the properties are not
|
|
205
|
+
// enumerable, and therefore will give false positives if tested like a standard object.
|
|
206
|
+
if (tag === ERROR_TAG) {
|
|
207
|
+
return areErrorsEqual(a, b, state);
|
|
208
|
+
}
|
|
209
|
+
|
|
174
210
|
// If an arguments tag, it should be treated as a standard object.
|
|
175
211
|
if (tag === ARGUMENTS_TAG) {
|
|
176
212
|
return areObjectsEqual(a, b, state);
|
|
@@ -211,9 +247,12 @@ export function createEqualityComparatorConfig<Meta>({
|
|
|
211
247
|
? areObjectsEqualStrictDefault
|
|
212
248
|
: areArraysEqualDefault,
|
|
213
249
|
areDatesEqual: areDatesEqualDefault,
|
|
250
|
+
areErrorsEqual: areErrorsEqualDefault,
|
|
251
|
+
areFunctionsEqual: areFunctionsEqualDefault,
|
|
214
252
|
areMapsEqual: strict
|
|
215
253
|
? combineComparators(areMapsEqualDefault, areObjectsEqualStrictDefault)
|
|
216
254
|
: areMapsEqualDefault,
|
|
255
|
+
areNumbersEqual: areNumbersEqualDefault,
|
|
217
256
|
areObjectsEqual: strict
|
|
218
257
|
? areObjectsEqualStrictDefault
|
|
219
258
|
: areObjectsEqualDefault,
|
|
@@ -224,7 +263,8 @@ export function createEqualityComparatorConfig<Meta>({
|
|
|
224
263
|
: areSetsEqualDefault,
|
|
225
264
|
areTypedArraysEqual: strict
|
|
226
265
|
? areObjectsEqualStrictDefault
|
|
227
|
-
:
|
|
266
|
+
: areTypedArraysEqualDefault,
|
|
267
|
+
areUrlsEqual: areUrlsEqualDefault,
|
|
228
268
|
};
|
|
229
269
|
|
|
230
270
|
if (createCustomConfig) {
|
package/src/equals.ts
CHANGED
|
@@ -36,6 +36,28 @@ export function areDatesEqual(a: Date, b: Date): boolean {
|
|
|
36
36
|
return sameValueZeroEqual(a.getTime(), b.getTime());
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Whether the errors passed are equal in value.
|
|
41
|
+
*/
|
|
42
|
+
export function areErrorsEqual(a: Error, b: Error): boolean {
|
|
43
|
+
return (
|
|
44
|
+
a.name === b.name &&
|
|
45
|
+
a.message === b.message &&
|
|
46
|
+
a.cause === b.cause &&
|
|
47
|
+
a.stack === b.stack
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether the functions passed are equal in value.
|
|
53
|
+
*/
|
|
54
|
+
export function areFunctionsEqual(
|
|
55
|
+
a: (...args: any[]) => any,
|
|
56
|
+
b: (...args: any[]) => any,
|
|
57
|
+
): boolean {
|
|
58
|
+
return a === b;
|
|
59
|
+
}
|
|
60
|
+
|
|
39
61
|
/**
|
|
40
62
|
* Whether the `Map`s are equal in value.
|
|
41
63
|
*/
|
|
@@ -96,6 +118,11 @@ export function areMapsEqual(
|
|
|
96
118
|
return true;
|
|
97
119
|
}
|
|
98
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Whether the numbers are equal in value.
|
|
123
|
+
*/
|
|
124
|
+
export const areNumbersEqual = sameValueZeroEqual;
|
|
125
|
+
|
|
99
126
|
/**
|
|
100
127
|
* Whether the objects are equal in value.
|
|
101
128
|
*/
|
|
@@ -298,3 +325,18 @@ export function areTypedArraysEqual(a: TypedArray, b: TypedArray) {
|
|
|
298
325
|
|
|
299
326
|
return true;
|
|
300
327
|
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Whether the URL instances are equal in value.
|
|
331
|
+
*/
|
|
332
|
+
export function areUrlsEqual(a: URL, b: URL): boolean {
|
|
333
|
+
return (
|
|
334
|
+
a.hostname === b.hostname &&
|
|
335
|
+
a.pathname === b.pathname &&
|
|
336
|
+
a.protocol === b.protocol &&
|
|
337
|
+
a.port === b.port &&
|
|
338
|
+
a.hash === b.hash &&
|
|
339
|
+
a.username === b.username &&
|
|
340
|
+
a.password === b.password
|
|
341
|
+
);
|
|
342
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,7 +8,24 @@ import type { CustomEqualCreatorOptions } from './internalTypes';
|
|
|
8
8
|
import { sameValueZeroEqual } from './utils';
|
|
9
9
|
|
|
10
10
|
export { sameValueZeroEqual };
|
|
11
|
-
export
|
|
11
|
+
export type {
|
|
12
|
+
AnyEqualityComparator,
|
|
13
|
+
Cache,
|
|
14
|
+
CircularState,
|
|
15
|
+
ComparatorConfig,
|
|
16
|
+
CreateCustomComparatorConfig,
|
|
17
|
+
CreateState,
|
|
18
|
+
CustomEqualCreatorOptions,
|
|
19
|
+
DefaultState,
|
|
20
|
+
Dictionary,
|
|
21
|
+
EqualityComparator,
|
|
22
|
+
EqualityComparatorCreator,
|
|
23
|
+
InternalEqualityComparator,
|
|
24
|
+
PrimitiveWrapper,
|
|
25
|
+
State,
|
|
26
|
+
TypeEqualityComparator,
|
|
27
|
+
TypedArray,
|
|
28
|
+
} from './internalTypes';
|
|
12
29
|
|
|
13
30
|
/**
|
|
14
31
|
* Whether the items passed are deeply-equal in value.
|
package/src/internalTypes.ts
CHANGED
|
@@ -52,11 +52,23 @@ export interface ComparatorConfig<Meta> {
|
|
|
52
52
|
* Whether the dates passed are equal in value.
|
|
53
53
|
*/
|
|
54
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>;
|
|
55
63
|
/**
|
|
56
64
|
* Whether the maps passed are equal in value. In strict mode, this includes
|
|
57
65
|
* additional properties added to the map.
|
|
58
66
|
*/
|
|
59
67
|
areMapsEqual: TypeEqualityComparator<any, Meta>;
|
|
68
|
+
/**
|
|
69
|
+
* Whether the numbers passed are equal in value.
|
|
70
|
+
*/
|
|
71
|
+
areNumbersEqual: TypeEqualityComparator<any, Meta>;
|
|
60
72
|
/**
|
|
61
73
|
* Whether the objects passed are equal in value. In strict mode, this includes
|
|
62
74
|
* non-enumerable properties added to the map, as well as symbol properties.
|
|
@@ -80,6 +92,10 @@ export interface ComparatorConfig<Meta> {
|
|
|
80
92
|
* additional properties added to the typed array.
|
|
81
93
|
*/
|
|
82
94
|
areTypedArraysEqual: TypeEqualityComparator<any, Meta>;
|
|
95
|
+
/**
|
|
96
|
+
* Whether the URLs passed are equal in value.
|
|
97
|
+
*/
|
|
98
|
+
areUrlsEqual: TypeEqualityComparator<any, Meta>;
|
|
83
99
|
}
|
|
84
100
|
|
|
85
101
|
export type CreateCustomComparatorConfig<Meta> = (
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
AnyEqualityComparator,
|
|
3
3
|
Cache,
|
|
4
4
|
CircularState,
|
|
@@ -84,5 +84,5 @@ export const hasOwn =
|
|
|
84
84
|
* Whether the values passed are strictly equal or both NaN.
|
|
85
85
|
*/
|
|
86
86
|
export function sameValueZeroEqual(a: any, b: any): boolean {
|
|
87
|
-
return a
|
|
87
|
+
return a === b || (!a && !b && a !== a && b !== b);
|
|
88
88
|
}
|