source: node_modules/ramda/es/cond.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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