prop.js 936 B

123456789101112131415161718192021222324252627282930313233343536
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isInteger from "./internal/_isInteger.js";
  3. import nth from "./nth.js";
  4. /**
  5. * Returns a function that when supplied an object returns the indicated
  6. * property of that object, if it exists.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.1.0
  11. * @category Object
  12. * @typedefn Idx = String | Int | Symbol
  13. * @sig Idx -> {s: a} -> a | Undefined
  14. * @param {String|Number} p The property name or array index
  15. * @param {Object} obj The object to query
  16. * @return {*} The value at `obj.p`.
  17. * @see R.path, R.props, R.pluck, R.project, R.nth
  18. * @example
  19. *
  20. * R.prop('x', {x: 100}); //=> 100
  21. * R.prop('x', {}); //=> undefined
  22. * R.prop(0, [100]); //=> 100
  23. * R.compose(R.inc, R.prop('x'))({ x: 3 }) //=> 4
  24. */
  25. var prop =
  26. /*#__PURE__*/
  27. _curry2(function prop(p, obj) {
  28. if (obj == null) {
  29. return;
  30. }
  31. return _isInteger(p) ? nth(p, obj) : obj[p];
  32. });
  33. export default prop;