-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 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,26 @@ | ||
import { curry, ap, path, __ } from 'ramda'; | ||
|
||
/** | ||
* Acts as multiple path: arrays of keys in, array of values out. Preserves order. | ||
* | ||
* @func paths | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.2.0|v1.2.0} | ||
* @category List | ||
* @sig [[k]] -> {k: v} - [v] | ||
* @param {Array} ps The property paths to fetch | ||
* @param {Object} obj The object to query | ||
* @return {Array} The corresponding values or partially applied function | ||
* @see {@link https://github.com/ramda/ramda/wiki/Cookbook#derivative-of-rprops-for-deep-fields|Ramda Cookbook}, {@link http://ramdajs.com/docs/#props|props} | ||
* @example | ||
* | ||
* const obj = { | ||
* a: { b: { c: 1 } }, | ||
* x: 2, | ||
* }; | ||
* | ||
* RA.paths([['a', 'b', 'c'], ['x']], obj); //=> [1, 2] | ||
*/ | ||
const paths = curry((ps, obj) => ap([path(__, obj)], ps)); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
describe('paths', function() { | ||
const obj = { a: { b: { c: 1 } }, d: 4, e: 5, f: 6 }; | ||
|
||
it('returns empty array if no paths requested', function() { | ||
eq(RA.paths([], obj), []); | ||
}); | ||
|
||
it('returns values for requested paths', function() { | ||
eq(RA.paths([['a', 'b', 'c'], ['e']], obj), [1, 5]); | ||
}); | ||
|
||
it('preserves order', function() { | ||
eq(RA.paths([['f'], ['a', 'b', 'c'], ['e']], obj), [6, 1, 5]); | ||
}); | ||
|
||
it('returns undefined for nonexistent paths', function() { | ||
const ps = RA.paths([['d'], ['nonexistent']], obj); | ||
eq(ps.length, 2); | ||
eq(ps[0], 4); | ||
eq(ps[1], undefined); | ||
}); | ||
|
||
it('is curried', function() { | ||
eq(RA.paths([['a', 'b', 'c'], ['d']])(obj), [1, 4]); | ||
}); | ||
}); |