main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
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 |
|
---|
23 | var 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 |
|
---|
45 | export default paths; |
---|
Note:
See
TracBrowser
for help on using the repository browser.