@newdash/newdash
Version:
javascript/typescript utility library
73 lines (72 loc) • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.curry = void 0;
const CONSTANTS_1 = require("./.internal/CONSTANTS");
const createCurry_1 = __importDefault(require("./.internal/createCurry"));
const assert_1 = require("./assert");
const toInteger_1 = __importDefault(require("./toInteger"));
/**
* Creates a function that accepts arguments of `func` and either invokes
* `func` returning its result, if at least `arity` number of arguments have
* been provided, or returns a function that accepts the remaining `func`
* arguments, and so on. The arity of `func` may be specified if `func.length`
* is not sufficient.
*
* **Note:** This method doesn't set the "length" property of curried functions.
*
* @since 5.5.0
* @category Function
* @param func The function to curry.
* @param arity The arity of `func`.
* @param guard Enables use as an iteratee for methods like `map`.
* @returns Returns the new curried function.
* @example
*
* ```js
* var abc = function(a, b, c) {
* return [a, b, c];
* };
*
* var curried = curry(abc);
*
* curried(1)(2)(3);
* // => [1, 2, 3]
*
* curried(1, 2)(3);
* // => [1, 2, 3]
*
* curried(1, 2, 3);
* // => [1, 2, 3]
*
* // Curried with placeholders.
* curried(1)(curry.placeholder, 3)(2);
* // => [1, 2, 3]
* ```
*/
function curry(func, arity = func?.length, guard) {
(0, assert_1.mustProvide)(arity, "arity", "number");
arity = (0, toInteger_1.default)(arity);
arity = guard ? undefined : arity;
const result = (0, createCurry_1.default)(func, CONSTANTS_1.WRAP_CURRY_FLAG, arity);
// default placeholder
result["placeholder"] = curry.placeholder;
// @ts-ignore
return result;
}
exports.curry = curry;
if (typeof Symbol == "function") {
/**
* placeholder of curry function
*/
curry["placeholder"] = Symbol("__curry__placeholder__");
}
else {
/**
* placeholder of curry function
*/
curry["placeholder"] = "__curry__placeholder__";
}
exports.default = curry;