UNPKG

2.56 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Wraps a function of any arity (including nullary) in a function that accepts
6 * exactly `n` parameters. Any extraneous parameters will not be passed to the
7 * supplied function.
8 *
9 * @func
10 * @memberOf R
11 * @since v0.1.0
12 * @category Function
13 * @sig Number -> (* -> a) -> (* -> a)
14 * @param {Number} n The desired arity of the new function.
15 * @param {Function} fn The function to wrap.
16 * @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
17 * arity `n`.
18 * @see R.binary, R.unary
19 * @example
20 *
21 * const takesTwoArgs = (a, b) => [a, b];
22 *
23 * takesTwoArgs.length; //=> 2
24 * takesTwoArgs(1, 2); //=> [1, 2]
25 *
26 * const takesOneArg = R.nAry(1, takesTwoArgs);
27 * takesOneArg.length; //=> 1
28 * // Only `n` arguments are passed to the wrapped function
29 * takesOneArg(1, 2); //=> [1, undefined]
30 * @symb R.nAry(0, f)(a, b) = f()
31 * @symb R.nAry(1, f)(a, b) = f(a)
32 * @symb R.nAry(2, f)(a, b) = f(a, b)
33 */
34
35
36var nAry =
37/*#__PURE__*/
38_curry2(function nAry(n, fn) {
39 switch (n) {
40 case 0:
41 return function () {
42 return fn.call(this);
43 };
44
45 case 1:
46 return function (a0) {
47 return fn.call(this, a0);
48 };
49
50 case 2:
51 return function (a0, a1) {
52 return fn.call(this, a0, a1);
53 };
54
55 case 3:
56 return function (a0, a1, a2) {
57 return fn.call(this, a0, a1, a2);
58 };
59
60 case 4:
61 return function (a0, a1, a2, a3) {
62 return fn.call(this, a0, a1, a2, a3);
63 };
64
65 case 5:
66 return function (a0, a1, a2, a3, a4) {
67 return fn.call(this, a0, a1, a2, a3, a4);
68 };
69
70 case 6:
71 return function (a0, a1, a2, a3, a4, a5) {
72 return fn.call(this, a0, a1, a2, a3, a4, a5);
73 };
74
75 case 7:
76 return function (a0, a1, a2, a3, a4, a5, a6) {
77 return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
78 };
79
80 case 8:
81 return function (a0, a1, a2, a3, a4, a5, a6, a7) {
82 return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
83 };
84
85 case 9:
86 return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
87 return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
88 };
89
90 case 10:
91 return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
92 return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
93 };
94
95 default:
96 throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
97 }
98});
99
100module.exports = nAry;
\No newline at end of file