/
cond.ts
65 lines (59 loc) · 1.66 KB
/
cond.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import getIteratee from "./.internal/getIteratee";
import arrayMap from "./.internal/arrayMap";
import baseRest from "./.internal/baseRest";
import apply from "./.internal/apply";
/**
* @ignore
* @private
*/
const FUNC_ERROR_TEXT = "Expected a function";
/**
* 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 5.12.0
* @category Util
* @param pairs The predicate-function pairs.
* @returns Returns the new composite function.
* @example
*
* ```js
* const func = cond([
* [matches({ 'a': 1 }), () => 'matches A'],
* [conforms({ 'b': isNumber }), () => 'matches B'],
* [() => true, () => 'no match']
* ])
*
* func({ 'a': 1, 'b': 2 })
* // => 'matches A'
*
* func({ 'a': 0, 'b': 1 })
* // => 'matches B'
*
* func({ 'a': '1', 'b': '2' })
* // => 'no match'
* ```
*/
export function cond<R = any>(pairs: Array<Array<[(...args: any[]) => boolean, (...args: any[]) => R]>>): R
export function cond(pairs: any): any {
const length = pairs == null ? 0 : pairs.length,
toIteratee = getIteratee();
pairs = !length ? [] : arrayMap(pairs, (pair) => {
if (typeof pair[1] != "function") {
throw new TypeError(FUNC_ERROR_TEXT);
}
return [toIteratee(pair[0]), pair[1]];
});
return baseRest(function(args) {
let index = -1;
while (++index < length) {
const pair = pairs[index];
if (apply(pair[0], this, args)) {
return apply(pair[1], this, args);
}
}
});
}
export default cond;