fast-equals 1.6.3 → 2.0.3

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/lib/utils.js DELETED
@@ -1,386 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.areSetsEqual = exports.areRegExpsEqual = exports.areObjectsEqual = exports.areMapsEqual = exports.areArraysEqual = exports.toValues = exports.toPairs = exports.createCircularEqual = exports.getNewCache = exports.isReactElement = exports.isPromiseLike = exports.isPlainObject = exports.sameValueZeroEqual = exports.hasValue = exports.hasPair = exports.addObjectToCache = void 0;
5
-
6
- var _constants = require("./constants");
7
-
8
- // constants
9
- var keys = Object.keys;
10
- /**
11
- * @function addObjectToCache
12
- *
13
- * @description
14
- * add object to cache if it is indeed an object
15
- *
16
- * @param {any} object the object to potentially add to the cache
17
- * @param {Object|WeakSet} cache the cache to add to
18
- * @returns {void}
19
- */
20
-
21
- var addObjectToCache = function addObjectToCache(object, cache) {
22
- return object && typeof object === 'object' && cache.add(object);
23
- };
24
- /**
25
- *
26
- * @param {Array<Array<any>>} pairs the pairs to check in
27
- * @param {Array<any>} pairToMatch the pair to check if exists
28
- * @param {function} isEqual the equality comparator
29
- * @param {any} meta the meta item to pass through
30
- * @returns {boolean} does the pair exist in the pairs
31
- */
32
-
33
-
34
- exports.addObjectToCache = addObjectToCache;
35
-
36
- var hasPair = function hasPair(pairs, pairToMatch, isEqual, meta) {
37
- var pair;
38
-
39
- for (var index = 0; index < pairs.length; index++) {
40
- pair = pairs[index];
41
-
42
- if (isEqual(pair[0], pairToMatch[0], meta) && isEqual(pair[1], pairToMatch[1], meta)) {
43
- return true;
44
- }
45
- }
46
-
47
- return false;
48
- };
49
- /**
50
- * @function hasValue
51
- *
52
- * @description
53
- * does the values include the vakye passed
54
- *
55
- * @param {Array<any>} values the values to check in
56
- * @param {any} item the value to locate
57
- * @param {function} isEqual the equality comparator
58
- * @param {any} meta the meta item to pass through
59
- * @returns {boolean} does the value exist in the values
60
- */
61
-
62
-
63
- exports.hasPair = hasPair;
64
-
65
- var hasValue = function hasValue(values, item, isEqual, meta) {
66
- for (var index = 0; index < values.length; index++) {
67
- if (isEqual(values[index], item, meta)) {
68
- return true;
69
- }
70
- }
71
-
72
- return false;
73
- };
74
- /**
75
- * @function sameValueZeroEqual
76
- *
77
- * @description
78
- * are the objects passed strictly equal or both NaN
79
- *
80
- * @param {any} objectA the object to compare against
81
- * @param {any} objectB the object to test
82
- * @returns {boolean} are the objects equal by the SameValueZero principle
83
- */
84
-
85
-
86
- exports.hasValue = hasValue;
87
-
88
- var sameValueZeroEqual = function sameValueZeroEqual(objectA, objectB) {
89
- return objectA === objectB || objectA !== objectA && objectB !== objectB;
90
- };
91
- /**
92
- * @function isPlainObject
93
- *
94
- * @description
95
- * is the object a plain object
96
- *
97
- * @param {any} object the object to test
98
- * @returns {boolean} is the object a plain object
99
- */
100
-
101
-
102
- exports.sameValueZeroEqual = sameValueZeroEqual;
103
-
104
- var isPlainObject = function isPlainObject(object) {
105
- return object.constructor === Object;
106
- };
107
- /**
108
- * @function isPromiseLike
109
- *
110
- * @description
111
- * is the object promise-like (thenable)
112
- *
113
- * @param {any} object the object to test
114
- * @returns {boolean} is the object promise-like
115
- */
116
-
117
-
118
- exports.isPlainObject = isPlainObject;
119
-
120
- var isPromiseLike = function isPromiseLike(object) {
121
- return typeof object.then === 'function';
122
- };
123
- /**
124
- * @function isReactElement
125
- *
126
- * @description
127
- * is the object passed a react element
128
- *
129
- * @param {any} object the object to test
130
- * @returns {boolean} is the object a react element
131
- */
132
-
133
-
134
- exports.isPromiseLike = isPromiseLike;
135
-
136
- var isReactElement = function isReactElement(object) {
137
- return !!(object.$$typeof && object._store);
138
- };
139
- /**
140
- * @function getNewCache
141
- *
142
- * @description
143
- * get a new cache object to prevent circular references
144
- *
145
- * @returns {Object|Weakset} the new cache object
146
- */
147
-
148
-
149
- exports.isReactElement = isReactElement;
150
-
151
- var getNewCache = function getNewCache() {
152
- return _constants.HAS_WEAKSET_SUPPORT ? new WeakSet() : Object.create({
153
- _values: [],
154
- add: function add(value) {
155
- this._values.push(value);
156
- },
157
- has: function has(value) {
158
- return !!~this._values.indexOf(value);
159
- }
160
- });
161
- };
162
- /**
163
- * @function createCircularEqual
164
- *
165
- * @description
166
- * create a custom isEqual handler specific to circular objects
167
- *
168
- * @param {funtion} [isEqual] the isEqual comparator to use instead of isDeepEqual
169
- * @returns {function(any, any): boolean}
170
- */
171
-
172
-
173
- exports.getNewCache = getNewCache;
174
-
175
- var createCircularEqual = function createCircularEqual(isEqual) {
176
- return function (isDeepEqual) {
177
- var comparator = isEqual || isDeepEqual;
178
- return function (objectA, objectB, cache) {
179
- if (cache === void 0) {
180
- cache = getNewCache();
181
- }
182
-
183
- var cacheHasA = cache.has(objectA);
184
- var cacheHasB = cache.has(objectB);
185
-
186
- if (cacheHasA || cacheHasB) {
187
- return cacheHasA && cacheHasB;
188
- }
189
-
190
- addObjectToCache(objectA, cache);
191
- addObjectToCache(objectB, cache);
192
- return comparator(objectA, objectB, cache);
193
- };
194
- };
195
- };
196
- /**
197
- * @function toPairs
198
- *
199
- * @param {Map} map the map to convert to [key, value] pairs (entries)
200
- * @returns {Array<Array<*>>} the [key, value] pairs
201
- */
202
-
203
-
204
- exports.createCircularEqual = createCircularEqual;
205
-
206
- var toPairs = function toPairs(map) {
207
- var pairs = [];
208
- map.forEach(function (value, key) {
209
- return pairs.push([key, value]);
210
- });
211
- return pairs;
212
- };
213
- /**
214
- * @function toValues
215
- *
216
- * @param {Set} set the set to convert to values
217
- * @returns {Array<*>} the values
218
- */
219
-
220
-
221
- exports.toPairs = toPairs;
222
-
223
- var toValues = function toValues(set) {
224
- var values = [];
225
- set.forEach(function (value) {
226
- return values.push(value);
227
- });
228
- return values;
229
- };
230
- /**
231
- * @function areArraysEqual
232
- *
233
- * @description
234
- * are the arrays equal in value
235
- *
236
- * @param {Array<any>} arrayA the array to test
237
- * @param {Array<any>} arrayB the array to test against
238
- * @param {function} isEqual the comparator to determine equality
239
- * @param {any} meta the meta object to pass through
240
- * @returns {boolean} are the arrays equal
241
- */
242
-
243
-
244
- exports.toValues = toValues;
245
-
246
- var areArraysEqual = function areArraysEqual(arrayA, arrayB, isEqual, meta) {
247
- if (arrayA.length !== arrayB.length) {
248
- return false;
249
- }
250
-
251
- for (var index = 0; index < arrayA.length; index++) {
252
- if (!isEqual(arrayA[index], arrayB[index], meta)) {
253
- return false;
254
- }
255
- }
256
-
257
- return true;
258
- };
259
- /**
260
- * @function areMapsEqual
261
- *
262
- * @description
263
- * are the maps equal in value
264
- *
265
- * @param {Map} mapA the map to test
266
- * @param {Map} mapB the map to test against
267
- * @param {function} isEqual the comparator to determine equality
268
- * @param {any} meta the meta map to pass through
269
- * @returns {boolean} are the maps equal
270
- */
271
-
272
-
273
- exports.areArraysEqual = areArraysEqual;
274
-
275
- var areMapsEqual = function areMapsEqual(mapA, mapB, isEqual, meta) {
276
- if (mapA.size !== mapB.size) {
277
- return false;
278
- }
279
-
280
- var pairsA = toPairs(mapA);
281
- var pairsB = toPairs(mapB);
282
-
283
- for (var index = 0; index < pairsA.length; index++) {
284
- if (!hasPair(pairsB, pairsA[index], isEqual, meta) || !hasPair(pairsA, pairsB[index], isEqual, meta)) {
285
- return false;
286
- }
287
- }
288
-
289
- return true;
290
- };
291
- /**
292
- * @function areObjectsEqual
293
- *
294
- * @description
295
- * are the objects equal in value
296
- *
297
- * @param {Object} objectA the object to test
298
- * @param {Object} objectB the object to test against
299
- * @param {function} isEqual the comparator to determine equality
300
- * @param {any} meta the meta object to pass through
301
- * @returns {boolean} are the objects equal
302
- */
303
-
304
-
305
- exports.areMapsEqual = areMapsEqual;
306
-
307
- var areObjectsEqual = function areObjectsEqual(objectA, objectB, isEqual, meta) {
308
- var keysA = keys(objectA);
309
- var keysB = keys(objectB);
310
-
311
- if (keysA.length !== keysB.length) {
312
- return false;
313
- }
314
-
315
- var key;
316
-
317
- for (var index = 0; index < keysA.length; index++) {
318
- key = keysA[index];
319
-
320
- if (!hasValue(keysB, key, sameValueZeroEqual)) {
321
- return false;
322
- } // if a react element, ignore the "_owner" key because its not necessary for equality comparisons
323
-
324
-
325
- if (key === '_owner' && isReactElement(objectA) && isReactElement(objectB)) {
326
- continue;
327
- }
328
-
329
- if (!isEqual(objectA[key], objectB[key], meta)) {
330
- return false;
331
- }
332
- }
333
-
334
- return true;
335
- };
336
- /**
337
- * @function areRegExpsEqual
338
- *
339
- * @description
340
- * are the regExps equal in value
341
- *
342
- * @param {RegExp} regExpA the regExp to test
343
- * @param {RegExp} regExpB the regExp to test agains
344
- * @returns {boolean} are the regExps equal
345
- */
346
-
347
-
348
- exports.areObjectsEqual = areObjectsEqual;
349
-
350
- var areRegExpsEqual = function areRegExpsEqual(regExpA, regExpB) {
351
- return regExpA.source === regExpB.source && regExpA.global === regExpB.global && regExpA.ignoreCase === regExpB.ignoreCase && regExpA.multiline === regExpB.multiline && regExpA.unicode === regExpB.unicode && regExpA.sticky === regExpB.sticky && regExpA.lastIndex === regExpB.lastIndex;
352
- };
353
- /**
354
- * @function areSetsEqual
355
- *
356
- * @description
357
- * are the sets equal in value
358
- *
359
- * @param {Set} setA the set to test
360
- * @param {Set} setB the set to test against
361
- * @param {function} isEqual the comparator to determine equality
362
- * @param {any} meta the meta set to pass through
363
- * @returns {boolean} are the sets equal
364
- */
365
-
366
-
367
- exports.areRegExpsEqual = areRegExpsEqual;
368
-
369
- var areSetsEqual = function areSetsEqual(setA, setB, isEqual, meta) {
370
- if (setA.size !== setB.size) {
371
- return false;
372
- }
373
-
374
- var valuesA = toValues(setA);
375
- var valuesB = toValues(setB);
376
-
377
- for (var index = 0; index < valuesA.length; index++) {
378
- if (!hasValue(valuesB, valuesA[index], isEqual, meta) || !hasValue(valuesA, valuesB[index], isEqual, meta)) {
379
- return false;
380
- }
381
- }
382
-
383
- return true;
384
- };
385
-
386
- exports.areSetsEqual = areSetsEqual;
@@ -1,86 +0,0 @@
1
- // constants
2
- import { HAS_MAP_SUPPORT, HAS_SET_SUPPORT } from './constants'; // utils
3
-
4
- import { areArraysEqual, areMapsEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, isPlainObject, isPromiseLike, sameValueZeroEqual } from './utils';
5
- var isArray = Array.isArray;
6
-
7
- var createComparator = function createComparator(createIsEqual) {
8
- // eslint-disable-next-line no-use-before-define
9
- var isEqual = typeof createIsEqual === 'function' ? createIsEqual(comparator) : comparator;
10
- /**
11
- * @function comparator
12
- *
13
- * @description
14
- * compare the value of the two objects and return true if they are equivalent in values
15
- *
16
- * @param {any} objectA the object to test against
17
- * @param {any} objectB the object to test
18
- * @param {any} [meta] an optional meta object that is passed through to all equality test calls
19
- * @returns {boolean} are objectA and objectB equivalent in value
20
- */
21
-
22
- function comparator(objectA, objectB, meta) {
23
- if (sameValueZeroEqual(objectA, objectB)) {
24
- return true;
25
- }
26
-
27
- var typeOfA = typeof objectA;
28
-
29
- if (typeOfA !== typeof objectB || typeOfA !== 'object' || !objectA || !objectB) {
30
- return false;
31
- }
32
-
33
- if (isPlainObject(objectA) && isPlainObject(objectB)) {
34
- return areObjectsEqual(objectA, objectB, isEqual, meta);
35
- }
36
-
37
- var arrayA = isArray(objectA);
38
- var arrayB = isArray(objectB);
39
-
40
- if (arrayA || arrayB) {
41
- return arrayA === arrayB && areArraysEqual(objectA, objectB, isEqual, meta);
42
- }
43
-
44
- var dateA = objectA instanceof Date;
45
- var dateB = objectB instanceof Date;
46
-
47
- if (dateA || dateB) {
48
- return dateA === dateB && sameValueZeroEqual(objectA.getTime(), objectB.getTime());
49
- }
50
-
51
- var regexpA = objectA instanceof RegExp;
52
- var regexpB = objectB instanceof RegExp;
53
-
54
- if (regexpA || regexpB) {
55
- return regexpA === regexpB && areRegExpsEqual(objectA, objectB);
56
- }
57
-
58
- if (isPromiseLike(objectA) || isPromiseLike(objectB)) {
59
- return objectA === objectB;
60
- }
61
-
62
- if (HAS_MAP_SUPPORT) {
63
- var mapA = objectA instanceof Map;
64
- var mapB = objectB instanceof Map;
65
-
66
- if (mapA || mapB) {
67
- return mapA === mapB && areMapsEqual(objectA, objectB, isEqual, meta);
68
- }
69
- }
70
-
71
- if (HAS_SET_SUPPORT) {
72
- var setA = objectA instanceof Set;
73
- var setB = objectB instanceof Set;
74
-
75
- if (setA || setB) {
76
- return setA === setB && areSetsEqual(objectA, objectB, isEqual, meta);
77
- }
78
- }
79
-
80
- return areObjectsEqual(objectA, objectB, isEqual, meta);
81
- }
82
-
83
- return comparator;
84
- };
85
-
86
- export default createComparator;
package/mjs/constants.mjs DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * @constant {boolean} HAS_MAP_SUPPORT
3
- */
4
- export var HAS_MAP_SUPPORT = typeof Map === 'function';
5
- /**
6
- * @constant {boolean} HAS_SET_SUPPORT
7
- */
8
-
9
- export var HAS_SET_SUPPORT = typeof Set === 'function';
10
- /**
11
- * @constant {boolean} HAS_WEAKSET_SUPPORT
12
- */
13
-
14
- export var HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function';
package/mjs/index.mjs DELETED
@@ -1,19 +0,0 @@
1
- // comparator
2
- import createCustomEqual from './comparator'; // utils
3
-
4
- import { createCircularEqual, sameValueZeroEqual } from './utils';
5
- export { createCustomEqual, sameValueZeroEqual };
6
- export var circularDeepEqual = createCustomEqual(createCircularEqual());
7
- export var circularShallowEqual = createCustomEqual(createCircularEqual(sameValueZeroEqual));
8
- export var deepEqual = createCustomEqual();
9
- export var shallowEqual = createCustomEqual(function () {
10
- return sameValueZeroEqual;
11
- });
12
- export default {
13
- circularDeep: circularDeepEqual,
14
- circularShallow: circularShallowEqual,
15
- createCustom: createCustomEqual,
16
- deep: deepEqual,
17
- sameValueZero: sameValueZeroEqual,
18
- shallow: shallowEqual
19
- };