forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposeWith.js
47 lines (37 loc) · 1.37 KB
/
composeWith.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
45
46
47
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('composeWith', function() {
it('performs right-to-left function composition with function applying', function() {
// f :: (String, Number?) -> ([Number] -> [Number])
var f = R.composeWith(function(f, res) {
return f(res);
})([R.map, R.multiply, parseInt]);
eq(f.length, 2);
eq(f('10')([1, 2, 3]), [10, 20, 30]);
eq(f('10', 2)([1, 2, 3]), [2, 4, 6]);
});
it('performs right-to-left function while not nil result', function() {
var isOdd = R.flip(R.modulo)(2);
var composeWhenNotNil = R.composeWith(function(f, res) {
return R.isNil(res) ? null : f(res);
});
var f = composeWhenNotNil([R.inc, R.ifElse(isOdd, R.identity, R.always(null)), parseInt]);
eq(f.length, 2);
eq(f('1'), 2);
eq(f('2'), null);
});
it('performs right-to-left function using promise chaining', function() {
var then = function(f, p) { return p.then(f); };
var composeP = R.composeWith(then);
var toListPromise = function(a) { return new Promise(function(res) { res([a]); }); };
var doubleListPromise = function(a) { return new Promise(function(res) { res(R.concat(a, a)); }); };
var f = composeP([
doubleListPromise,
toListPromise
]);
return f(1)
.then(function(res) {
eq(res, [1, 1]);
});
});
});