paths.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isInteger from "./internal/_isInteger.js";
  3. import nth from "./nth.js";
  4. /**
  5. * Retrieves the values at given paths of an object.
  6. *
  7. * @func
  8. * @memberOf R
  9. * @since v0.27.1
  10. * @category Object
  11. * @typedefn Idx = [String | Int | Symbol]
  12. * @sig [Idx] -> {a} -> [a | Undefined]
  13. * @param {Array} pathsArray The array of paths to be fetched.
  14. * @param {Object} obj The object to retrieve the nested properties from.
  15. * @return {Array} A list consisting of values at paths specified by "pathsArray".
  16. * @see R.path
  17. * @example
  18. *
  19. * R.paths([['a', 'b'], ['p', 0, 'q']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, 3]
  20. * R.paths([['a', 'b'], ['p', 'r']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, undefined]
  21. */
  22. var paths =
  23. /*#__PURE__*/
  24. _curry2(function paths(pathsArray, obj) {
  25. return pathsArray.map(function (paths) {
  26. var val = obj;
  27. var idx = 0;
  28. var p;
  29. while (idx < paths.length) {
  30. if (val == null) {
  31. return;
  32. }
  33. p = paths[idx];
  34. val = _isInteger(p) ? nth(p, val) : val[p];
  35. idx += 1;
  36. }
  37. return val;
  38. });
  39. });
  40. export default paths;