UNPKG

alepha

Version:

Easy-to-use modern TypeScript framework for building many kind of applications.

37 lines (36 loc) 823 B
/** * Shallow structural equality over own enumerable keys. * * Intended as the `equality` argument of `useSelector` when the selector * builds a fresh object/array on every call (e.g. `(s) => ({ a: s.a })`). */ export const shallowEqual = (a: unknown, b: unknown): boolean => { if (Object.is(a, b)) { return true; } if ( typeof a !== "object" || a === null || typeof b !== "object" || b === null ) { return false; } const keysA = Object.keys(a); const keysB = Object.keys(b); if (keysA.length !== keysB.length) { return false; } for (const key of keysA) { if ( !Object.hasOwn(b, key) || !Object.is( (a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key], ) ) { return false; } } return true; };