Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.js 5.3KB

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