- 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
util-methods.md docs/
1# Utility Methods23General-purpose utilities including function creation, control flow, miscellaneous helpers, and core Lodash functionality.45## Capabilities67### Attempt89Attempts to invoke func, returning either the result or the caught error object.1011```javascript { .api }12/**13* Attempts to invoke `func`, returning either the result or the caught error object.14* Any additional arguments are provided to `func` when it's invoked.15* @param func - The function to attempt16* @param args - The arguments to invoke `func` with17* @returns Returns the `func` result or error object18*/19function attempt<T>(func: (...args: any[]) => T, ...args: any[]): T | Error;20```2122### Bind All2324Binds methods of an object to the object itself, overwriting the existing methods.2526```javascript { .api }27/**28* Binds methods of an object to the object itself, overwriting the existing29* methods.30* @param object - The object to bind and assign the bound methods to31* @param methodNames - The object method names to bind32* @returns Returns `object`33*/34function bindAll<T>(object: T, ...methodNames: string[]): T;35```3637### Cond3839Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.4041```javascript { .api }42/**43* Creates a function that iterates over `pairs` and invokes the corresponding44* function of the first predicate to return truthy. The predicate-function45* pairs are invoked with the `this` binding and arguments of the created function.46* @param pairs - The predicate-function pairs47* @returns Returns the new composite function48*/49function cond<T, R>(pairs: Array<[(args: T) => boolean, (args: T) => R]>): (args: T) => R;50function cond<R>(pairs: Array<[(...args: any[]) => boolean, (...args: any[]) => R]>): (...args: any[]) => R;51```5253### Conforms5455Creates a function that invokes the predicate properties of source with the corresponding property values of a given object, returning true if all predicates return truthy, else false.5657```javascript { .api }58/**59* Creates a function that invokes the predicate properties of `source` with60* the corresponding property values of a given object, returning `true` if61* all predicates return truthy, else `false`.62* @param source - The object of property predicates to conform to63* @returns Returns the new spec function64*/65function conforms<T>(source: Record<keyof T, (value: any) => boolean>): (object: T) => boolean;66```6768### Constant6970Creates a function that returns value.7172```javascript { .api }73/**74* Creates a function that returns `value`.75* @param value - The value to return from the new function76* @returns Returns the new constant function77*/78function constant<T>(value: T): () => T;79```8081### Default To8283Checks value to determine whether a default value should be returned in its place.8485```javascript { .api }86/**87* Checks `value` to determine whether a default value should be returned in88* its place. The `defaultValue` is returned if `value` is `NaN`, `null`,89* or `undefined`.90* @param value - The value to check91* @param defaultValue - The default value92* @returns Returns the resolved value93*/94function defaultTo<T>(value: T | null | undefined, defaultValue: T): T;95function defaultTo<T, TDefault>(value: T | null | undefined, defaultValue: TDefault): T | TDefault;96```9798### Flow99100Creates a function that is the composition of the provided functions, where each successive invocation is supplied the return value of the previous.101102```javascript { .api }103/**104* Creates a function that is the composition of the provided functions,105* where each successive invocation is supplied the return value of the previous.106* @param funcs - The functions to invoke107* @returns Returns the new composite function108*/109function flow<A extends any[], R1, R2, R3, R4, R5, R6, R7>(110f1: (...args: A) => R1,111f2: (a: R1) => R2,112f3?: (a: R2) => R3,113f4?: (a: R3) => R4,114f5?: (a: R4) => R5,115f6?: (a: R5) => R6,116f7?: (a: R6) => R7117): (...args: A) => R7;118119function flow(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;120```121122### Flow Right123124Like flow except that it creates a function that invokes the given functions from right to left.125126```javascript { .api }127/**128* This method is like `flow` except that it creates a function that invokes129* the given functions from right to left.130* @param funcs - The functions to invoke131* @returns Returns the new composite function132*/133function flowRight<A extends any[], R1, R2, R3, R4, R5, R6, R7>(134f7: (a: R6) => R7,135f6?: (a: R5) => R6,136f5?: (a: R4) => R5,137f4?: (a: R3) => R4,138f3?: (a: R2) => R3,139f2?: (a: R1) => R2,140f1?: (...args: A) => R1141): (...args: A) => R7;142143function flowRight(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;144```145146### Identity147148This method returns the first argument it receives.149150```javascript { .api }151/**152* This method returns the first argument it receives.153* @param value - Any value154* @returns Returns `value`155*/156function identity<T>(value: T): T;157```158159### Iteratee160161Creates a function that invokes func with the arguments of the created function.162163```javascript { .api }164/**165* Creates a function that invokes `func` with the arguments of the created166* function. If `func` is a property name, the created function returns the167* property value for a given element. If `func` is an array or object, the168* created function returns `true` for elements that contain the equivalent169* source properties, otherwise it returns `false`.170* @param func - The value to convert to a callback171* @returns Returns the callback172*/173function iteratee<T>(func?: T): T extends string174? (object: any) => any175: T extends object176? (object: any) => boolean177: T extends (...args: any[]) => any178? T179: (value: any) => any;180```181182### Matches183184Creates a function that performs a partial deep comparison between a given object and source, returning true if the given object has equivalent property values, else false.185186```javascript { .api }187/**188* Creates a function that performs a partial deep comparison between a given189* object and `source`, returning `true` if the given object has equivalent190* property values, else `false`.191* @param source - The object of property values to match192* @returns Returns the new spec function193*/194function matches<T>(source: T): (object: any) => boolean;195```196197### Matches Property198199Creates a function that performs a partial deep comparison between the value at path of a given object to srcValue, returning true if the object value is equivalent, else false.200201```javascript { .api }202/**203* Creates a function that performs a partial deep comparison between the204* value at `path` of a given object to `srcValue`, returning `true` if the205* object value is equivalent, else `false`.206* @param path - The path of the property to get207* @param srcValue - The value to match208* @returns Returns the new spec function209*/210function matchesProperty(path: PropertyPath, srcValue: any): (object: any) => boolean;211```212213### Method214215Creates a function that invokes the method at path of a given object.216217```javascript { .api }218/**219* Creates a function that invokes the method at `path` of a given object.220* Any additional arguments are provided to the invoked method.221* @param path - The path of the method to invoke222* @param args - The arguments to invoke the method with223* @returns Returns the new invoker function224*/225function method(path: PropertyPath, ...args: any[]): (object: any) => any;226```227228### Method Of229230The opposite of method; this method creates a function that invokes the method at a given path of object.231232```javascript { .api }233/**234* The opposite of `method`; this method creates a function that invokes235* the method at a given path of `object`. Any additional arguments are236* provided to the invoked method.237* @param object - The object to query238* @param args - The arguments to invoke the method with239* @returns Returns the new invoker function240*/241function methodOf(object: any, ...args: any[]): (path: PropertyPath) => any;242```243244### Mixin245246Adds all own enumerable string keyed function properties of a source object to the destination object.247248```javascript { .api }249/**250* Adds all own enumerable string keyed function properties of a source251* object to the destination object. If `object` is a function, then methods252* are added to its prototype as well.253* @param object - The destination object254* @param source - The object of functions to add255* @param options - The options object256* @returns Returns `object`257*/258function mixin<T>(object: T, source: any, options?: { chain?: boolean }): T;259function mixin(source: any, options?: { chain?: boolean }): typeof _;260```261262### No Conflict263264Reverts the _ variable to its previous value and returns a reference to the lodash function.265266```javascript { .api }267/**268* Reverts the `_` variable to its previous value and returns a reference to269* the `lodash` function.270* @returns Returns the `lodash` function271*/272function noConflict(): typeof _;273```274275### Noop276277This method returns undefined.278279```javascript { .api }280/**281* This method returns `undefined`.282* @returns Returns `undefined`283*/284function noop(): void;285```286287### Nth Arg288289Creates a function that gets the argument at index n.290291```javascript { .api }292/**293* Creates a function that gets the argument at index `n`. If `n` is negative,294* the nth argument from the end is returned.295* @param n - The index of the argument to return296* @returns Returns the new pass-thru function297*/298function nthArg(n?: number): (...args: any[]) => any;299```300301### Over302303Creates a function that invokes iteratees with the arguments it receives and returns their results.304305```javascript { .api }306/**307* Creates a function that invokes `iteratees` with the arguments it receives308* and returns their results.309* @param iteratees - The iteratees to invoke310* @returns Returns the new function311*/312function over<T extends ReadonlyArray<(...args: any[]) => any>>(313...iteratees: T314): (...args: any[]) => { [K in keyof T]: T[K] extends (...args: any[]) => infer R ? R : never };315```316317### Over Every318319Creates a function that checks if all of the provided predicates return truthy when invoked with the arguments it receives.320321```javascript { .api }322/**323* Creates a function that checks if **all** of the provided predicates return324* truthy when invoked with the arguments it receives.325* @param predicates - The predicates to check326* @returns Returns the new function327*/328function overEvery<T extends ReadonlyArray<(...args: any[]) => boolean>>(329...predicates: T330): (...args: any[]) => boolean;331```332333### Over Some334335Creates a function that checks if any of the provided predicates return truthy when invoked with the arguments it receives.336337```javascript { .api }338/**339* Creates a function that checks if **any** of the provided predicates return340* truthy when invoked with the arguments it receives.341* @param predicates - The predicates to check342* @returns Returns the new function343*/344function overSome<T extends ReadonlyArray<(...args: any[]) => boolean>>(345...predicates: T346): (...args: any[]) => boolean;347```348349### Property350351Creates a function that returns the value at path of a given object.352353```javascript { .api }354/**355* Creates a function that returns the value at `path` of a given object.356* @param path - The path of the property to get357* @returns Returns the new accessor function358*/359function property<T>(path: PropertyPath): (obj: any) => T;360```361362### Property Of363364The opposite of property; this method creates a function that returns the value at a given path of object.365366```javascript { .api }367/**368* The opposite of `property`; this method creates a function that returns369* the value at a given path of `object`.370* @param object - The object to query371* @returns Returns the new accessor function372*/373function propertyOf<T>(object: T): (path: PropertyPath) => any;374```375376### Run In Context377378Create a pristine lodash function to avoid conflicts caused by modifying the original.379380```javascript { .api }381/**382* Create a pristine `lodash` function to avoid conflicts caused by modifying383* the original.384* @param context - The context object385* @returns Returns a new `lodash` function386*/387function runInContext(context?: any): typeof _;388```389390### Times391392Invokes the iteratee n times, returning an array of the results of each invocation.393394```javascript { .api }395/**396* Invokes the iteratee `n` times, returning an array of the results of397* each invocation. The iteratee is invoked with one argument; (index).398* @param n - The number of times to invoke `iteratee`399* @param iteratee - The function invoked per iteration400* @returns Returns the array of results401*/402function times<T>(n: number, iteratee: (index: number) => T): T[];403function times(n: number): number[];404```405406### Unique Id407408Generates a unique ID.409410```javascript { .api }411/**412* Generates a unique ID. If `prefix` is given, the ID is appended to it.413* @param prefix - The value to prefix the ID with414* @returns Returns the unique ID415*/416function uniqueId(prefix?: string): string;417```418419## Date Methods420421### Now422423Gets the timestamp of the number of milliseconds that have elapsed since the Unix epoch.424425```javascript { .api }426/**427* Gets the timestamp of the number of milliseconds that have elapsed since428* the Unix epoch (1 January 1970 00:00:00 UTC).429* @returns Returns the timestamp430*/431function now(): number;432```433434## Number Methods435436### Clamp437438Clamps number within the inclusive lower and upper bounds.439440```javascript { .api }441/**442* Clamps `number` within the inclusive `lower` and `upper` bounds.443* @param number - The number to clamp444* @param lower - The lower bound445* @param upper - The upper bound446* @returns Returns the clamped number447*/448function clamp(number: number, lower: number, upper?: number): number;449```450451### Random452453Produces a random number between the inclusive lower and upper bounds.454455```javascript { .api }456/**457* Produces a random number between the inclusive `lower` and `upper` bounds.458* If only one argument is provided a number between `0` and the given number459* is returned. If `floating` is `true`, or either `lower` or `upper` are460* floats, a floating-point number is returned instead of an integer.461* @param lower - The lower bound462* @param upper - The upper bound463* @param floating - Specify returning a floating-point number464* @returns Returns the random number465*/466function random(lower?: number, upper?: number, floating?: boolean): number;467function random(floating?: boolean): number;468```469470## Sequence Methods471472### Chain473474Creates a lodash wrapper instance that wraps value with explicit method chain sequences enabled.475476```javascript { .api }477/**478* Creates a `lodash` wrapper instance that wraps `value` with explicit method479* chain sequences enabled. The result of such sequences must be unwrapped480* with `_#value`.481* @param value - The value to wrap482* @returns Returns the new `lodash` wrapper instance483*/484function chain<T>(value: T): LodashExplicitWrapper<T>;485```486487### Tap488489This method invokes interceptor and returns value.490491```javascript { .api }492/**493* This method invokes `interceptor` and returns `value`. The interceptor494* is invoked with one argument; (value). The purpose of this method is to495* "tap into" a method chain sequence in order to modify intermediate results.496* @param value - The value to provide to `interceptor`497* @param interceptor - The function to invoke498* @returns Returns `value`499*/500function tap<T>(value: T, interceptor: (value: T) => void): T;501```502503### Thru504505This method is like tap except that it returns the result of interceptor.506507```javascript { .api }508/**509* This method is like `tap` except that it returns the result of `interceptor`.510* The purpose of this method is to "pass thru" values replacing intermediate511* results in a method chain sequence.512* @param value - The value to provide to `interceptor`513* @param interceptor - The function to invoke514* @returns Returns the result of `interceptor`515*/516function thru<T, R>(value: T, interceptor: (value: T) => R): R;517```518519## Method Aliases520521### Each522523Alias for forEach. Iterates over elements of collection and invokes iteratee for each element.524525```javascript { .api }526/**527* Iterates over elements of `collection` and invokes `iteratee` for each element.528* The iteratee is invoked with three arguments: (value, index|key, collection).529* Iteratee functions may exit iteration early by explicitly returning `false`.530* @param collection - The collection to iterate over531* @param iteratee - The function invoked per iteration532* @returns Returns `collection`533*/534function each<T>(collection: T[], iteratee?: (value: T, index: number, collection: T[]) => any): T[];535function each<T>(collection: Record<string, T>, iteratee?: (value: T, key: string, collection: Record<string, T>) => any): Record<string, T>;536```537538### Each Right539540Alias for forEachRight. Like each except that it iterates over elements of collection from right to left.541542```javascript { .api }543/**544* This method is like `each` except that it iterates over elements of545* `collection` from right to left.546* @param collection - The collection to iterate over547* @param iteratee - The function invoked per iteration548* @returns Returns `collection`549*/550function eachRight<T>(collection: T[], iteratee?: (value: T, index: number, collection: T[]) => any): T[];551function eachRight<T>(collection: Record<string, T>, iteratee?: (value: T, key: string, collection: Record<string, T>) => any): Record<string, T>;552```553554### Entries555556Alias for toPairs. Creates an array of own enumerable string keyed-value pairs for object.557558```javascript { .api }559/**560* Creates an array of own enumerable string keyed-value pairs for `object`561* which can be consumed by `fromPairs`. If `object` is a map or set, its562* entries are returned.563* @param object - The object to query564* @returns Returns the key-value pairs565*/566function entries<T>(object?: Record<string, T>): Array<[string, T]>;567function entries(object?: any): Array<[string, any]>;568```569570### Entries In571572Alias for toPairsIn. Creates an array of own and inherited enumerable string keyed-value pairs for object.573574```javascript { .api }575/**576* Creates an array of own and inherited enumerable string keyed-value pairs577* for `object` which can be consumed by `fromPairs`. If `object` is a map578* or set, its entries are returned.579* @param object - The object to query580* @returns Returns the key-value pairs581*/582function entriesIn<T>(object?: Record<string, T>): Array<[string, T]>;583function entriesIn(object?: any): Array<[string, any]>;584```585586### Extend587588Alias for assign. Assigns own enumerable string keyed properties of source objects to the destination object.589590```javascript { .api }591/**592* Assigns own enumerable string keyed properties of source objects to the593* destination object. Source objects are applied from left to right.594* Subsequent sources overwrite property assignments of previous sources.595* @param object - The destination object596* @param sources - The source objects597* @returns Returns `object`598*/599function extend<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;600function extend<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;601function extend<TObject>(object: TObject, ...sources: any[]): any;602```603604### Extend With605606Alias for assignWith. Like extend except that it accepts customizer which is invoked to produce the assigned values.607608```javascript { .api }609/**610* This method is like `extend` except that it accepts `customizer` which611* is invoked to produce the assigned values. If `customizer` returns `undefined`,612* assignment is handled by the method instead. The `customizer` is invoked613* with five arguments: (objValue, srcValue, key, object, source).614* @param object - The destination object615* @param sources - The source objects616* @param customizer - The function to customize assigned values617* @returns Returns `object`618*/619function extendWith<TObject, TSource>(620object: TObject,621source: TSource,622customizer: (objValue: any, srcValue: any, key: string, object: TObject, source: TSource) => any623): TObject & TSource;624function extendWith<TObject>(object: TObject, ...otherArgs: any[]): any;625```626627### First628629Alias for head. Gets the first element of array.630631```javascript { .api }632/**633* Gets the first element of `array`.634* @param array - The array to query635* @returns Returns the first element of `array`636*/637function first<T>(array: T[] | null | undefined): T | undefined;638```639640## Types641642```javascript { .api }643type PropertyPath = string | number | symbol | Array<string | number | symbol>;644645interface LodashExplicitWrapper<TValue> {646chain(): LodashExplicitWrapper<TValue>;647value(): TValue;648valueOf(): TValue;649}650651declare const _: LoDashStatic;652653interface LoDashStatic {654VERSION: string;655templateSettings: TemplateSettings;656657// All lodash methods658[key: string]: any;659}660661interface TemplateSettings {662escape?: RegExp;663evaluate?: RegExp;664interpolate?: RegExp;665variable?: string;666}667```