forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexBy.js
31 lines (26 loc) · 1.14 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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
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('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(0, R.toUpper),
R.adjust(1, R.omit(['id']))
)));
var expected = {ABC: {title: 'B'}, XYZ: {title: 'A'}};
eq(R.into({}, transducer, list), expected);
eq(R.transduce(transducer, (result, input) => {result[input[0]] = input[1]; return result;}, {}, list), expected);
});
});