when.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Tests the final argument by passing it to the given predicate function. If
  4. * the predicate is satisfied, the function will return the result of calling
  5. * the `whenTrueFn` function with the same argument. If the predicate is not
  6. * satisfied, the argument is returned as is.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.18.0
  11. * @category Logic
  12. * @sig (a -> Boolean) -> (a -> b) -> a -> a | b
  13. * @param {Function} pred A predicate function
  14. * @param {Function} whenTrueFn A function to invoke when the `condition`
  15. * evaluates to a truthy value.
  16. * @param {*} x An object to test with the `pred` function and
  17. * pass to `whenTrueFn` if necessary.
  18. * @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
  19. * @see R.ifElse, R.unless, R.cond
  20. * @example
  21. *
  22. * // truncate :: String -> String
  23. * const truncate = R.when(
  24. * R.propSatisfies(R.gt(R.__, 10), 'length'),
  25. * R.pipe(R.take(10), R.append('…'), R.join(''))
  26. * );
  27. * truncate('12345'); //=> '12345'
  28. * truncate('0123456789ABC'); //=> '0123456789…'
  29. */
  30. var when =
  31. /*#__PURE__*/
  32. _curry3(function when(pred, whenTrueFn, x) {
  33. return pred(x) ? whenTrueFn(x) : x;
  34. });
  35. export default when;