UNPKG

crocks

Version:

A collection of well known Algebraic Datatypes for your utter enjoyment.

39 lines (29 loc) 887 B
/** @license ISC License (c) copyright 2019 original and current authors */ /** @author Ian Hofmann-Hicks */ var curry = require('../core/curry') var isDefined = require('../core/isDefined') var isEmpty = require('../core/isEmpty') var isInteger = require('../core/isInteger') var isNil = require('../core/isNil') var isString = require('../core/isString') function fn(name) { function getPropOr(def, key, target) { if(!(isString(key) && !isEmpty(key) || isInteger(key))) { throw new TypeError((name + ": Non-empty String or Integer required for second argument")) } if(isNil(target)) { return def } var value = target[key] return isDefined(value) ? value : def } return curry(getPropOr) } // getPropOr : a -> (String | Integer) -> b -> c var getPropOr = fn('getPropOr') getPropOr.origFn = fn module.exports = getPropOr