forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexBy.js
37 lines (30 loc) · 1.23 KB
/
indexBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var R = require('..');
var eq = require('./shared/eq');
describe('indexBy', function() {
it('indexes list by the given property', function() {
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
var indexed = R.indexBy(R.prop('id'), list);
eq(indexed, {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}});
});
it('indexes list by the given property upper case', function() {
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
var indexed = R.indexBy(R.compose(R.toUpper, R.prop('id')), list);
eq(indexed, {ABC: {id: 'abc', title: 'B'}, XYZ: {id: 'xyz', title: 'A'}});
});
it('is curried', function() {
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
var indexed = R.indexBy(R.prop('id'))(list);
eq(indexed, {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}});
});
it('can act as a transducer', function() {
var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
var transducer = R.compose(
R.indexBy(R.prop('id')),
R.map(R.pipe(
R.adjust(R.toUpper, 0),
R.adjust(R.omit('id'), 1)
)));
var result = R.into({}, transducer, list)
eq(result, {ABC: {title: 'B'}, XYZ: {title: 'A'}});
});
});