forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinto.js
44 lines (36 loc) · 1.4 KB
/
into.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
38
39
40
41
42
43
44
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('into', function() {
var add = R.add;
var isOdd = function(b) { return b % 2 !== 0; };
var addXf = {
'@@transducer/step': add,
'@@transducer/init': R.always(0),
'@@transducer/result': R.identity
};
it('transduces into arrays', function() {
eq(R.into([], R.map(add(1)), [1, 2, 3, 4]), [2, 3, 4, 5]);
eq(R.into([], R.filter(isOdd), [1, 2, 3, 4]), [1, 3]);
eq(R.into([], R.compose(R.map(add(1)), R.take(2)), [1, 2, 3, 4]), [2, 3]);
});
it('transduces into strings', function() {
eq(R.into('', R.map(add(1)), [1, 2, 3, 4]), '2345');
eq(R.into('', R.filter(isOdd), [1, 2, 3, 4]), '13');
eq(R.into('', R.compose(R.map(add(1)), R.take(2)), [1, 2, 3, 4]), '23');
});
it('transduces into objects', function() {
eq(R.into({}, R.identity, [['a', 1], ['b', 2]]), {a: 1, b: 2});
eq(R.into({}, R.identity, [{a: 1}, {b: 2, c: 3}]), {a: 1, b: 2, c: 3});
});
it('dispatches to objects that implement `reduce`', function() {
var obj = {x: [1, 2, 3], reduce: function() { return 'override'; }};
eq(R.into([], R.map(add(1)), obj), 'override');
eq(R.into([], R.filter(isOdd), obj), 'override');
});
it('allows custom transformer', function() {
var intoSum = R.into(addXf);
var add2 = R.map(add(2));
var result = intoSum(add2);
eq(result([1, 2, 3, 4]), 18);
});
});