- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.9.x
- Description
- A comprehensive JavaScript utility library with 296+ functions for arrays, objects, strings, and functional programming
- Author
- tessl
- Last updated
function-methods.md docs/
1# Function Methods23Function composition, currying, debouncing, throttling, and other function manipulation utilities for enhancing and controlling function behavior.45## Capabilities67### After89Creates a function that invokes func once it's called n or more times.1011```javascript { .api }12/**13* Creates a function that invokes `func` once it's called `n` or more times.14* @param n - The number of calls before `func` is invoked15* @param func - The function to restrict16* @returns Returns the new restricted function17*/18function after<T extends (...args: any[]) => any>(n: number, func: T): T;19```2021### Ary2223Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.2425```javascript { .api }26/**27* Creates a function that invokes `func`, with up to `n` arguments,28* ignoring any additional arguments.29* @param func - The function to cap arguments for30* @param n - The arity cap (defaults to func.length)31* @returns Returns the new capped function32*/33function ary<T extends (...args: any[]) => any>(func: T, n?: number): T;34```3536### Before3738Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.3940```javascript { .api }41/**42* Creates a function that invokes `func`, with the `this` binding and arguments43* of the created function, while it's called less than `n` times. Subsequent44* calls to the created function return the result of the last `func` invocation.45* @param n - The number of calls at which `func` is no longer invoked46* @param func - The function to restrict47* @returns Returns the new restricted function48*/49function before<T extends (...args: any[]) => any>(n: number, func: T): T;50```5152### Bind5354Creates a function that invokes func with the this binding of thisArg and prepends any additional bind arguments to those provided to the bound function.5556```javascript { .api }57/**58* Creates a function that invokes `func` with the `this` binding of `thisArg`59* and `partials` prepended to the arguments it receives.60* @param func - The function to bind61* @param thisArg - The `this` binding of `func`62* @param partials - The arguments to be partially applied63* @returns Returns the new bound function64*/65function bind<T extends (...args: any[]) => any>(66func: T,67thisArg: any,68...partials: any[]69): T;70```7172### Bind All7374Binds methods of an object to the object itself, overwriting the existing methods.7576```javascript { .api }77/**78* Binds methods of an object to the object itself, overwriting the existing79* methods.80* @param object - The object to bind and assign the bound methods to81* @param methodNames - The object method names to bind82* @returns Returns `object`83*/84function bindAll<T>(object: T, ...methodNames: string[]): T;85```8687### Bind Key8889Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.9091```javascript { .api }92/**93* Creates a function that invokes the method at `object[key]` with `partials`94* prepended to the arguments it receives.95* @param object - The object to invoke the method on96* @param key - The key of the method97* @param partials - The arguments to be partially applied98* @returns Returns the new bound function99*/100function bindKey(101object: any,102key: string,103...partials: any[]104): (...args: any[]) => any;105```106107### Curry108109Creates 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.110111```javascript { .api }112/**113* Creates a function that accepts arguments of `func` and either invokes114* `func` returning its result, if at least `arity` number of arguments have115* been provided, or returns a function that accepts the remaining `func`116* arguments, and so on.117* @param func - The function to curry118* @param arity - The arity of `func` (defaults to func.length)119* @returns Returns the new curried function120*/121function curry<T extends (...args: any[]) => any>(func: T, arity?: number): CurriedFunction<T>;122123interface CurriedFunction<T> {124(): CurriedFunction<T>;125<A>(a: A): T extends (a: A, ...rest: any[]) => any ? CurriedFunction<T> : never;126<A, B>(a: A, b: B): T extends (a: A, b: B, ...rest: any[]) => any ? CurriedFunction<T> : never;127<A, B, C>(a: A, b: B, c: C): T extends (a: A, b: B, c: C, ...rest: any[]) => any ? CurriedFunction<T> : never;128(...args: any[]): any;129}130```131132### Curry Right133134Like curry except that arguments are applied to func in the manner of partialRight instead of partial.135136```javascript { .api }137/**138* This method is like `curry` except that arguments are applied to `func`139* in the manner of `partialRight` instead of `partial`.140* @param func - The function to curry141* @param arity - The arity of `func` (defaults to func.length)142* @returns Returns the new curried function143*/144function curryRight<T extends (...args: any[]) => any>(145func: T,146arity?: number147): CurriedFunction<T>;148```149150### Debounce151152Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.153154```javascript { .api }155/**156* Creates a debounced function that delays invoking `func` until after `wait`157* milliseconds have elapsed since the last time the debounced function was158* invoked.159* @param func - The function to debounce160* @param wait - The number of milliseconds to delay (defaults to 0)161* @param options - The options object162* @returns Returns the new debounced function163*/164function debounce<T extends (...args: any[]) => any>(165func: T,166wait?: number,167options?: DebounceOptions168): DebouncedFunc<T>;169170interface DebounceOptions {171/** Specify invoking on the leading edge of the timeout */172leading?: boolean;173/** The maximum time `func` is allowed to be delayed before it's invoked */174maxWait?: number;175/** Specify invoking on the trailing edge of the timeout */176trailing?: boolean;177}178179interface DebouncedFunc<T extends (...args: any[]) => any> {180(...args: Parameters<T>): ReturnType<T> | undefined;181/** Cancels any pending invocation */182cancel(): void;183/** Immediately invokes the function */184flush(): ReturnType<T> | undefined;185}186```187188### Defer189190Defers invoking the func until the current call stack has cleared.191192```javascript { .api }193/**194* Defers invoking the `func` until the current call stack has cleared.195* Any additional arguments are provided to `func` when it's invoked.196* @param func - The function to defer197* @param args - The arguments to invoke `func` with198* @returns Returns the timer id199*/200function defer<T extends (...args: any[]) => any>(func: T, ...args: Parameters<T>): number;201```202203### Delay204205Invokes func after wait milliseconds.206207```javascript { .api }208/**209* Invokes `func` after `wait` milliseconds. Any additional arguments are210* provided to `func` when it's invoked.211* @param func - The function to delay212* @param wait - The number of milliseconds to delay invocation213* @param args - The arguments to invoke `func` with214* @returns Returns the timer id215*/216function delay<T extends (...args: any[]) => any>(217func: T,218wait: number,219...args: Parameters<T>220): number;221```222223### Flip224225Creates a function that invokes func with arguments flipped.226227```javascript { .api }228/**229* Creates a function that invokes `func` with arguments flipped.230* @param func - The function to flip arguments for231* @returns Returns the new flipped function232*/233function flip<T extends (...args: any[]) => any>(func: T): T;234```235236### Memoize237238Creates a function that memoizes the result of func.239240```javascript { .api }241/**242* Creates a function that memoizes the result of `func`. If `resolver` is243* provided, it determines the cache key for storing the result based on the244* arguments provided to the memoized function.245* @param func - The function to have its output memoized246* @param resolver - The function to resolve the cache key247* @returns Returns the new memoized function248*/249function memoize<T extends (...args: any[]) => any>(250func: T,251resolver?: (...args: Parameters<T>) => any252): MemoizedFunction<T>;253254interface MemoizedFunction<T extends (...args: any[]) => any> extends T {255/** The cache object for the memoized function */256cache: Map<any, ReturnType<T>>;257}258```259260### Negate261262Creates a function that negates the result of the predicate func.263264```javascript { .api }265/**266* Creates a function that negates the result of the predicate `func`. The267* `func` predicate is invoked with the `this` binding and arguments of the268* created function.269* @param predicate - The predicate to negate270* @returns Returns the new negated function271*/272function negate<T extends (...args: any[]) => boolean>(predicate: T): T;273```274275### Once276277Creates a function that is restricted to invoking func once.278279```javascript { .api }280/**281* Creates a function that is restricted to invoking `func` once. Repeat calls282* to the function return the value of the first invocation.283* @param func - The function to restrict284* @returns Returns the new restricted function285*/286function once<T extends (...args: any[]) => any>(func: T): T;287```288289### Over Args290291Creates a function that invokes func with its arguments transformed.292293```javascript { .api }294/**295* Creates a function that invokes `func` with its arguments transformed.296* @param func - The function to wrap297* @param transforms - The argument transforms298* @returns Returns the new function299*/300function overArgs<T extends (...args: any[]) => any>(301func: T,302...transforms: Array<(arg: any) => any>303): T;304```305306### Partial307308Creates a function that invokes func with partials prepended to the arguments it receives.309310```javascript { .api }311/**312* Creates a function that invokes `func` with `partials` prepended to the313* arguments it receives. This method is like `bind` except it does **not**314* alter the `this` binding.315* @param func - The function to partially apply arguments to316* @param partials - The arguments to be partially applied317* @returns Returns the new partially applied function318*/319function partial<T extends (...args: any[]) => any>(320func: T,321...partials: any[]322): T;323```324325### Partial Right326327Like partial except that partially applied arguments are appended to the arguments it receives.328329```javascript { .api }330/**331* This method is like `partial` except that partially applied arguments332* are appended to the arguments it receives.333* @param func - The function to partially apply arguments to334* @param partials - The arguments to be partially applied335* @returns Returns the new partially applied function336*/337function partialRight<T extends (...args: any[]) => any>(338func: T,339...partials: any[]340): T;341```342343### Rearg344345Creates a function that invokes func with arguments arranged according to the specified indexes.346347```javascript { .api }348/**349* Creates a function that invokes `func` with arguments arranged according350* to the specified `indexes` where the argument value at the first index is351* provided as the first argument, the argument value at the second index is352* provided as the second argument, and so on.353* @param func - The function to rearrange arguments for354* @param indexes - The arranged argument indexes355* @returns Returns the new function356*/357function rearg<T extends (...args: any[]) => any>(358func: T,359...indexes: number[]360): T;361```362363### Rest364365Creates a function that invokes func with the this binding of the created function and arguments from start and beyond provided as an array.366367```javascript { .api }368/**369* Creates a function that invokes `func` with the `this` binding of the370* created function and arguments from `start` and beyond provided as371* an array.372* @param func - The function to apply a rest parameter to373* @param start - The start position of the rest parameter (defaults to func.length - 1)374* @returns Returns the new function375*/376function rest<T extends (...args: any[]) => any>(377func: T,378start?: number379): (...args: any[]) => ReturnType<T>;380```381382### Spread383384Creates a function that invokes func with the this binding of the created function and an array of arguments much like Function#apply.385386```javascript { .api }387/**388* Creates a function that invokes `func` with the `this` binding of the389* created function and an array of arguments much like390* [`Function#apply`](http://www.ecma-international.org/ecma-262/7.0/#sec-function.prototype.apply).391* @param func - The function to spread arguments over392* @param start - The start position of the spread (defaults to 0)393* @returns Returns the new function394*/395function spread<T extends (args: any[]) => any>(396func: T,397start?: number398): (...args: any[]) => ReturnType<T>;399```400401### Throttle402403Creates a throttled function that only invokes func at most once per every wait milliseconds.404405```javascript { .api }406/**407* Creates a throttled function that only invokes `func` at most once per408* every `wait` milliseconds. The throttled function comes with a `cancel`409* method to cancel delayed `func` invocations and a `flush` method to410* immediately invoke them.411* @param func - The function to throttle412* @param wait - The number of milliseconds to throttle invocations to (defaults to 0)413* @param options - The options object414* @returns Returns the new throttled function415*/416function throttle<T extends (...args: any[]) => any>(417func: T,418wait?: number,419options?: ThrottleOptions420): ThrottledFunc<T>;421422interface ThrottleOptions {423/** Specify invoking on the leading edge of the timeout */424leading?: boolean;425/** Specify invoking on the trailing edge of the timeout */426trailing?: boolean;427}428429interface ThrottledFunc<T extends (...args: any[]) => any> {430(...args: Parameters<T>): ReturnType<T> | undefined;431/** Cancels any pending invocation */432cancel(): void;433/** Immediately invokes the function */434flush(): ReturnType<T> | undefined;435}436```437438### Unary439440Creates a function that accepts up to one argument, ignoring any additional arguments.441442```javascript { .api }443/**444* Creates a function that accepts up to one argument, ignoring any445* additional arguments.446* @param func - The function to cap arguments for447* @returns Returns the new capped function448*/449function unary<T extends (arg: any, ...rest: any[]) => any>(func: T): (arg: Parameters<T>[0]) => ReturnType<T>;450```451452### Wrap453454Creates a function that provides value to wrapper as its first argument.455456```javascript { .api }457/**458* Creates a function that provides `value` to `wrapper` as its first459* argument. Any additional arguments provided to the function are appended460* to those provided to the `wrapper`.461* @param value - The value to wrap462* @param wrapper - The wrapper function463* @returns Returns the new function464*/465function wrap<T, R>(466value: T,467wrapper: (value: T, ...args: any[]) => R468): (...args: any[]) => R;469```470471## Flow Control472473### Flow474475Creates a function that is the composition of the provided functions, where each successive invocation is supplied the return value of the previous.476477```javascript { .api }478/**479* Creates a function that is the composition of the provided functions,480* where each successive invocation is supplied the return value of the previous.481* @param funcs - The functions to invoke482* @returns Returns the new composite function483*/484function flow<A extends any[], R1, R2, R3, R4, R5, R6, R7>(485f1: (...args: A) => R1,486f2: (a: R1) => R2,487f3?: (a: R2) => R3,488f4?: (a: R3) => R4,489f5?: (a: R4) => R5,490f6?: (a: R5) => R6,491f7?: (a: R6) => R7492): (...args: A) => R7;493494function flow(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;495```496497### Flow Right498499Like flow except that it creates a function that invokes the given functions from right to left.500501```javascript { .api }502/**503* This method is like `flow` except that it creates a function that invokes504* the given functions from right to left.505* @param funcs - The functions to invoke506* @returns Returns the new composite function507*/508function flowRight<A extends any[], R1, R2, R3, R4, R5, R6, R7>(509f7: (a: R6) => R7,510f6?: (a: R5) => R6,511f5?: (a: R4) => R5,512f4?: (a: R3) => R4,513f3?: (a: R2) => R3,514f2?: (a: R1) => R2,515f1?: (...args: A) => R1516): (...args: A) => R7;517518function flowRight(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;519```