UNPKG

moltres-utils

Version:
54 lines (47 loc) 1.82 kB
"use strict"; require("core-js/modules/es6.object.define-property"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _curry = _interopRequireDefault(require("./curry")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Wraps a function of any arity (including nullary) in a function that accepts exactly `n` parameters. Any extraneous parameters are spread and then reapplied on execution. This is useful when you want to ensure a function's paramter length is exactly `n` but still passes all arguments through. * * @function * @since v0.0.4 * @category common * @param {Number} n The desired arity of the new function. * @param {Function} fn The function to wrap. * @returns {Function} A new function wrapping `fn`. The new function is guaranteed to be of parameter length `n`. * @example * * const takesNArgs = (...args) => [ ...args ] * * takesNArgs.length //=> 0 * takesNArgs(1, 2) //=> [1, 2] * * const takesTwoArgs = nArySpread(2, takesNArgs) * takesTwoArgs.length //=> 2 * // All arguments are passed to the wrapped function * takesTwoArgs(1, 2, 3) //=> [1, 2, 3] * * const curriedTakesTwoArgs = curry(takesTwoArgs) * // auto currying works as expected * const takesAtLeastOneMoreArg = curriedTakesTwoArgs(3) * takesAtLeastOneMoreArg(1, 2) // => [3, 1, 2] */ var nArySpread = (0, _curry.default)(function (n, fn) { var idx = 0; var argNames = []; while (idx < n) { argNames.push("a".concat(idx)); idx += 1; } var func = new Function('fn', "return function(".concat(argNames.join(', '), ") {\n return fn.apply(this, arguments);\n }")); return func(fn); }); var _default = nArySpread; exports.default = _default; //# sourceMappingURL=nArySpread.js.map