crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
35 lines (28 loc) • 968 B
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var curry = require('../core/curry')
var isFoldable = require('../core/isFoldable')
var isObject = require('../core/isObject')
var isString = require('../core/isString')
function pickKeys(obj) {
return function(acc, key) {
var obj$1;
if(!isString(key)) {
throw new TypeError('pick: Foldable of Strings is required for first argument')
}
return key && obj[key] !== undefined
? Object.assign(acc, ( obj$1 = {}, obj$1[key] = obj[key], obj$1 ))
: acc
}
}
// pick : ([ String ] | List String) -> Object -> Object
function pick(keys, obj) {
if(!isFoldable(keys)) {
throw new TypeError('pick: Foldable required for first argument')
}
else if(!isObject(obj)) {
throw new TypeError('pick: Object required for second argument')
}
return keys.reduce(pickKeys(obj), {})
}
module.exports = curry(pick)