Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.ts 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { isEqual } from 'lodash-es';
  2. import { IReduxState, IStore } from '../../app/types';
  3. import { IStateful } from '../app/types';
  4. /**
  5. * Sets specific properties of a specific state to specific values and prevents
  6. * unnecessary state changes.
  7. *
  8. * @param {T} target - The state on which the specified properties are to
  9. * be set.
  10. * @param {T} source - The map of properties to values which are to be set
  11. * on the specified target.
  12. * @returns {T} The specified target if the values of the specified
  13. * properties equal the specified values; otherwise, a new state constructed
  14. * from the specified target by setting the specified properties to the
  15. * specified values.
  16. */
  17. export function assign<T extends Object>(target: T, source: Partial<T>): T {
  18. let t = target;
  19. for (const property in source) { // eslint-disable-line guard-for-in
  20. t = _set(t, property, source[property], t === target);
  21. }
  22. return t;
  23. }
  24. /**
  25. * Determines whether {@code a} equals {@code b} according to deep comparison
  26. * (which makes sense for Redux and its state definition).
  27. *
  28. * @param {*} a - The value to compare to {@code b}.
  29. * @param {*} b - The value to compare to {@code a}.
  30. * @returns {boolean} True if {@code a} equals {@code b} (according to deep
  31. * comparison); false, otherwise.
  32. */
  33. export function equals(a: any, b: any) {
  34. return isEqual(a, b);
  35. }
  36. /**
  37. * Sets a specific property of a specific state to a specific value. Prevents
  38. * unnecessary state changes (when the specified {@code value} is equal to the
  39. * value of the specified {@code property} of the specified {@code state}).
  40. *
  41. * @param {T} state - The (Redux) state from which a new state is to be
  42. * constructed by setting the specified {@code property} to the specified
  43. * {@code value}.
  44. * @param {string} property - The property of {@code state} which is to be
  45. * assigned the specified {@code value} (in the new state).
  46. * @param {*} value - The value to assign to the specified {@code property}.
  47. * @returns {T} The specified {@code state} if the value of the specified
  48. * {@code property} equals the specified <tt>value/tt>; otherwise, a new state
  49. * constructed from the specified {@code state} by setting the specified
  50. * {@code property} to the specified {@code value}.
  51. */
  52. export function set<T extends Object>(state: T, property: keyof T, value: any): T {
  53. return _set(state, property, value, /* copyOnWrite */ true);
  54. }
  55. /**
  56. * Sets a specific property of a specific state to a specific value. Prevents
  57. * unnecessary state changes (when the specified {@code value} is equal to the
  58. * value of the specified {@code property} of the specified {@code state}).
  59. *
  60. * @param {T} state - The (Redux) state from which a state is to be
  61. * constructed by setting the specified {@code property} to the specified
  62. * {@code value}.
  63. * @param {string} property - The property of {@code state} which is to be
  64. * assigned the specified {@code value}.
  65. * @param {*} value - The value to assign to the specified {@code property}.
  66. * @param {boolean} copyOnWrite - If the specified {@code state} is to not be
  67. * modified, {@code true}; otherwise, {@code false}.
  68. * @returns {T} The specified {@code state} if the value of the specified
  69. * {@code property} equals the specified <tt>value/tt> or {@code copyOnWrite}
  70. * is truthy; otherwise, a new state constructed from the specified
  71. * {@code state} by setting the specified {@code property} to the specified
  72. * {@code value}.
  73. */
  74. function _set<T extends Object>(
  75. state: T,
  76. property: keyof T,
  77. value: any,
  78. copyOnWrite: boolean): T {
  79. // Delete state properties that are to be set to undefined. (It is a matter
  80. // of personal preference, mostly.)
  81. if (typeof value === 'undefined'
  82. && Object.prototype.hasOwnProperty.call(state, property)) {
  83. const newState = copyOnWrite ? { ...state } : state;
  84. if (delete newState[property]) {
  85. return newState;
  86. }
  87. }
  88. if (state[property] !== value) {
  89. if (copyOnWrite) {
  90. return {
  91. ...state,
  92. [property]: value
  93. };
  94. }
  95. state[property] = value;
  96. }
  97. return state;
  98. }
  99. /* eslint-enable max-params */
  100. /**
  101. * Whether or not the entity is of type IStore.
  102. *
  103. * @param {IStateful} stateful - The entity to check.
  104. * @returns {boolean}
  105. */
  106. function isStore(stateful: IStateful): stateful is IStore {
  107. return 'getState' in stateful && typeof stateful.getState === 'function';
  108. }
  109. /**
  110. * Returns redux state from the specified {@code stateful} which is presumed to
  111. * be related to the redux state (e.g. The redux store, the redux
  112. * {@code getState} function).
  113. *
  114. * @param {Function|IStore} stateful - The entity such as the redux store or the
  115. * redux {@code getState} function from which the redux state is to be
  116. * returned.
  117. * @returns {Object} The redux state.
  118. */
  119. export function toState(stateful: IStateful): IReduxState {
  120. if (stateful) {
  121. if (typeof stateful === 'function') {
  122. return stateful();
  123. }
  124. if (isStore(stateful)) {
  125. return stateful.getState();
  126. }
  127. }
  128. return stateful;
  129. }