2017-09-01 21:25:48 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-04-23 20:17:31 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2016-12-12 00:29:13 +00:00
|
|
|
/**
|
|
|
|
* Sets specific properties of a specific state to specific values and prevents
|
|
|
|
* unnecessary state changes.
|
|
|
|
*
|
|
|
|
* @param {Object} target - The state on which the specified properties are to
|
|
|
|
* be set.
|
|
|
|
* @param {Object} source - The map of properties to values which are to be set
|
|
|
|
* on the specified target.
|
|
|
|
* @returns {Object} The specified target if the values of the specified
|
|
|
|
* properties equal the specified values; otherwise, a new state constructed
|
|
|
|
* from the specified target by setting the specified properties to the
|
|
|
|
* specified values.
|
|
|
|
*/
|
2017-09-01 21:25:48 +00:00
|
|
|
export function assign(target: Object, source: Object) {
|
2016-12-12 00:29:13 +00:00
|
|
|
let t = target;
|
|
|
|
|
|
|
|
for (const property in source) { // eslint-disable-line guard-for-in
|
2017-04-22 22:57:08 +00:00
|
|
|
t = set(t, property, source[property], t === target);
|
2016-12-12 00:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2017-04-23 20:17:31 +00:00
|
|
|
/**
|
|
|
|
* Determines whether {@code a} equals {@code b} according to deep comparison
|
|
|
|
* (which makes sense for Redux and its state definition).
|
|
|
|
*
|
|
|
|
* @param {*} a - The value to compare to {@code b}.
|
|
|
|
* @param {*} b - The value to compare to {@code a}.
|
|
|
|
* @returns {boolean} True if {@code a} equals {@code b} (according to deep
|
|
|
|
* comparison); false, otherwise.
|
|
|
|
*/
|
2017-09-01 21:25:48 +00:00
|
|
|
export function equals(a: any, b: any) {
|
2017-04-23 20:17:31 +00:00
|
|
|
return _.isEqual(a, b);
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:14:50 +00:00
|
|
|
/**
|
|
|
|
* Sets a specific property of a specific state to a specific value. Prevents
|
|
|
|
* unnecessary state changes (when the specified <tt>value</tt> is equal to the
|
|
|
|
* value of the specified <tt>property</tt> of the specified <tt>state</tt>).
|
|
|
|
*
|
|
|
|
* @param {Object} state - The (Redux) state from which a new state is to be
|
|
|
|
* constructed by setting the specified <tt>property</tt> to the specified
|
|
|
|
* <tt>value</tt>.
|
|
|
|
* @param {string} property - The property of <tt>state</tt> which is to be
|
|
|
|
* assigned the specified <tt>value</tt> (in the new state).
|
|
|
|
* @param {*} value - The value to assign to the specified <tt>property</tt>.
|
|
|
|
* @returns {Object} The specified <tt>state</tt> if the value of the specified
|
|
|
|
* <tt>property</tt> equals the specified <tt>value/tt>; otherwise, a new state
|
|
|
|
* constructed from the specified <tt>state</tt> by setting the specified
|
|
|
|
* <tt>property</tt> to the specified <tt>value</tt>.
|
|
|
|
*/
|
2017-09-01 21:25:48 +00:00
|
|
|
export function set(state: Object, property: string, value: any) {
|
2017-04-22 22:57:08 +00:00
|
|
|
return _set(state, property, value, /* copyOnWrite */ true);
|
2016-12-12 00:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable max-params */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a specific property of a specific state to a specific value. Prevents
|
|
|
|
* unnecessary state changes (when the specified <tt>value</tt> is equal to the
|
|
|
|
* value of the specified <tt>property</tt> of the specified <tt>state</tt>).
|
|
|
|
*
|
|
|
|
* @param {Object} state - The (Redux) state from which a state is to be
|
|
|
|
* constructed by setting the specified <tt>property</tt> to the specified
|
|
|
|
* <tt>value</tt>.
|
|
|
|
* @param {string} property - The property of <tt>state</tt> which is to be
|
|
|
|
* assigned the specified <tt>value</tt>.
|
|
|
|
* @param {*} value - The value to assign to the specified <tt>property</tt>.
|
|
|
|
* @param {boolean} copyOnWrite - If the specified <tt>state</tt> is to not be
|
|
|
|
* modified, <tt>true</tt>; otherwise, <tt>false</tt>.
|
|
|
|
* @returns {Object} The specified <tt>state</tt> if the value of the specified
|
|
|
|
* <tt>property</tt> equals the specified <tt>value/tt> or <tt>copyOnWrite</tt>
|
|
|
|
* is truthy; otherwise, a new state constructed from the specified
|
|
|
|
* <tt>state</tt> by setting the specified <tt>property</tt> to the specified
|
|
|
|
* <tt>value</tt>.
|
|
|
|
*/
|
2017-09-01 21:25:48 +00:00
|
|
|
function _set(
|
|
|
|
state: Object,
|
|
|
|
property: string,
|
|
|
|
value: any,
|
|
|
|
copyOnWrite: boolean) {
|
2016-12-05 15:14:50 +00:00
|
|
|
// Delete state properties that are to be set to undefined. (It is a matter
|
|
|
|
// of personal preference, mostly.)
|
|
|
|
if (typeof value === 'undefined'
|
|
|
|
&& Object.prototype.hasOwnProperty.call(state, property)) {
|
2016-12-12 00:29:13 +00:00
|
|
|
const newState = copyOnWrite ? { ...state } : state;
|
2016-12-05 15:14:50 +00:00
|
|
|
|
|
|
|
if (delete newState[property]) {
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state[property] !== value) {
|
2016-12-12 00:29:13 +00:00
|
|
|
if (copyOnWrite) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
[property]: value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
state[property] = value;
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
2016-12-12 00:29:13 +00:00
|
|
|
|
|
|
|
/* eslint-enable max-params */
|
2017-09-01 21:25:48 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-18 20:52:10 +00:00
|
|
|
* Returns redux state from the specified <tt>stateful</tt> which is presumed to
|
|
|
|
* be related to the redux state (e.g. the redux store, the redux
|
|
|
|
* <tt>getState</tt> function).
|
2017-09-01 21:25:48 +00:00
|
|
|
*
|
2017-09-18 20:52:10 +00:00
|
|
|
* @param {Function|Object} stateful - The entity such as the redux store or the
|
|
|
|
* redux <tt>getState</tt> function from which the redux state is to be
|
|
|
|
* returned.
|
2017-09-01 21:25:48 +00:00
|
|
|
* @returns {Object} The redux state.
|
|
|
|
*/
|
2017-09-18 20:52:10 +00:00
|
|
|
export function toState(stateful: Function | Object) {
|
|
|
|
if (stateful) {
|
|
|
|
if (typeof stateful === 'function') {
|
|
|
|
return stateful();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { getState } = stateful;
|
|
|
|
|
2017-09-25 17:31:19 +00:00
|
|
|
if (typeof getState === 'function') {
|
2017-09-18 20:52:10 +00:00
|
|
|
return getState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateful;
|
2017-09-01 21:25:48 +00:00
|
|
|
}
|