Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
383c0a3939 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.31.0",
|
"version": "0.31.1",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
+7
-3
@@ -255,7 +255,8 @@ export function includes(target: any, values: any, allowMissing = false): boolea
|
|||||||
* @param {string} [path] - Internal, current dot-path
|
* @param {string} [path] - Internal, current dot-path
|
||||||
* @returns {boolean} True if they match
|
* @returns {boolean} True if they match
|
||||||
*/
|
*/
|
||||||
export function isEqual(a: any, b: any, compare?: string[] | ((key: string, a: any, b: any) => boolean | void), seen = new WeakMap<object, object>(), path = ''): boolean {
|
export function isEqual(a: any, b: any, compare?: string[] | ((key: string, a: any, b: any) => boolean | void)): boolean {
|
||||||
|
const eq = (a: any, b: any, compare: any, seen: WeakMap<object, object>, path: string) => {
|
||||||
if(Array.isArray(compare)) {
|
if(Array.isArray(compare)) {
|
||||||
if(compare.some(p => new RegExp(`^${p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^.]*')}(\\.|$)`).test(path))) return true;
|
if(compare.some(p => new RegExp(`^${p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^.]*')}(\\.|$)`).test(path))) return true;
|
||||||
} else if(typeof compare === 'function') {
|
} else if(typeof compare === 'function') {
|
||||||
@@ -275,18 +276,21 @@ export function isEqual(a: any, b: any, compare?: string[] | ((key: string, a: a
|
|||||||
const isArrayA = Array.isArray(a), isArrayB = Array.isArray(b);
|
const isArrayA = Array.isArray(a), isArrayB = Array.isArray(b);
|
||||||
if(isArrayA && isArrayB) {
|
if(isArrayA && isArrayB) {
|
||||||
if(a.length !== b.length) return false;
|
if(a.length !== b.length) return false;
|
||||||
for(let i = 0; i < a.length; i++) if(!isEqual(a[i], b[i], compare, seen, path ? `${path}.${i}` : `${i}`)) return false;
|
for(let i = 0; i < a.length; i++) if(!eq(a[i], b[i], compare, seen, path ? `${path}.${i}` : `${i}`)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(isArrayA !== isArrayB) return false;
|
if(isArrayA !== isArrayB) return false;
|
||||||
const keysA = Object.keys(a), keysB = Object.keys(b);
|
const keysA = Object.keys(a), keysB = Object.keys(b);
|
||||||
if(keysA.length !== keysB.length) return false;
|
if(keysA.length !== keysB.length) return false;
|
||||||
for(const key of keysA) {
|
for(const key of keysA) {
|
||||||
if(!Object.prototype.hasOwnProperty.call(b, key) || !isEqual(a[key], b[key], compare, seen, path ? `${path}.${key}` : key)) return false;
|
if(!Object.prototype.hasOwnProperty.call(b, key) || !eq(a[key], b[key], compare, seen, path ? `${path}.${key}` : key)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return eq(a, b, compare, new WeakMap(), '');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Experimental: Combine multiple object prototypes into one
|
* Experimental: Combine multiple object prototypes into one
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user