crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
27 lines (21 loc) • 724 B
JavaScript
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var curry = require('../core/curry')
var predOrFunc = require('../core/predOrFunc')
var isPredOrFunc = require('../core/isPredOrFunc')
var isFunction = require('../core/isFunction')
// when : (a -> Boolean) | Pred -> (a -> b) -> a -> b | a
function when(pred, f) {
if(!isPredOrFunc(pred)) {
throw new TypeError(
'when: Pred or predicate function required for first argument'
)
}
if(!isFunction(f)) {
throw new TypeError(
'when: Function required for second argument'
)
}
return function (x) { return predOrFunc(pred, x) ? f(x) : x; }
}
module.exports = curry(when)