shapey
Version:
A simple syntax for remapping objects, inspired by several of Ramda's spec based functions
43 lines (35 loc) • 1.59 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _curry = _interopRequireDefault(require("vanillas/curry"));
var _flatten = _interopRequireDefault(require("vanillas/flatten"));
var _isObject = _interopRequireDefault(require("vanillas/isObject"));
var _merge = _interopRequireDefault(require("vanillas/merge"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Combines two values of the same type (if it makes sense to combine them).
* Numbers are summarized, strings and arrays are concatenated together, and true objects are merged (the second value merged on top of the first).
* In any other case only the first value is returned.
*
* @function
* @name combine
* @sig a -> a -> a
* @param {*} val1 The base value to be combined with
* @param {*} val2 The value to combine
* @returns {*} If the values are of the same type, this represents the combined value of the two of them. Otherwise only the first value is returned
*/
var _default = (0, _curry.default)(function (val1, val2) {
if (typeof val1 === 'number' && typeof val2 === 'number') {
return val1 + val2;
} else if (typeof val1 === 'string' && typeof val2 === 'string') {
return [val1, val2].join('');
} else if (Array.isArray(val1) && Array.isArray(val2)) {
return (0, _flatten.default)([val1, val2]);
} else if ((0, _isObject.default)(val1) && (0, _isObject.default)(val2)) {
return (0, _merge.default)(val1, val2);
} else {
return val1;
}
});
exports.default = _default;
module.exports = exports.default;