After omitting null
values we should somehow bypass initial call to customizer
without checking that object does not includes
null
values once again.
I've invented some sort of nice bike for such purpose:
const compareWithoutNullProperties = (a: any, b: any) =>
isPlainObject(a) && isPlainObject(b)
? isEqualWith(
omitBy(a, (value) => value == null),
omitBy(b, (value) => value == null),
(a: any, b: any, key?: number | string | symbol): boolean | undefined =>
// Next initial call should be ignored.
key != null ? compareWithoutNullProperties(a, b) : undefined,
)
: undefined;
const isEqualWithoutNullProperties = (a: any, b: any) =>
isEqualWith(a, b, compareWithoutNullProperties);
Tests returning true
:
isEqualWithoutNullProperties({ a: undefined }, {});
isEqualWithoutNullProperties({ a: null }, {});
isEqualWithoutNullProperties([{ a: undefined }], [{}]);
isEqualWithoutNullProperties([{ a: null }], [{}]);
isEqualWithoutNullProperties({ a: { a: undefined } }, { a: {} });
isEqualWithoutNullProperties({ a: { a: null } }, { a: {} });
isEqualWithoutNullProperties({ a: [{ a: undefined }] }, { a: [{}] });
isEqualWithoutNullProperties({ a: [{ a: null }] }, { a: [{}] });
Tests returning false
:
isEqualWithoutNullProperties([undefined], []);
isEqualWithoutNullProperties([null], []);
isEqualWithoutNullProperties([undefined], [1]);
isEqualWithoutNullProperties([null], [1]);
isEqualWithoutNullProperties({ a: undefined }, { a: 1 });
isEqualWithoutNullProperties({ a: null }, { a: 1 });
isEqualWithoutNullProperties([{ a: undefined }], [{ a: 1 }]);
isEqualWithoutNullProperties([{ a: null }], [{ a: 1 }]);
isEqualWithoutNullProperties({ a: { a: undefined } }, { a: { a: 1 } });
isEqualWithoutNullProperties({ a: { a: null } }, { a: { a: 1 } });
isEqualWithoutNullProperties({ a: [{ a: undefined }] }, { a: [{ a: 1 }] });
isEqualWithoutNullProperties({ a: [{ a: null }] }, { a: [{ a: 1 }] });
isEqualWith
does normally.