forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurryN.js
108 lines (86 loc) · 2.88 KB
/
curryN.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var R = require('..');
var eq = require('./shared/eq');
describe('curryN', function() {
function source(a, b, c, d) {
void d;
return a * b * c;
}
it('accepts an arity', function() {
var curried = R.curryN(3, source);
eq(curried(1)(2)(3), 6);
eq(curried(1, 2)(3), 6);
eq(curried(1)(2, 3), 6);
eq(curried(1, 2, 3), 6);
});
it('can be partially applied', function() {
var curry3 = R.curryN(3);
var curried = curry3(source);
eq(curried.length, 3);
eq(curried(1)(2)(3), 6);
eq(curried(1, 2)(3), 6);
eq(curried(1)(2, 3), 6);
eq(curried(1, 2, 3), 6);
});
it('preserves context', function() {
var ctx = {x: 10};
var f = function(a, b) { return a + b * this.x; };
var g = R.curryN(2, f);
eq(g.call(ctx, 2, 4), 42);
eq(g.call(ctx, 2).call(ctx, 4), 42);
});
it('supports R.__ placeholder', function() {
var f = function() { return Array.prototype.slice.call(arguments); };
var g = R.curryN(3, f);
var _ = R.__;
eq(g(1)(2)(3), [1, 2, 3]);
eq(g(1)(2, 3), [1, 2, 3]);
eq(g(1, 2)(3), [1, 2, 3]);
eq(g(1, 2, 3), [1, 2, 3]);
eq(g(_, 2, 3)(1), [1, 2, 3]);
eq(g(1, _, 3)(2), [1, 2, 3]);
eq(g(1, 2, _)(3), [1, 2, 3]);
eq(g(1, _, _)(2)(3), [1, 2, 3]);
eq(g(_, 2, _)(1)(3), [1, 2, 3]);
eq(g(_, _, 3)(1)(2), [1, 2, 3]);
eq(g(1, _, _)(2, 3), [1, 2, 3]);
eq(g(_, 2, _)(1, 3), [1, 2, 3]);
eq(g(_, _, 3)(1, 2), [1, 2, 3]);
eq(g(1, _, _)(_, 3)(2), [1, 2, 3]);
eq(g(_, 2, _)(_, 3)(1), [1, 2, 3]);
eq(g(_, _, 3)(_, 2)(1), [1, 2, 3]);
eq(g(_, _, _)(_, _)(_)(1, 2, 3), [1, 2, 3]);
eq(g(_, _, _)(1, _, _)(_, _)(2, _)(_)(3), [1, 2, 3]);
});
it('supports @@functional/placeholder', function() {
var f = function() { return Array.prototype.slice.call(arguments); };
var g = R.curryN(3, f);
var _ = {'@@functional/placeholder': true, x: Math.random()};
eq(g(1)(2)(3), [1, 2, 3]);
eq(g(1)(2, 3), [1, 2, 3]);
eq(g(1, 2)(3), [1, 2, 3]);
eq(g(1, 2, 3), [1, 2, 3]);
eq(g(_, 2, 3)(1), [1, 2, 3]);
eq(g(1, _, 3)(2), [1, 2, 3]);
eq(g(1, 2, _)(3), [1, 2, 3]);
eq(g(1, _, _)(2)(3), [1, 2, 3]);
eq(g(_, 2, _)(1)(3), [1, 2, 3]);
eq(g(_, _, 3)(1)(2), [1, 2, 3]);
eq(g(1, _, _)(2, 3), [1, 2, 3]);
eq(g(_, 2, _)(1, 3), [1, 2, 3]);
eq(g(_, _, 3)(1, 2), [1, 2, 3]);
eq(g(1, _, _)(_, 3)(2), [1, 2, 3]);
eq(g(_, 2, _)(_, 3)(1), [1, 2, 3]);
eq(g(_, _, 3)(_, 2)(1), [1, 2, 3]);
eq(g(_, _, _)(_, _)(_)(1, 2, 3), [1, 2, 3]);
eq(g(_, _, _)(1, _, _)(_, _)(2, _)(_)(3), [1, 2, 3]);
});
it('forwards extra arguments', function() {
var f = function() { return Array.prototype.slice.call(arguments); };
var g = R.curryN(3, f);
eq(g(1, 2, 3), [1, 2, 3]);
eq(g(1, 2, 3, 4), [1, 2, 3, 4]);
eq(g(1, 2)(3, 4), [1, 2, 3, 4]);
eq(g(1)(2, 3, 4), [1, 2, 3, 4]);
eq(g(1)(2)(3, 4), [1, 2, 3, 4]);
});
});