ruddy
Version:
Modularized state-management tools for modern front-end applications. Manage dispatched messages in a clean and predictable way for either small or large scale projects
91 lines (82 loc) • 3.71 kB
JavaScript
import _default6 from 'ramda/src/mergeDeepRight';
import _default5 from 'ramda/src/isNil';
import _default4 from 'ramda/src/type';
import _default3 from 'ramda/src/always';
import _default2 from 'ramda/src/is';
import _default from 'ramda/src/unless';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
import { isPlainObj, isPrimitiveish } from './is';
/**
* Takes a prop of any type and depending will wrap it in an array -
* unless the prop is an array, in which case it will be returned [as-is](http://ramdajs.com/docs/#identity)
*
* @func
* @sig * -> [*]
* @param {Array|*} p A prop of any type (arrays will be returned as-is)
* @returns {Array} either the original prop (if it was an array) or an Array wrapping the original prop
*/
export var coerceToArray = _default(_default2(Array), Array);
/**
* Takes a prop of any type and depending will [wrap a function around it](http://ramdajs.com/docs/#always) -
* unless the prop is a function, in which case it will be returned [as-is](http://ramdajs.com/docs/#identity)
*
* @func
* @sig * -> (() -> *)
* @param {Function|*} p A prop of any type (functions will be returned as-is)
* @returns {Function} either the original prop (if it was a Function) or a Function wrapping the original prop
*/
export var coerceToFn = _default(_default2(Function), _default3);
/**
* Converts a value of any type to [its string equivalent](http://ramdajs.com/docs/#toString),
* but passes through a String value [as-is](http://ramdajs.com/docs/#identity)
*
* @func
* @sig * -> String
* @param {*} s A value of any type (strings will be returned as-is)
* @returns {String} either the original prop (if it was a String) or a stringified rendering of the original prop
*/
export var coerceToString = _default(_default2(String), toString);
/**
* Simplest of common reducer functions that merges an array of key/value pairs into a single object
*
* @func
* @sig ({k: v}, [k, v]) -> {k: v}
* @param {Object} returnObj Accumulator object that each key/val pair will be
* merged into
* @param {Array} pairs A simple key/value pair array (just two indexes in the array)
* @returns {Objedt} The merged object of key/value pairs
*/
export var listOfPairsToOneObject = function listOfPairsToOneObject(returnObj, _ref) {
var _extends2;
var key = _ref[0],
val = _ref[1];
return _extends({}, returnObj, (_extends2 = {}, _extends2[key] = val, _extends2));
};
/**
* A function which merges two Objects, with the latter merging over the former
* upon finding any duplicate fields. Also will return one or the other in full
* (un-merged) if one of the params is not an Object.
*
* @func
* @sig ({k: v}, {k: v}) -> *
* @param {Object} parent An Object whose props will have lower precedence when
* merged with another Object
* @param {Object} child An Object whose props will have higher precedence when
* merged with another Object
* @returns {*} the merged result of two values
*/
export var simpleMergeStrategy = function simpleMergeStrategy(parent, child) {
if (_default4(parent) !== _default4(child) || _default5(child)) {
if (_default2(Array, parent) && !_default5(child)) {
return [].concat(parent, coerceToArray(child));
}
if (!isPlainObj(parent)) {
return parent;
}
} else if (isPrimitiveish(child) || _default2(Function, child)) {
return child;
} else if (_default2(Array, parent)) {
return [].concat(parent, coerceToArray(child));
}
return _default6(parent, child);
};