mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
import arrayMap from './.internal/arrayMap.js'
|
|
|
|
/**
|
|
* Creates a function that iterates over `pairs` and invokes the corresponding
|
|
* function of the first predicate to return truthy. The predicate-function
|
|
* pairs are invoked with the `this` binding and arguments of the created
|
|
* function.
|
|
*
|
|
* @since 4.0.0
|
|
* @category Util
|
|
* @param {Array} pairs The predicate-function pairs.
|
|
* @returns {Function} Returns the new composite function.
|
|
* @example
|
|
*
|
|
* const func = cond([
|
|
* [matches({ 'a': 1 }), constant('matches A')],
|
|
* [conforms({ 'b': isNumber }), constant('matches B')],
|
|
* [stubTrue, constant('no match')]
|
|
* ])
|
|
*
|
|
* func({ 'a': 1, 'b': 2 })
|
|
* // => 'matches A'
|
|
*
|
|
* func({ 'a': 0, 'b': 1 })
|
|
* // => 'matches B'
|
|
*
|
|
* func({ 'a': '1', 'b': '2' })
|
|
* // => 'no match'
|
|
*/
|
|
function cond(pairs) {
|
|
const length = pairs == null ? 0 : pairs.length
|
|
|
|
pairs = !length ? [] : arrayMap(pairs, pair => {
|
|
if (typeof pair[1] != 'function') {
|
|
throw new TypeError('Expected a function')
|
|
}
|
|
return [pair[0], pair[1]]
|
|
})
|
|
|
|
return (...args) => {
|
|
let index = -1
|
|
while (++index < length) {
|
|
const pair = pairs[index]
|
|
if (pair[0].apply(this, args)) {
|
|
return pair[1].apply(this, args)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export default cond
|