You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Sets specific properties of a specific state to specific values and prevents
  3. * unnecessary state changes.
  4. *
  5. * @param {Object} target - The state on which the specified properties are to
  6. * be set.
  7. * @param {Object} source - The map of properties to values which are to be set
  8. * on the specified target.
  9. * @returns {Object} The specified target if the values of the specified
  10. * properties equal the specified values; otherwise, a new state constructed
  11. * from the specified target by setting the specified properties to the
  12. * specified values.
  13. */
  14. export function setStateProperties(target, source) {
  15. let t = target;
  16. for (const property in source) { // eslint-disable-line guard-for-in
  17. t = setStateProperty(t, property, source[property], t === target);
  18. }
  19. return t;
  20. }
  21. /**
  22. * Sets a specific property of a specific state to a specific value. Prevents
  23. * unnecessary state changes (when the specified <tt>value</tt> is equal to the
  24. * value of the specified <tt>property</tt> of the specified <tt>state</tt>).
  25. *
  26. * @param {Object} state - The (Redux) state from which a new state is to be
  27. * constructed by setting the specified <tt>property</tt> to the specified
  28. * <tt>value</tt>.
  29. * @param {string} property - The property of <tt>state</tt> which is to be
  30. * assigned the specified <tt>value</tt> (in the new state).
  31. * @param {*} value - The value to assign to the specified <tt>property</tt>.
  32. * @returns {Object} The specified <tt>state</tt> if the value of the specified
  33. * <tt>property</tt> equals the specified <tt>value/tt>; otherwise, a new state
  34. * constructed from the specified <tt>state</tt> by setting the specified
  35. * <tt>property</tt> to the specified <tt>value</tt>.
  36. */
  37. export function setStateProperty(state, property, value) {
  38. return _setStateProperty(state, property, value, /* copyOnWrite */ true);
  39. }
  40. /* eslint-disable max-params */
  41. /**
  42. * Sets a specific property of a specific state to a specific value. Prevents
  43. * unnecessary state changes (when the specified <tt>value</tt> is equal to the
  44. * value of the specified <tt>property</tt> of the specified <tt>state</tt>).
  45. *
  46. * @param {Object} state - The (Redux) state from which a state is to be
  47. * constructed by setting the specified <tt>property</tt> to the specified
  48. * <tt>value</tt>.
  49. * @param {string} property - The property of <tt>state</tt> which is to be
  50. * assigned the specified <tt>value</tt>.
  51. * @param {*} value - The value to assign to the specified <tt>property</tt>.
  52. * @param {boolean} copyOnWrite - If the specified <tt>state</tt> is to not be
  53. * modified, <tt>true</tt>; otherwise, <tt>false</tt>.
  54. * @returns {Object} The specified <tt>state</tt> if the value of the specified
  55. * <tt>property</tt> equals the specified <tt>value/tt> or <tt>copyOnWrite</tt>
  56. * is truthy; otherwise, a new state constructed from the specified
  57. * <tt>state</tt> by setting the specified <tt>property</tt> to the specified
  58. * <tt>value</tt>.
  59. */
  60. function _setStateProperty(state, property, value, copyOnWrite) {
  61. // Delete state properties that are to be set to undefined. (It is a matter
  62. // of personal preference, mostly.)
  63. if (typeof value === 'undefined'
  64. && Object.prototype.hasOwnProperty.call(state, property)) {
  65. const newState = copyOnWrite ? { ...state } : state;
  66. if (delete newState[property]) {
  67. return newState;
  68. }
  69. }
  70. if (state[property] !== value) {
  71. if (copyOnWrite) {
  72. return {
  73. ...state,
  74. [property]: value
  75. };
  76. }
  77. state[property] = value;
  78. }
  79. return state;
  80. }
  81. /* eslint-enable max-params */