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
115 lines (92 loc) • 4.56 kB
JavaScript
;
exports.__esModule = true;
exports.simpleMergeStrategy = exports.listOfPairsToOneObject = exports.coerceToString = exports.coerceToFn = exports.coerceToArray = undefined;
var _mergeDeepRight = require('ramda/src/mergeDeepRight');
var _mergeDeepRight2 = _interopRequireDefault(_mergeDeepRight);
var _isNil = require('ramda/src/isNil');
var _isNil2 = _interopRequireDefault(_isNil);
var _type = require('ramda/src/type');
var _type2 = _interopRequireDefault(_type);
var _always = require('ramda/src/always');
var _always2 = _interopRequireDefault(_always);
var _is = require('ramda/src/is');
var _is2 = _interopRequireDefault(_is);
var _unless = require('ramda/src/unless');
var _unless2 = _interopRequireDefault(_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; };
var _is3 = require('./is');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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
*/
var coerceToArray = exports.coerceToArray = (0, _unless2.default)((0, _is2.default)(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
*/
var coerceToFn = exports.coerceToFn = (0, _unless2.default)((0, _is2.default)(Function), _always2.default);
/**
* 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
*/
var coerceToString = exports.coerceToString = (0, _unless2.default)((0, _is2.default)(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
*/
var listOfPairsToOneObject = exports.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
*/
var simpleMergeStrategy = exports.simpleMergeStrategy = function simpleMergeStrategy(parent, child) {
if ((0, _type2.default)(parent) !== (0, _type2.default)(child) || (0, _isNil2.default)(child)) {
if ((0, _is2.default)(Array, parent) && !(0, _isNil2.default)(child)) {
return [].concat(parent, coerceToArray(child));
}
if (!(0, _is3.isPlainObj)(parent)) {
return parent;
}
} else if ((0, _is3.isPrimitiveish)(child) || (0, _is2.default)(Function, child)) {
return child;
} else if ((0, _is2.default)(Array, parent)) {
return [].concat(parent, coerceToArray(child));
}
return (0, _mergeDeepRight2.default)(parent, child);
};