Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feature: adding paths operator - #2740 * refactor: making path a derivative of paths * refactor: making props a derivative of path
- Loading branch information
Showing
5 changed files
with
90 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import _curry2 from './internal/_curry2'; | ||
import _isInteger from './internal/_isInteger'; | ||
import nth from './nth'; | ||
|
||
/** | ||
* Retrieves the values at given paths of an object. | ||
* | ||
* @func | ||
* @memberOf R | ||
* @category Object | ||
* @typedefn Idx = [String | Int] | ||
* @sig [Idx] -> {a} -> [a | Undefined] | ||
* @param {Array} pathsArray The array of paths to be fetched. | ||
* @param {Object} obj The object to retrieve the nested properties from. | ||
* @return {Array} A list consisting of values at paths specified by "pathsArray". | ||
* @see R.path | ||
* @example | ||
* | ||
* R.paths([['a', 'b'], ['p', 0, 'q']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, 3] | ||
* R.paths([['a', 'b'], ['p', 'r']], {a: {b: 2}, p: [{q: 3}]}); //=> [2, undefined] | ||
*/ | ||
var paths = _curry2(function paths(pathsArray, obj) { | ||
return pathsArray.map(function(paths) { | ||
var val = obj; | ||
var idx = 0; | ||
var p; | ||
while (idx < paths.length) { | ||
if (val == null) { | ||
return; | ||
} | ||
p = paths[idx]; | ||
val = _isInteger(p) ? nth(p, val) : val[p]; | ||
idx += 1; | ||
} | ||
return val; | ||
}); | ||
}); | ||
export default paths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var R = require('../source'); | ||
var eq = require('./shared/eq'); | ||
|
||
describe('paths', function() { | ||
var obj = { | ||
a: { | ||
b: { | ||
c: 1, | ||
d: 2 | ||
} | ||
}, | ||
p: [{q: 3}, 'Hi'], | ||
x: { | ||
y: 'Alice', | ||
z: [[{}]] | ||
} | ||
}; | ||
it('takes paths and returns values at those paths', function() { | ||
eq(R.paths([['a', 'b', 'c'], ['x', 'y']], obj), [1, 'Alice']); | ||
eq(R.paths([['a', 'b', 'd'], ['p', 'q']], obj), [2, undefined]); | ||
}); | ||
|
||
it('takes a paths that contains indices into arrays', function() { | ||
eq(R.paths([['p', 0, 'q'], ['x', 'z', 0, 0]], obj), [3, {}]); | ||
eq(R.paths([['p', 0, 'q'], ['x', 'z', 2, 1]], obj), [3, undefined]); | ||
}); | ||
|
||
it('takes a path that contains negative indices into arrays', function() { | ||
eq(R.paths([['p', -2, 'q'], ['p', -1]], obj), [3, 'Hi']); | ||
eq(R.paths([['p', -4, 'q'], ['x', 'z', -1, 0]], obj), [undefined, {}]); | ||
}); | ||
|
||
it("gets a deep property's value from objects", function() { | ||
eq(R.paths([['a', 'b']], obj), [obj.a.b]); | ||
eq(R.paths([['p', 0]], obj), [obj.p[0]]); | ||
}); | ||
|
||
it('returns undefined for items not found', function() { | ||
eq(R.paths([['a', 'x', 'y']], obj), [undefined]); | ||
eq(R.paths([['p', 2]], obj), [undefined]); | ||
}); | ||
|
||
}); |