-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
I encountered the necessity to compare some generated data with test data, where those data sets include dynamically generated id values nested in different levels. So the goal was to test the object for equality by ignoring/ommiting those id_ keys.
E.G.
// test for equality but ignore the values of id
{
id: 1234,
a: 'foo',
b: {
id: 45678
c: 'bar'
}
}
I cerated my own - much simpler deepEql_ function
const deepEql = (a: unknown, b: unknown, omitKeys: string[] = []): boolean => {
if (a === b) return true;
omitKeys.forEach(key => delete a[key]);
omitKeys.forEach(key => delete b[key]);
if (typeof a !== 'object' || typeof b !== 'object' || a == null || b == null) return false;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
// eslint-disable-next-line no-restricted-syntax
for (const key of keysA) {
if (!keysB.includes(key)) return false;
if (typeof a[key] === 'function' || typeof b[key] === 'function') {
if (a[key].toString() !== b[key].toString()) return false;
} else if (!this.deepEql(a[key], b[key], omitKeys)) return false;
}
return true;
}
which works in my case, but I would love to see the possibility to specify which keys to exclude in the deep-eql api which seems obviously much more reliable than my solution
Metadata
Metadata
Assignees
Labels
No labels