Ramda Adjunct 3.0.0

defaultWhen.js

  1. import { curry } from 'ramda';
  2. /**
  3. * Returns the second argument if predicate function returns `true`,
  4. * otherwise the third argument is returned.
  5. *
  6. * @func defaultWhen
  7. * @memberOf RA
  8. * @since {@link https://char0n.github.io/ramda-adjunct/2.2.0|v2.2.0}
  9. * @category Logic
  10. * @sig (a -> Boolean) -> b -> a -> a | b
  11. * @param {!function} predicate The predicate function
  12. * @param {*} defaultVal The default value
  13. * @param {*} val `val` will be returned instead of `defaultVal` if predicate returns false
  14. * @return {*} The `val` if predicate returns `false`, otherwise the default value
  15. * @see {@link http://ramdajs.com/docs/#defaultTo|R.defaultTo}
  16. * @example
  17. *
  18. * RA.defaultWhen(RA.isNull, 1, null); // => 1
  19. * RA.defaultWhen(RA.isNull, 1, 2); // => 2
  20. */
  21. const defaultWhen = curry((predicate, defaultVal, val) =>
  22. predicate(val) ? defaultVal : val
  23. );
  24. export default defaultWhen;