path.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _curry2 from "./internal/_curry2.js";
  2. import paths from "./paths.js";
  3. /**
  4. * Retrieves the value at a given path. The nodes of the path can be arbitrary strings or non-negative integers.
  5. * For anything else, the value is unspecified. Integer paths are meant to index arrays, strings are meant for objects.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.2.0
  10. * @category Object
  11. * @typedefn Idx = String | Int | Symbol
  12. * @sig [Idx] -> {a} -> a | Undefined
  13. * @sig Idx = String | NonNegativeInt
  14. * @param {Array} path The path to use.
  15. * @param {Object} obj The object or array to retrieve the nested property from.
  16. * @return {*} The data at `path`.
  17. * @see R.prop, R.nth, R.assocPath, R.dissocPath
  18. * @example
  19. *
  20. * R.path(['a', 'b'], {a: {b: 2}}); //=> 2
  21. * R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
  22. * R.path(['a', 'b', 0], {a: {b: [1, 2, 3]}}); //=> 1
  23. * R.path(['a', 'b', -2], {a: {b: [1, 2, 3]}}); //=> 2
  24. * R.path([2], {'2': 2}); //=> 2
  25. * R.path([-2], {'-2': 'a'}); //=> undefined
  26. */
  27. var path =
  28. /*#__PURE__*/
  29. _curry2(function path(pathAr, obj) {
  30. return paths([pathAr], obj)[0];
  31. });
  32. export default path;