super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
88 lines (87 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.curry = curry;
exports.curryRight = curryRight;
/**
* 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.
*
* @param func - The function to curry
* @param arity - The arity of func
* @returns The new curried function
*
* @example
* ```ts
* const abc = function(a, b, c) {
* return [a, b, c];
* };
*
* const curried = curry(abc);
*
* curried(1)(2)(3);
* // => [1, 2, 3]
*
* curried(1, 2)(3);
* // => [1, 2, 3]
*
* curried(1, 2, 3);
* // => [1, 2, 3]
* ```
*/
function curry(func, arity = func.length) {
// Return a recursive function that collects arguments
function curried(...args) {
// If we have enough args, call the original function
if (args.length >= arity) {
return func.apply(this, args);
}
// Otherwise, return a function that collects more args
return (...moreArgs) => {
return curried.apply(this, [...args, ...moreArgs]);
};
}
return curried;
}
/**
* This method is like `curry` except that arguments are applied to func in the
* manner of `partialRight` instead of `partial`.
*
* @param func - The function to curry
* @param arity - The arity of func
* @returns The new curried function
*
* @example
* ```ts
* const abc = function(a, b, c) {
* return [a, b, c];
* };
*
* const curried = curryRight(abc);
*
* curried(3)(2)(1);
* // => [1, 2, 3]
*
* curried(2, 3)(1);
* // => [1, 2, 3]
*
* curried(1, 2, 3);
* // => [1, 2, 3]
* ```
*/
function curryRight(func, arity = func.length) {
// Return a recursive function that collects arguments from right to left
function curried(...args) {
// If we have enough args, call the original function with reversed args
if (args.length >= arity) {
// Apply the arguments in reverse order
return func.apply(this, args.reverse().slice(0, arity).reverse());
}
// Otherwise, return a function that collects more args
return (...moreArgs) => {
return curried.apply(this, [...args, ...moreArgs]);
};
}
return curried;
}