source: node_modules/ramda/src/cond.js@ d24f17c

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

Initial commit

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[d24f17c]1var _arity =
2/*#__PURE__*/
3require("./internal/_arity.js");
4
5var _curry1 =
6/*#__PURE__*/
7require("./internal/_curry1.js");
8
9var map =
10/*#__PURE__*/
11require("./map.js");
12
13var max =
14/*#__PURE__*/
15require("./max.js");
16
17var reduce =
18/*#__PURE__*/
19require("./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
53var 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
72module.exports = cond;
Note: See TracBrowser for help on using the repository browser.