forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseWith.js
43 lines (35 loc) · 1.33 KB
/
useWith.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('useWith', function() {
function max() { return Math.max.apply(Math, arguments); }
function add1(x) { return x + 1; }
function mult2(x) { return x * 2; }
function div3(x) { return x / 3; }
var f = R.useWith(max, [add1, mult2, div3]);
it('takes a list of function and returns a function', function() {
eq(typeof R.useWith(max, []), 'function');
eq(typeof R.useWith(max, [add1]), 'function');
eq(typeof R.useWith(max, [add1, mult2, div3]), 'function');
});
it('passes the arguments received to their respective functions', function() {
eq(f(7, 8, 9), 16); // max(7 + 1, 8 * 2, 9 / 3);
});
it('passes additional arguments to the main function', function() {
eq(f(7, 8, 9, 10), 16);
eq(f(7, 8, 9, 20), 20);
});
it('has the correct arity', function() {
eq(f.length, 3);
});
it('passes context to its functions', function() {
var a = function(x) { return this.f1(x); };
var b = function(x) { return this.f2(x); };
var c = function(x, y) { return this.f3(x, y); };
var d = R.useWith(c, [a, b]);
var context = {f1: R.add(1), f2: R.add(2), f3: R.add};
eq(a.call(context, 1), 2);
eq(b.call(context, 1), 3);
eq(d.apply(context, [1, 1]), 5);
eq(d.apply(context, [2, 3]), 8);
});
});