isEqual callback safety
Build / Publish Docs (push) Successful in 29s
Build / Build NPM Project (push) Successful in 32s
Build / Tag Version (push) Successful in 7s

This commit is contained in:
2026-10-08 12:28:30 -04:00
parent f0849255df
commit 383c0a3939
2 changed files with 33 additions and 29 deletions
+1 -1
View File
@@ -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",
+32 -28
View File
@@ -255,36 +255,40 @@ 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 {
if(Array.isArray(compare)) { const eq = (a: any, b: any, compare: any, seen: WeakMap<object, object>, path: string) => {
if(compare.some(p => new RegExp(`^${p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^.]*')}(\\.|$)`).test(path))) return true; if(Array.isArray(compare)) {
} else if(typeof compare === 'function') { if(compare.some(p => new RegExp(`^${p.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^.]*')}(\\.|$)`).test(path))) return true;
const r = compare(path, a, b); } else if(typeof compare === 'function') {
if(r !== undefined) return r; const r = compare(path, a, b);
} if(r !== undefined) return r;
if(a === b) return true; }
if(a instanceof Date && b instanceof Date) return a.getTime() === b.getTime(); if(a === b) return true;
if(a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags; if(a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if(typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) { if(a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
if(Number.isNaN(a) && Number.isNaN(b)) return true; if(typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) {
if(typeof a === 'function' && typeof b === 'function') return a.toString() === b.toString(); if(Number.isNaN(a) && Number.isNaN(b)) return true;
return false; if(typeof a === 'function' && typeof b === 'function') return a.toString() === b.toString();
} return false;
if(seen.has(a)) return seen.get(a) === b; }
seen.set(a, b); if(seen.has(a)) return seen.get(a) === b;
const isArrayA = Array.isArray(a), isArrayB = Array.isArray(b); seen.set(a, b);
if(isArrayA && isArrayB) { const isArrayA = Array.isArray(a), isArrayB = Array.isArray(b);
if(a.length !== b.length) return false; if(isArrayA && isArrayB) {
for(let i = 0; i < a.length; i++) if(!isEqual(a[i], b[i], compare, seen, path ? `${path}.${i}` : `${i}`)) return false; if(a.length !== b.length) 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;
}
if(isArrayA !== isArrayB) return false;
const keysA = Object.keys(a), keysB = Object.keys(b);
if(keysA.length !== keysB.length) return false;
for(const key of keysA) {
if(!Object.prototype.hasOwnProperty.call(b, key) || !eq(a[key], b[key], compare, seen, path ? `${path}.${key}` : key)) return false;
}
return true; return true;
} }
if(isArrayA !== isArrayB) return false;
const keysA = Object.keys(a), keysB = Object.keys(b); return eq(a, b, compare, new WeakMap(), '');
if(keysA.length !== keysB.length) return false;
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;
}
return true;
} }
/** /**