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