19 lines
578 B
TypeScript
19 lines
578 B
TypeScript
export function preserveShallowEqual<T extends object>(current: T, next: T): T {
|
|
if (Object.is(current, next)) return current;
|
|
|
|
const currentRecord = current as Record<string, unknown>;
|
|
const nextRecord = next as Record<string, unknown>;
|
|
const currentKeys = Object.keys(currentRecord);
|
|
const nextKeys = Object.keys(nextRecord);
|
|
|
|
if (currentKeys.length !== nextKeys.length) return next;
|
|
|
|
for (const key of currentKeys) {
|
|
if (!Object.hasOwn(nextRecord, key) || !Object.is(currentRecord[key], nextRecord[key])) {
|
|
return next;
|
|
}
|
|
}
|
|
|
|
return current;
|
|
}
|