forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeWith.js
47 lines (37 loc) · 1.34 KB
/
pipeWith.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('pipeWith', function() {
it('performs left-to-right function composition with function applying', function() {
// f :: (String, Number?) -> ([Number] -> [Number])
var f = R.pipeWith(function(f, res) {
return f(res);
})([parseInt, R.multiply, R.map]);
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 left-to-right function while not nil result', function() {
var isOdd = R.flip(R.modulo)(2);
var pipeWhenNotNil = R.pipeWith(function(f, res) {
return R.isNil(res) ? null : f(res);
});
var f = pipeWhenNotNil([parseInt, R.ifElse(isOdd, R.identity, R.always(null)), R.inc]);
eq(f.length, 2);
eq(f('1'), 2);
eq(f('2'), null);
});
it('performs left-to-right function using promise chaining', function() {
var then = function(f, p) { return p.then(f); };
var pipeP = R.pipeWith(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 = pipeP([
toListPromise,
doubleListPromise
]);
return f(1)
.then(function(res) {
eq(res, [1, 1]);
});
});
});