ifElse.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import _curry3 from "./internal/_curry3.js";
  2. import curryN from "./curryN.js";
  3. /**
  4. * Creates a function that will process either the `onTrue` or the `onFalse`
  5. * function depending upon the result of the `condition` predicate.
  6. *
  7. * Note that `ifElse` takes its arity from the longest of the three functions passed to it.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.8.0
  12. * @category Logic
  13. * @sig (*... -> Boolean) -> (*... -> *) -> (*... -> *) -> (*... -> *)
  14. * @param {Function} condition A predicate function
  15. * @param {Function} onTrue A function to invoke when the `condition` evaluates to a truthy value.
  16. * @param {Function} onFalse A function to invoke when the `condition` evaluates to a falsy value.
  17. * @return {Function} A new function that will process either the `onTrue` or the `onFalse`
  18. * function depending upon the result of the `condition` predicate.
  19. * @see R.unless, R.when, R.cond
  20. * @example
  21. *
  22. * const incCount = R.ifElse(
  23. * R.has('count'),
  24. * R.over(R.lensProp('count'), R.inc),
  25. * R.assoc('count', 1)
  26. * );
  27. * incCount({ count: 1 }); //=> { count: 2 }
  28. * incCount({}); //=> { count: 1 }
  29. */
  30. var ifElse =
  31. /*#__PURE__*/
  32. _curry3(function ifElse(condition, onTrue, onFalse) {
  33. return curryN(Math.max(condition.length, onTrue.length, onFalse.length), function _ifElse() {
  34. return condition.apply(this, arguments) ? onTrue.apply(this, arguments) : onFalse.apply(this, arguments);
  35. });
  36. });
  37. export default ifElse;