You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.4 KiB

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