curryRight
function
This method is like curry except that arguments are applied to func in the manner of partialRight instead of partial.
Installation
Import
import { curryRight } from '@tulx/utils';Source Code
Implementation
/**
* 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 Returns the new curried function.
*
* @example
* ```ts
* const abc = (a: string, b: string, c: string) => [a, b, c];
* const curried = curryRight(abc);
* curried('c')('b')('a'); // ['a', 'b', 'c']
* curried('c', 'b')('a'); // ['a', 'b', 'c']
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function curryRight<T extends (...args: any[]) => any>(
func: T,
arity: number = func.length
): (...args: unknown[]) => unknown {
return function curried(this: unknown, ...args: unknown[]): unknown {
if (args.length >= arity) {
return func.apply(this, args);
}
return (...nextArgs: unknown[]) =>
curried.apply(this, [...nextArgs, ...args]);
};
}
Example
import { curryRight } from '@tulx/utils';
const abc = (a: string, b: string, c: string) => [a, b, c];
const curried = curryRight(abc);
curried('c')('b')('a'); // ['a', 'b', 'c']
curried('c', 'b')('a'); // ['a', 'b', 'c']Related Functions
after
The opposite of before; this method creates a function that invokes func once it's called n or more times.
ary
Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
before
Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.
bind
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
bindKey
Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.
curry
Creates a function that accepts arguments of func and either invokes func returning its result, or returns a function that accepts the remaining arguments.