UNPKG

mocoolka-function

Version:

Function lib for function.

84 lines 3.16 kB
Object.defineProperty(exports, "__esModule", { value: true }); var arity_1 = require("./internal/arity"); var isPlaceholder_1 = require("./internal/isPlaceholder"); var setName_1 = require("./setName"); var getName_1 = require("./getName"); var _curryN = function (length, received, fn, reverse) { if (reverse === void 0) { reverse = false; } var temp = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var combined = []; var argsIdx = 0; var left = length; var combinedIdx = 0; while (combinedIdx < received.length || argsIdx < args.length) { var result = void 0; // get result from current arguments when placeholder is true otherwise get from received if (combinedIdx < received.length && (!isPlaceholder_1.default(received[combinedIdx]) || argsIdx >= args.length)) { result = received[combinedIdx]; } else { result = args[argsIdx]; argsIdx += 1; } combined[combinedIdx] = result; if (!isPlaceholder_1.default(result)) { left -= 1; } combinedIdx += 1; } if (left <= 0) { return (reverse === true) ? fn.apply(null, combined.reverse()) : fn.apply(null, combined); } else { return arity_1.default(left, _curryN(length, combined, fn, reverse)); } }; setName_1.default(temp, getName_1.default(fn)); return temp; }; /** * Returns a curried equivalent of the provided function. * The curried function has two unusual capabilities. * First, its arguments needn't be provided one at a time. * If f is a ternary function and g is curry(f), the following are equivalent: * * - g(1)(2)(3) * - g(1)(2, 3) * - g(1, 2)(3) * - g(1, 2, 3) * Secondly, the special placeholder value __ may be used to specify "gaps", * allowing partial application of any combination of arguments, * regardless of their positions. If g is as above and _ is placeholder, * the following are equivalent: * g(1, 2, 3) * - g(__, 2, 3)(1) * - g(__, __, 3)(1)(2) * - g(__, __, 3)(1, 2) * - g(__, 2)(1)(3) * - g(__, 2)(1, 3) * - g(__, 2)(__, 3)(1) * @since v0.1.0 * @category Arity * @ts Number -> (* -> a) -> (* -> a) * @keywords wrap * @param {number} length -The param number in new function. * @param {Function} fn The function to curry. * @param {boolean} [reverse] * True meaning The given function call params from left to right. * False meaning The given function call params from right to left * @return {Function} A new curried function. */ var curryN = function (fn, length, reverse) { if (reverse === void 0) { reverse = false; } /* istanbul ignore next */ length = length < 0 ? 0 : length; return arity_1.default(length, _curryN(length, [], fn, reverse)); }; exports.default = curryN; //# sourceMappingURL=curryN.js.map