@lowentry/utils 1.19.4 → 1.20.1
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/build/LeUtils.d.ts +1 -0
- package/index.d.ts +1 -0
- package/index.js +63 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +70 -1
- package/tests/equals.test.js +216 -0
package/build/LeUtils.d.ts
CHANGED
package/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare function ISSET(value: any): boolean;
|
|
|
24
24
|
|
|
25
25
|
export declare namespace LeUtils {
|
|
26
26
|
export function equals(value: any, other: any): boolean;
|
|
27
|
+
export function equalsMapLike(elementsA: any, elementsB: any, ignoreKeys?: any[]): boolean;
|
|
27
28
|
export function clone(value: any): any;
|
|
28
29
|
export function onDomReady(callback: Function): {
|
|
29
30
|
remove: Function;
|
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
|
|
2
2
|
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
3
|
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
|
|
4
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
5
4
|
import _typeof from '@babel/runtime/helpers/typeof';
|
|
5
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
6
6
|
import '@babel/runtime/helpers/awaitAsyncGenerator';
|
|
7
7
|
import _wrapAsyncGenerator from '@babel/runtime/helpers/wrapAsyncGenerator';
|
|
8
8
|
import _regeneratorRuntime from '@babel/runtime/regenerator';
|
|
@@ -257,6 +257,68 @@ var LeUtils = {
|
|
|
257
257
|
equals: function equals(value, other) {
|
|
258
258
|
return FastDeepEqual(value, other);
|
|
259
259
|
},
|
|
260
|
+
/**
|
|
261
|
+
* Performs a deep equality comparison between two collections (objects, maps, arrays, etc), sorting on the keys before comparing them.
|
|
262
|
+
*
|
|
263
|
+
* This is useful for comparing objects that have the same properties, but in a different order, and/or in a different collection type (like Maps vs Objects).
|
|
264
|
+
*
|
|
265
|
+
* @param {*} elementsA The elements to compare.
|
|
266
|
+
* @param {*} elementsB The other elements to compare.
|
|
267
|
+
* @param {string[]} [ignoreKeys=[]] An array of keys to ignore when comparing the elements. This is useful for ignoring properties that are not relevant for the comparison.
|
|
268
|
+
* @return {boolean} Returns true if the given values are equivalent, ignoring the order of properties.
|
|
269
|
+
*/
|
|
270
|
+
equalsMapLike: function () {
|
|
271
|
+
var sortKeyValueArrays = function sortKeyValueArrays(pairA, pairB) {
|
|
272
|
+
return LeUtils.compare(pairA[0], pairB[0]);
|
|
273
|
+
};
|
|
274
|
+
return function (elementsA, elementsB) {
|
|
275
|
+
var ignoreKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
276
|
+
elementsA = LeUtils.mapToArray(elementsA, function (value, key) {
|
|
277
|
+
return [key, value];
|
|
278
|
+
}).sort(sortKeyValueArrays);
|
|
279
|
+
elementsB = LeUtils.mapToArray(elementsB, function (value, key) {
|
|
280
|
+
return [key, value];
|
|
281
|
+
}).sort(sortKeyValueArrays);
|
|
282
|
+
ignoreKeys = typeof ignoreKeys === 'string' ? ARRAY(ignoreKeys) : LeUtils.mapToArray(ignoreKeys);
|
|
283
|
+
var indexA = 0;
|
|
284
|
+
var indexB = 0;
|
|
285
|
+
while (indexA < elementsA.length && indexB < elementsB.length) {
|
|
286
|
+
var _elementsA$indexA = _slicedToArray(elementsA[indexA], 2),
|
|
287
|
+
mapKey = _elementsA$indexA[0],
|
|
288
|
+
mapValue = _elementsA$indexA[1];
|
|
289
|
+
var _elementsB$indexB = _slicedToArray(elementsB[indexB], 2),
|
|
290
|
+
ownMapKey = _elementsB$indexB[0],
|
|
291
|
+
ownMapValue = _elementsB$indexB[1];
|
|
292
|
+
var ignoreKeysIncludesMapKey = ignoreKeys.includes(mapKey);
|
|
293
|
+
var ignoreKeysIncludesOwnMapKey = ignoreKeys.includes(ownMapKey);
|
|
294
|
+
if (ignoreKeysIncludesMapKey) {
|
|
295
|
+
indexA++;
|
|
296
|
+
if (ignoreKeysIncludesOwnMapKey) {
|
|
297
|
+
indexB++;
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
} else if (ignoreKeysIncludesOwnMapKey) {
|
|
301
|
+
indexB++;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (!LeUtils.equals(mapKey, ownMapKey) || !LeUtils.equals(mapValue, ownMapValue)) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
indexA++;
|
|
308
|
+
indexB++;
|
|
309
|
+
}
|
|
310
|
+
while (indexA < elementsA.length && ignoreKeys.includes(elementsA[indexA][0])) {
|
|
311
|
+
indexA++;
|
|
312
|
+
}
|
|
313
|
+
if (indexA < elementsA.length) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
while (indexB < elementsB.length && ignoreKeys.includes(elementsB[indexB][0])) {
|
|
317
|
+
indexB++;
|
|
318
|
+
}
|
|
319
|
+
return indexB >= elementsB.length;
|
|
320
|
+
};
|
|
321
|
+
}(),
|
|
260
322
|
/**
|
|
261
323
|
* Returns a deep copy of the given value.
|
|
262
324
|
*
|