forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.js
59 lines (49 loc) · 1.29 KB
/
pipe.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
48
49
50
51
52
53
54
55
56
57
58
59
var assert = require('assert');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('pipe', function() {
it('is a variadic function', function() {
eq(typeof R.pipe, 'function');
eq(R.pipe.length, 0);
});
it('performs left-to-right function composition', function() {
// f :: (String, Number?) -> ([Number] -> [Number])
var f = R.pipe(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('passes context to functions', function() {
function x(val) {
return this.x * val;
}
function y(val) {
return this.y * val;
}
function z(val) {
return this.z * val;
}
var context = {
a: R.pipe(x, y, z),
x: 4,
y: 2,
z: 1
};
eq(context.a(5), 40);
});
it('throws if given no arguments', function() {
assert.throws(
function() { R.pipe(); },
function(err) {
return err.constructor === Error &&
err.message === 'pipe requires at least one argument';
}
);
});
it('can be applied to one argument', function() {
var f = function(a, b, c) { return [a, b, c]; };
var g = R.pipe(f);
eq(g.length, 3);
eq(g(1, 2, 3), [1, 2, 3]);
});
});