prettier/website/playground/helpers.js

50 lines
914 B
JavaScript
Raw Normal View History

2018-04-12 00:22:03 +03:00
export function stateToggler(key) {
2018-04-17 23:29:44 +03:00
return state => ({ [key]: !state[key] });
2018-04-12 00:22:03 +03:00
}
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;
}
2018-04-17 23:29:44 +03:00
// Step 6.a: NaN == NaN
return x !== x && y !== y;
2018-04-12 00:22:03 +03:00
}
export function shallowEqual(objA, objB) {
2018-04-17 23:29:44 +03:00
if (is(objA, objB)) {
return true;
}
2018-04-12 00:22:03 +03:00
if (
typeof objA !== "object" ||
objA === null ||
typeof objB !== "object" ||
objB === null
) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
2018-04-17 23:29:44 +03:00
if (keysA.length !== keysB.length) {
return false;
}
2018-04-12 00:22:03 +03:00
2018-04-17 22:09:37 +03:00
for (let i = 0; i < keysA.length; i++) {
2018-04-12 00:22:03 +03:00
if (
!hasOwnProperty.call(objB, keysA[i]) ||
!is(objA[keysA[i]], objB[keysA[i]])
) {
return false;
}
}
return true;
}