1 | var _arity =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_arity.js");
|
---|
4 |
|
---|
5 | var _curry1 =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_curry1.js");
|
---|
8 |
|
---|
9 | var map =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./map.js");
|
---|
12 |
|
---|
13 | var max =
|
---|
14 | /*#__PURE__*/
|
---|
15 | require("./max.js");
|
---|
16 |
|
---|
17 | var reduce =
|
---|
18 | /*#__PURE__*/
|
---|
19 | require("./reduce.js");
|
---|
20 | /**
|
---|
21 | * Returns a function, `fn`, which encapsulates `if/else, if/else, ...` logic.
|
---|
22 | * `R.cond` takes a list of [predicate, transformer] pairs. All of the arguments
|
---|
23 | * to `fn` are applied to each of the predicates in turn until one returns a
|
---|
24 | * "truthy" value, at which point `fn` returns the result of applying its
|
---|
25 | * arguments to the corresponding transformer. If none of the predicates
|
---|
26 | * matches, `fn` returns undefined.
|
---|
27 | *
|
---|
28 | * **Please note**: This is not a direct substitute for a `switch` statement.
|
---|
29 | * Remember that both elements of every pair passed to `cond` are *functions*,
|
---|
30 | * and `cond` returns a function.
|
---|
31 | *
|
---|
32 | * @func
|
---|
33 | * @memberOf R
|
---|
34 | * @since v0.6.0
|
---|
35 | * @category Logic
|
---|
36 | * @sig [[(*... -> Boolean),(*... -> *)]] -> (*... -> *)
|
---|
37 | * @param {Array} pairs A list of [predicate, transformer]
|
---|
38 | * @return {Function}
|
---|
39 | * @see R.ifElse, R.unless, R.when
|
---|
40 | * @example
|
---|
41 | *
|
---|
42 | * const fn = R.cond([
|
---|
43 | * [R.equals(0), R.always('water freezes at 0°C')],
|
---|
44 | * [R.equals(100), R.always('water boils at 100°C')],
|
---|
45 | * [R.T, temp => 'nothing special happens at ' + temp + '°C']
|
---|
46 | * ]);
|
---|
47 | * fn(0); //=> 'water freezes at 0°C'
|
---|
48 | * fn(50); //=> 'nothing special happens at 50°C'
|
---|
49 | * fn(100); //=> 'water boils at 100°C'
|
---|
50 | */
|
---|
51 |
|
---|
52 |
|
---|
53 | var cond =
|
---|
54 | /*#__PURE__*/
|
---|
55 | _curry1(function cond(pairs) {
|
---|
56 | var arity = reduce(max, 0, map(function (pair) {
|
---|
57 | return pair[0].length;
|
---|
58 | }, pairs));
|
---|
59 | return _arity(arity, function () {
|
---|
60 | var idx = 0;
|
---|
61 |
|
---|
62 | while (idx < pairs.length) {
|
---|
63 | if (pairs[idx][0].apply(this, arguments)) {
|
---|
64 | return pairs[idx][1].apply(this, arguments);
|
---|
65 | }
|
---|
66 |
|
---|
67 | idx += 1;
|
---|
68 | }
|
---|
69 | });
|
---|
70 | });
|
---|
71 |
|
---|
72 | module.exports = cond; |
---|