UNPKG

1.13 kBJavaScriptView Raw
1import _curry2 from "./internal/_curry2.js";
2import _isInteger from "./internal/_isInteger.js";
3import 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.0
10 * @category Object
11 * @typedefn Idx = [String | Int]
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
23var paths =
24/*#__PURE__*/
25_curry2(function paths(pathsArray, obj) {
26 return pathsArray.map(function (paths) {
27 var val = obj;
28 var idx = 0;
29 var p;
30
31 while (idx < paths.length) {
32 if (val == null) {
33 return;
34 }
35
36 p = paths[idx];
37 val = _isInteger(p) ? nth(p, val) : val[p];
38 idx += 1;
39 }
40
41 return val;
42 });
43});
44
45export default paths;
\No newline at end of file