crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
39 lines (29 loc) • 1.25 kB
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var curry = require('../core/curry')
var isObject = require('../core/isObject')
var isFunction = require('../core/isFunction')
var isNil = require('../core/isNil')
// applyMap :: ({ (* -> *) }, Object) -> (Object , String) -> Object
var applyMap = function (fns, obj) { return function(acc, key) {
var obj$1, obj$2, obj$3;
if(isNil(fns[key])) {
return Object.assign({}, acc, ( obj$1 = {}, obj$1[key] = obj[key], obj$1 ))
}
if(isObject(fns[key])) {
return Object.assign({}, acc, ( obj$2 = {}, obj$2[key] = isObject(obj[key]) ? mapProps(fns[key], obj[key]) : obj[key], obj$2 ))
}
if(!isFunction(fns[key])) {
throw new TypeError('mapProps: Object of functions required for first argument')
}
return Object.assign({}, acc, ( obj$3 = {}, obj$3[key] = fns[key](obj[key]), obj$3 ))
}; }
// mapProps :: { (* -> *) } -> Object -> Object
function mapProps(fns, obj) {
if(!(isObject(fns) && isObject(obj))) {
throw new TypeError('mapProps: Objects required for both arguments')
}
return Object.keys(obj)
.reduce(applyMap(fns, obj), {})
}
module.exports = curry(mapProps)