UNPKG

1.44 kBJavaScriptView Raw
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3");
4
5var curryN =
6/*#__PURE__*/
7require("./curryN");
8/**
9 * Creates a function that will process either the `onTrue` or the `onFalse`
10 * function depending upon the result of the `condition` predicate.
11 *
12 * @func
13 * @memberOf R
14 * @since v0.8.0
15 * @category Logic
16 * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
17 * @param {Function} condition A predicate function
18 * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
19 * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
20 * @return {Function} A new function that will process either the `onTrue` or the `onFalse`
21 * function depending upon the result of the `condition` predicate.
22 * @see R.unless, R.when, R.cond
23 * @example
24 *
25 * const incCount = R.ifElse(
26 * R.has('count'),
27 * R.over(R.lensProp('count'), R.inc),
28 * R.assoc('count', 1)
29 * );
30 * incCount({}); //=> { count: 1 }
31 * incCount({ count: 1 }); //=> { count: 2 }
32 */
33
34
35var ifElse =
36/*#__PURE__*/
37_curry3(function ifElse(condition, onTrue, onFalse) {
38 return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
39 return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
40 });
41});
42
43module.exports = ifElse;
\No newline at end of file