feat(stores): add shallow comparison helper for Zustand selectors

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
Sondre Engebråten
2026-02-17 13:34:00 +01:00
co-authored by Claude Opus 4.6
parent 3c91793a94
commit e71b3ea8ff
@@ -0,0 +1,36 @@
/**
* Shallow comparison function for Zustand selectors.
* Returns true if a and b are shallowly equal (same reference or same primitives).
*/
export function shallow<T>(objA: T, objB: T): boolean {
if (Object.is(objA, objB)) {
return true;
}
if (
typeof objA !== 'object' ||
objA === null ||
typeof objB !== 'object' ||
objB === null
) {
return false;
}
const keysA = Object.keys(objA as unknown as object);
const keysB = Object.keys(objB as unknown as object);
if (keysA.length !== keysB.length) {
return false;
}
for (const key of keysA) {
if (
!Object.prototype.hasOwnProperty.call(objB, key) ||
!Object.is((objA as any)[key], (objB as any)[key])
) {
return false;
}
}
return true;
}