47 lines
919 B
JavaScript
47 lines
919 B
JavaScript
![]() |
export function stateToggler(key) {
|
||
|
return state => ({ [key]: !Boolean(state[key]) });
|
||
|
}
|
||
|
|
||
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||
|
|
||
|
function is(x, y) {
|
||
|
// SameValue algorithm
|
||
|
if (x === y) {
|
||
|
// Steps 1-5, 7-10
|
||
|
// Steps 6.b-6.e: +0 != -0
|
||
|
return x !== 0 || 1 / x === 1 / y;
|
||
|
} else {
|
||
|
// Step 6.a: NaN == NaN
|
||
|
return x !== x && y !== y;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function shallowEqual(objA, objB) {
|
||
|
if (is(objA, objB)) return true;
|
||
|
|
||
|
if (
|
||
|
typeof objA !== "object" ||
|
||
|
objA === null ||
|
||
|
typeof objB !== "object" ||
|
||
|
objB === null
|
||
|
) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
const keysA = Object.keys(objA);
|
||
|
const keysB = Object.keys(objB);
|
||
|
|
||
|
if (keysA.length !== keysB.length) return false;
|
||
|
|
||
|
for (let i = 0; i <= keysA.length; i++) {
|
||
|
if (
|
||
|
!hasOwnProperty.call(objB, keysA[i]) ||
|
||
|
!is(objA[keysA[i]], objB[keysA[i]])
|
||
|
) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|