97 lines
2.5 KiB
JavaScript
97 lines
2.5 KiB
JavaScript
|
import _curry2 from "./internal/_curry2.js";
|
||
|
/**
|
||
|
* Wraps a function of any arity (including nullary) in a function that accepts
|
||
|
* exactly `n` parameters. Any extraneous parameters will not be passed to the
|
||
|
* supplied function.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.1.0
|
||
|
* @category Function
|
||
|
* @sig Number -> (* -> a) -> (* -> a)
|
||
|
* @param {Number} n The desired arity of the new function.
|
||
|
* @param {Function} fn The function to wrap.
|
||
|
* @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
|
||
|
* arity `n`.
|
||
|
* @see R.binary, R.unary
|
||
|
* @example
|
||
|
*
|
||
|
* const takesTwoArgs = (a, b) => [a, b];
|
||
|
*
|
||
|
* takesTwoArgs.length; //=> 2
|
||
|
* takesTwoArgs(1, 2); //=> [1, 2]
|
||
|
*
|
||
|
* const takesOneArg = R.nAry(1, takesTwoArgs);
|
||
|
* takesOneArg.length; //=> 1
|
||
|
* // Only `n` arguments are passed to the wrapped function
|
||
|
* takesOneArg(1, 2); //=> [1, undefined]
|
||
|
* @symb R.nAry(0, f)(a, b) = f()
|
||
|
* @symb R.nAry(1, f)(a, b) = f(a)
|
||
|
* @symb R.nAry(2, f)(a, b) = f(a, b)
|
||
|
*/
|
||
|
|
||
|
var nAry =
|
||
|
/*#__PURE__*/
|
||
|
_curry2(function nAry(n, fn) {
|
||
|
switch (n) {
|
||
|
case 0:
|
||
|
return function () {
|
||
|
return fn.call(this);
|
||
|
};
|
||
|
|
||
|
case 1:
|
||
|
return function (a0) {
|
||
|
return fn.call(this, a0);
|
||
|
};
|
||
|
|
||
|
case 2:
|
||
|
return function (a0, a1) {
|
||
|
return fn.call(this, a0, a1);
|
||
|
};
|
||
|
|
||
|
case 3:
|
||
|
return function (a0, a1, a2) {
|
||
|
return fn.call(this, a0, a1, a2);
|
||
|
};
|
||
|
|
||
|
case 4:
|
||
|
return function (a0, a1, a2, a3) {
|
||
|
return fn.call(this, a0, a1, a2, a3);
|
||
|
};
|
||
|
|
||
|
case 5:
|
||
|
return function (a0, a1, a2, a3, a4) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4);
|
||
|
};
|
||
|
|
||
|
case 6:
|
||
|
return function (a0, a1, a2, a3, a4, a5) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4, a5);
|
||
|
};
|
||
|
|
||
|
case 7:
|
||
|
return function (a0, a1, a2, a3, a4, a5, a6) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4, a5, a6);
|
||
|
};
|
||
|
|
||
|
case 8:
|
||
|
return function (a0, a1, a2, a3, a4, a5, a6, a7) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);
|
||
|
};
|
||
|
|
||
|
case 9:
|
||
|
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);
|
||
|
};
|
||
|
|
||
|
case 10:
|
||
|
return function (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
|
||
|
return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
||
|
};
|
||
|
|
||
|
default:
|
||
|
throw new Error('First argument to nAry must be a non-negative integer no greater than ten');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
export default nAry;
|