helpers.js 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. export function stateToggler(key) {
  2. return state => ({ [key]: !state[key] });
  3. }
  4. const hasOwnProperty = Object.prototype.hasOwnProperty;
  5. function is(x, y) {
  6. // SameValue algorithm
  7. if (x === y) {
  8. // Steps 1-5, 7-10
  9. // Steps 6.b-6.e: +0 != -0
  10. return x !== 0 || 1 / x === 1 / y;
  11. }
  12. // Step 6.a: NaN == NaN
  13. return x !== x && y !== y;
  14. }
  15. export function shallowEqual(objA, objB) {
  16. if (is(objA, objB)) {
  17. return true;
  18. }
  19. if (
  20. typeof objA !== "object" ||
  21. objA === null ||
  22. typeof objB !== "object" ||
  23. objB === null
  24. ) {
  25. return false;
  26. }
  27. const keysA = Object.keys(objA);
  28. const keysB = Object.keys(objB);
  29. if (keysA.length !== keysB.length) {
  30. return false;
  31. }
  32. for (let i = 0; i < keysA.length; i++) {
  33. if (
  34. !hasOwnProperty.call(objB, keysA[i]) ||
  35. !is(objA[keysA[i]], objB[keysA[i]])
  36. ) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }