either.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isFunction from "./internal/_isFunction.js";
  3. import lift from "./lift.js";
  4. import or from "./or.js";
  5. /**
  6. * A function wrapping calls to the two functions in an `||` operation,
  7. * returning the result of the first function if it is truth-y and the result
  8. * of the second function otherwise. Note that this is short-circuited,
  9. * meaning that the second function will not be invoked if the first returns a
  10. * truth-y value.
  11. *
  12. * In addition to functions, `R.either` also accepts any fantasy-land compatible
  13. * applicative functor.
  14. *
  15. * @func
  16. * @memberOf R
  17. * @since v0.12.0
  18. * @category Logic
  19. * @sig (*... -> Boolean) -> (*... -> Boolean) -> (*... -> Boolean)
  20. * @param {Function} f a predicate
  21. * @param {Function} g another predicate
  22. * @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
  23. * @see R.both, R.anyPass, R.or
  24. * @example
  25. *
  26. * const gt10 = x => x > 10;
  27. * const even = x => x % 2 === 0;
  28. * const f = R.either(gt10, even);
  29. * f(101); //=> true
  30. * f(8); //=> true
  31. *
  32. * R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
  33. * R.either([false, false, 'a'], [11]) // => [11, 11, "a"]
  34. */
  35. var either =
  36. /*#__PURE__*/
  37. _curry2(function either(f, g) {
  38. return _isFunction(f) ? function _either() {
  39. return f.apply(this, arguments) || g.apply(this, arguments);
  40. } : lift(or)(f, g);
  41. });
  42. export default either;