propOr.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import _curry3 from "./internal/_curry3.js";
  2. import defaultTo from "./defaultTo.js";
  3. import prop from "./prop.js";
  4. /**
  5. * Return the specified property of the given non-null object if the property
  6. * is present and it's value is not `null`, `undefined` or `NaN`.
  7. *
  8. * Otherwise the first argument is returned.
  9. *
  10. * @func
  11. * @memberOf R
  12. * @since v0.6.0
  13. * @category Object
  14. * @sig a -> String -> Object -> a
  15. * @param {*} val The default value.
  16. * @param {String} p The name of the property to return.
  17. * @param {Object} obj The object to query.
  18. * @return {*} The value of given property of the supplied object or the default value.
  19. * @example
  20. *
  21. * const alice = {
  22. * name: 'ALICE',
  23. * age: 101
  24. * };
  25. * const favorite = R.prop('favoriteLibrary');
  26. * const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary');
  27. *
  28. * favorite(alice); //=> undefined
  29. * favoriteWithDefault(alice); //=> 'Ramda'
  30. */
  31. var propOr =
  32. /*#__PURE__*/
  33. _curry3(function propOr(val, p, obj) {
  34. return defaultTo(val, prop(p, obj));
  35. });
  36. export default propOr;