defaultTo.js 965 B

1234567891011121314151617181920212223242526272829303132
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Returns the second argument if it is not `null`, `undefined` or `NaN`;
  4. * otherwise the first argument is returned.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.10.0
  9. * @category Logic
  10. * @sig a -> b -> a | b
  11. * @param {a} default The default value.
  12. * @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
  13. * @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
  14. * @example
  15. *
  16. * const defaultTo42 = R.defaultTo(42);
  17. *
  18. * defaultTo42(null); //=> 42
  19. * defaultTo42(undefined); //=> 42
  20. * defaultTo42(false); //=> false
  21. * defaultTo42('Ramda'); //=> 'Ramda'
  22. * // parseInt('string') results in NaN
  23. * defaultTo42(parseInt('string')); //=> 42
  24. */
  25. var defaultTo =
  26. /*#__PURE__*/
  27. _curry2(function defaultTo(d, v) {
  28. return v == null || v !== v ? d : v;
  29. });
  30. export default defaultTo;