Home Reference Source

ts_temp/defaultTo.js

  1. import curryN from './function/curryN';
  2. /**
  3. * Returns the second argument if it is not `null`, `undefined` or `NaN`
  4. * otherwise the first argument is returned.
  5. *
  6. * @param {a} dflt The default value.
  7. * @param {b} x The value to return if it is not null or undefined
  8. * @return {*} The the second value or the default value
  9. * @example
  10. *
  11. * var defaultTo42 = defaultTo(42);
  12. *
  13. * defaultTo42(null); //=> 42
  14. * defaultTo42(undefined); //=> 42
  15. * defaultTo42('Example'); //=> 'Example'
  16. * defaultTo42(parseInt('string')); //=> 42
  17. */
  18. export default curryN(2, (dflt, x) => {
  19. if (x == null || x !== x) {
  20. // eslint-disable-line no-self-compare
  21. return dflt;
  22. }
  23. return x;
  24. });