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 1.4KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Sets a specific property of a specific state to a specific value. Prevents
  3. * unnecessary state changes (when the specified <tt>value</tt> is equal to the
  4. * value of the specified <tt>property</tt> of the specified <tt>state</tt>).
  5. *
  6. * @param {Object} state - The (Redux) state from which a new state is to be
  7. * constructed by setting the specified <tt>property</tt> to the specified
  8. * <tt>value</tt>.
  9. * @param {string} property - The property of <tt>state</tt> which is to be
  10. * assigned the specified <tt>value</tt> (in the new state).
  11. * @param {*} value - The value to assign to the specified <tt>property</tt>.
  12. * @returns {Object} The specified <tt>state</tt> if the value of the specified
  13. * <tt>property</tt> equals the specified <tt>value/tt>; otherwise, a new state
  14. * constructed from the specified <tt>state</tt> by setting the specified
  15. * <tt>property</tt> to the specified <tt>value</tt>.
  16. */
  17. export function setStateProperty(state, property, value) {
  18. // Delete state properties that are to be set to undefined. (It is a matter
  19. // of personal preference, mostly.)
  20. if (typeof value === 'undefined'
  21. && Object.prototype.hasOwnProperty.call(state, property)) {
  22. const newState = { ...state };
  23. if (delete newState[property]) {
  24. return newState;
  25. }
  26. }
  27. if (state[property] !== value) {
  28. return {
  29. ...state,
  30. [property]: value
  31. };
  32. }
  33. return state;
  34. }