Function composition, currying, debouncing, throttling, and other function manipulation utilities for enhancing and controlling function behavior.
Creates a function that invokes func once it's called n or more times.
/**
* Creates a function that invokes `func` once it's called `n` or more times.
* @param n - The number of calls before `func` is invoked
* @param func - The function to restrict
* @returns Returns the new restricted function
*/
function after<T extends (...args: any[]) => any>(n: number, func: T): T;Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
/**
* Creates a function that invokes `func`, with up to `n` arguments,
* ignoring any additional arguments.
* @param func - The function to cap arguments for
* @param n - The arity cap (defaults to func.length)
* @returns Returns the new capped function
*/
function ary<T extends (...args: any[]) => any>(func: T, n?: number): T;Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.
/**
* Creates a function that invokes `func`, with the `this` binding and arguments
* of the created function, while it's called less than `n` times. Subsequent
* calls to the created function return the result of the last `func` invocation.
* @param n - The number of calls at which `func` is no longer invoked
* @param func - The function to restrict
* @returns Returns the new restricted function
*/
function before<T extends (...args: any[]) => any>(n: number, func: T): T;Creates a function that invokes func with the this binding of thisArg and prepends any additional bind arguments to those provided to the bound function.
/**
* Creates a function that invokes `func` with the `this` binding of `thisArg`
* and `partials` prepended to the arguments it receives.
* @param func - The function to bind
* @param thisArg - The `this` binding of `func`
* @param partials - The arguments to be partially applied
* @returns Returns the new bound function
*/
function bind<T extends (...args: any[]) => any>(
func: T,
thisArg: any,
...partials: any[]
): T;Binds methods of an object to the object itself, overwriting the existing methods.
/**
* Binds methods of an object to the object itself, overwriting the existing
* methods.
* @param object - The object to bind and assign the bound methods to
* @param methodNames - The object method names to bind
* @returns Returns `object`
*/
function bindAll<T>(object: T, ...methodNames: string[]): T;Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.
/**
* Creates a function that invokes the method at `object[key]` with `partials`
* prepended to the arguments it receives.
* @param object - The object to invoke the method on
* @param key - The key of the method
* @param partials - The arguments to be partially applied
* @returns Returns the new bound function
*/
function bindKey(
object: any,
key: string,
...partials: any[]
): (...args: any[]) => any;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.
/**
* 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.
* @param func - The function to curry
* @param arity - The arity of `func` (defaults to func.length)
* @returns Returns the new curried function
*/
function curry<T extends (...args: any[]) => any>(func: T, arity?: number): CurriedFunction<T>;
interface CurriedFunction<T> {
(): CurriedFunction<T>;
<A>(a: A): T extends (a: A, ...rest: any[]) => any ? CurriedFunction<T> : never;
<A, B>(a: A, b: B): T extends (a: A, b: B, ...rest: any[]) => any ? CurriedFunction<T> : never;
<A, B, C>(a: A, b: B, c: C): T extends (a: A, b: B, c: C, ...rest: any[]) => any ? CurriedFunction<T> : never;
(...args: any[]): any;
}Like curry except that arguments are applied to func in the manner of partialRight instead of partial.
/**
* 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` (defaults to func.length)
* @returns Returns the new curried function
*/
function curryRight<T extends (...args: any[]) => any>(
func: T,
arity?: number
): CurriedFunction<T>;Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.
/**
* Creates a debounced function that delays invoking `func` until after `wait`
* milliseconds have elapsed since the last time the debounced function was
* invoked.
* @param func - The function to debounce
* @param wait - The number of milliseconds to delay (defaults to 0)
* @param options - The options object
* @returns Returns the new debounced function
*/
function debounce<T extends (...args: any[]) => any>(
func: T,
wait?: number,
options?: DebounceOptions
): DebouncedFunc<T>;
interface DebounceOptions {
/** Specify invoking on the leading edge of the timeout */
leading?: boolean;
/** The maximum time `func` is allowed to be delayed before it's invoked */
maxWait?: number;
/** Specify invoking on the trailing edge of the timeout */
trailing?: boolean;
}
interface DebouncedFunc<T extends (...args: any[]) => any> {
(...args: Parameters<T>): ReturnType<T> | undefined;
/** Cancels any pending invocation */
cancel(): void;
/** Immediately invokes the function */
flush(): ReturnType<T> | undefined;
}Defers invoking the func until the current call stack has cleared.
/**
* Defers invoking the `func` until the current call stack has cleared.
* Any additional arguments are provided to `func` when it's invoked.
* @param func - The function to defer
* @param args - The arguments to invoke `func` with
* @returns Returns the timer id
*/
function defer<T extends (...args: any[]) => any>(func: T, ...args: Parameters<T>): number;Invokes func after wait milliseconds.
/**
* Invokes `func` after `wait` milliseconds. Any additional arguments are
* provided to `func` when it's invoked.
* @param func - The function to delay
* @param wait - The number of milliseconds to delay invocation
* @param args - The arguments to invoke `func` with
* @returns Returns the timer id
*/
function delay<T extends (...args: any[]) => any>(
func: T,
wait: number,
...args: Parameters<T>
): number;Creates a function that invokes func with arguments flipped.
/**
* Creates a function that invokes `func` with arguments flipped.
* @param func - The function to flip arguments for
* @returns Returns the new flipped function
*/
function flip<T extends (...args: any[]) => any>(func: T): T;Creates a function that memoizes the result of func.
/**
* Creates a function that memoizes the result of `func`. If `resolver` is
* provided, it determines the cache key for storing the result based on the
* arguments provided to the memoized function.
* @param func - The function to have its output memoized
* @param resolver - The function to resolve the cache key
* @returns Returns the new memoized function
*/
function memoize<T extends (...args: any[]) => any>(
func: T,
resolver?: (...args: Parameters<T>) => any
): MemoizedFunction<T>;
interface MemoizedFunction<T extends (...args: any[]) => any> extends T {
/** The cache object for the memoized function */
cache: Map<any, ReturnType<T>>;
}Creates a function that negates the result of the predicate func.
/**
* Creates a function that negates the result of the predicate `func`. The
* `func` predicate is invoked with the `this` binding and arguments of the
* created function.
* @param predicate - The predicate to negate
* @returns Returns the new negated function
*/
function negate<T extends (...args: any[]) => boolean>(predicate: T): T;Creates a function that is restricted to invoking func once.
/**
* Creates a function that is restricted to invoking `func` once. Repeat calls
* to the function return the value of the first invocation.
* @param func - The function to restrict
* @returns Returns the new restricted function
*/
function once<T extends (...args: any[]) => any>(func: T): T;Creates a function that invokes func with its arguments transformed.
/**
* Creates a function that invokes `func` with its arguments transformed.
* @param func - The function to wrap
* @param transforms - The argument transforms
* @returns Returns the new function
*/
function overArgs<T extends (...args: any[]) => any>(
func: T,
...transforms: Array<(arg: any) => any>
): T;Creates a function that invokes func with partials prepended to the arguments it receives.
/**
* Creates a function that invokes `func` with `partials` prepended to the
* arguments it receives. This method is like `bind` except it does **not**
* alter the `this` binding.
* @param func - The function to partially apply arguments to
* @param partials - The arguments to be partially applied
* @returns Returns the new partially applied function
*/
function partial<T extends (...args: any[]) => any>(
func: T,
...partials: any[]
): T;Like partial except that partially applied arguments are appended to the arguments it receives.
/**
* This method is like `partial` except that partially applied arguments
* are appended to the arguments it receives.
* @param func - The function to partially apply arguments to
* @param partials - The arguments to be partially applied
* @returns Returns the new partially applied function
*/
function partialRight<T extends (...args: any[]) => any>(
func: T,
...partials: any[]
): T;Creates a function that invokes func with arguments arranged according to the specified indexes.
/**
* Creates a function that invokes `func` with arguments arranged according
* to the specified `indexes` where the argument value at the first index is
* provided as the first argument, the argument value at the second index is
* provided as the second argument, and so on.
* @param func - The function to rearrange arguments for
* @param indexes - The arranged argument indexes
* @returns Returns the new function
*/
function rearg<T extends (...args: any[]) => any>(
func: T,
...indexes: number[]
): T;Creates a function that invokes func with the this binding of the created function and arguments from start and beyond provided as an array.
/**
* Creates a function that invokes `func` with the `this` binding of the
* created function and arguments from `start` and beyond provided as
* an array.
* @param func - The function to apply a rest parameter to
* @param start - The start position of the rest parameter (defaults to func.length - 1)
* @returns Returns the new function
*/
function rest<T extends (...args: any[]) => any>(
func: T,
start?: number
): (...args: any[]) => ReturnType<T>;Creates a function that invokes func with the this binding of the created function and an array of arguments much like Function#apply.
/**
* Creates a function that invokes `func` with the `this` binding of the
* created function and an array of arguments much like
* [`Function#apply`](http://www.ecma-international.org/ecma-262/7.0/#sec-function.prototype.apply).
* @param func - The function to spread arguments over
* @param start - The start position of the spread (defaults to 0)
* @returns Returns the new function
*/
function spread<T extends (args: any[]) => any>(
func: T,
start?: number
): (...args: any[]) => ReturnType<T>;Creates a throttled function that only invokes func at most once per every wait milliseconds.
/**
* Creates a throttled function that only invokes `func` at most once per
* every `wait` milliseconds. The throttled function comes with a `cancel`
* method to cancel delayed `func` invocations and a `flush` method to
* immediately invoke them.
* @param func - The function to throttle
* @param wait - The number of milliseconds to throttle invocations to (defaults to 0)
* @param options - The options object
* @returns Returns the new throttled function
*/
function throttle<T extends (...args: any[]) => any>(
func: T,
wait?: number,
options?: ThrottleOptions
): ThrottledFunc<T>;
interface ThrottleOptions {
/** Specify invoking on the leading edge of the timeout */
leading?: boolean;
/** Specify invoking on the trailing edge of the timeout */
trailing?: boolean;
}
interface ThrottledFunc<T extends (...args: any[]) => any> {
(...args: Parameters<T>): ReturnType<T> | undefined;
/** Cancels any pending invocation */
cancel(): void;
/** Immediately invokes the function */
flush(): ReturnType<T> | undefined;
}Creates a function that accepts up to one argument, ignoring any additional arguments.
/**
* Creates a function that accepts up to one argument, ignoring any
* additional arguments.
* @param func - The function to cap arguments for
* @returns Returns the new capped function
*/
function unary<T extends (arg: any, ...rest: any[]) => any>(func: T): (arg: Parameters<T>[0]) => ReturnType<T>;Creates a function that provides value to wrapper as its first argument.
/**
* Creates a function that provides `value` to `wrapper` as its first
* argument. Any additional arguments provided to the function are appended
* to those provided to the `wrapper`.
* @param value - The value to wrap
* @param wrapper - The wrapper function
* @returns Returns the new function
*/
function wrap<T, R>(
value: T,
wrapper: (value: T, ...args: any[]) => R
): (...args: any[]) => R;Creates a function that is the composition of the provided functions, where each successive invocation is supplied the return value of the previous.
/**
* Creates a function that is the composition of the provided functions,
* where each successive invocation is supplied the return value of the previous.
* @param funcs - The functions to invoke
* @returns Returns the new composite function
*/
function flow<A extends any[], R1, R2, R3, R4, R5, R6, R7>(
f1: (...args: A) => R1,
f2: (a: R1) => R2,
f3?: (a: R2) => R3,
f4?: (a: R3) => R4,
f5?: (a: R4) => R5,
f6?: (a: R5) => R6,
f7?: (a: R6) => R7
): (...args: A) => R7;
function flow(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;Like flow except that it creates a function that invokes the given functions from right to left.
/**
* This method is like `flow` except that it creates a function that invokes
* the given functions from right to left.
* @param funcs - The functions to invoke
* @returns Returns the new composite function
*/
function flowRight<A extends any[], R1, R2, R3, R4, R5, R6, R7>(
f7: (a: R6) => R7,
f6?: (a: R5) => R6,
f5?: (a: R4) => R5,
f4?: (a: R3) => R4,
f3?: (a: R2) => R3,
f2?: (a: R1) => R2,
f1?: (...args: A) => R1
): (...args: A) => R7;
function flowRight(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;