@types/underscore
Version:
TypeScript definitions for underscore
1,316 lines (1,172 loc) • 251 kB
TypeScript
declare var _: _.UnderscoreStatic;
export = _;
export as namespace _;
// The DOM is not required to be present, but these definitions reference type Element for the
// isElement check. If the DOM is present, this declaration will merge.
declare global {
interface Element {}
}
declare namespace _ {
/**
* underscore.js _.throttle options.
*/
interface ThrottleSettings {
/**
* If you'd like to disable the leading-edge call, pass this as false.
*/
leading?: boolean | undefined;
/**
* If you'd like to disable the execution on the trailing-edge, pass false.
*/
trailing?: boolean | undefined;
}
/**
* underscore.js template settings, set templateSettings or pass as an argument
* to 'template()' to override defaults.
*/
interface TemplateSettings {
/**
* Default value is '/<%([\s\S]+?)%>/g'.
*/
evaluate?: RegExp | undefined;
/**
* Default value is '/<%=([\s\S]+?)%>/g'.
*/
interpolate?: RegExp | undefined;
/**
* Default value is '/<%-([\s\S]+?)%>/g'.
*/
escape?: RegExp | undefined;
/**
* By default, 'template()' places the values from your data in the local scope via the 'with' statement.
* However, you can specify a single variable name with this setting.
*/
variable?: string | undefined;
}
interface CompiledTemplate {
(data?: any): string;
source: string;
}
// Common interface between Arrays and jQuery objects
interface List<T> {
[index: number]: T;
length: number;
}
interface Dictionary<T> {
[index: string]: T;
}
type Collection<T> = List<T> | Dictionary<T>;
type CollectionKey<V> = V extends never ? any
: V extends List<any> ? number
: V extends Dictionary<any> ? string
: V extends undefined ? undefined
: never;
interface Predicate<T> {
(value: T): boolean;
}
interface CollectionIterator<T extends TypeOfList<V> | TypeOfDictionary<V, any>, TResult, V = Collection<T>> {
(element: T, key: CollectionKey<V>, collection: V): TResult;
}
interface ListIterator<T extends TypeOfList<V>, TResult, V = List<T>> extends CollectionIterator<T, TResult, V> {}
interface ObjectIterator<T extends TypeOfDictionary<V, any>, TResult, V = Dictionary<T>>
extends CollectionIterator<T, TResult, V>
{}
type Iteratee<V, R, T extends TypeOfCollection<V, any> = TypeOfCollection<V>> =
| CollectionIterator<T, R, V>
| string
| number
| Array<string | number>
| Partial<T>
| null
| undefined;
type IterateeResult<I, T> = I extends (...args: any[]) => infer R ? R
: I extends keyof T ? T[I]
: I extends string | number | Array<string | number> ? any
: I extends object ? boolean
: I extends null | undefined ? T
: never;
type PropertyTypeOrAny<T, K> = K extends keyof T ? T[K] : any;
interface MemoCollectionIterator<T extends TypeOfList<V> | TypeOfDictionary<V, any>, TResult, V = Collection<T>> {
(prev: TResult, curr: T, key: CollectionKey<V>, collection: V): TResult;
}
interface MemoIterator<T extends TypeOfList<V>, TResult, V = List<T>>
extends MemoCollectionIterator<T, TResult, V>
{}
interface MemoObjectIterator<T extends TypeOfDictionary<V>, TResult, V = Dictionary<T>>
extends MemoCollectionIterator<T, TResult, V>
{}
type TypeOfList<V> = V extends never ? any
: V extends List<infer T> ? T
: never;
type TypeOfDictionary<V, TDefault = never> = V extends never ? any
: V extends Dictionary<infer T> ? T
: TDefault;
type TypeOfCollection<V, TObjectDefault = never> = V extends List<any> ? TypeOfList<V>
: TypeOfDictionary<V, TObjectDefault>;
type DeepTypeOfCollection<V, P> = P extends [infer H, ...infer R]
? H extends keyof V ? DeepTypeOfCollection<V[H], R>
: never
: V;
type ListItemOrSelf<T> = T extends List<infer TItem> ? TItem : T;
// unfortunately it's not possible to recursively collapse all possible list dimensions to T[] at this time,
// so give up after one dimension since that's likely the most common case
// '& object' prevents strings from being matched by list checks so types like string[] don't end up resulting in any
type DeepestListItemOrSelf<T> = T extends List<infer TItem> & object ? TItem extends List<any> & object ? any
: TItem
: T;
// if T is an inferrable pair, the value type for the pair
// if T is a list, assume that it contains pairs of some type, so any
// if T isn't a list, there's no way that it can provide pairs, so never
type PairValue<T> = T extends Readonly<[string | number, infer TValue]> ? TValue
: T extends List<infer TValue> ? TValue
: never;
type AnyFalsy = undefined | null | false | "" | 0;
type Truthy<T> = Exclude<T, AnyFalsy>;
type _Pick<V, K extends string> = Extract<K, keyof V> extends never ? Partial<V>
: Pick<V, Extract<K, keyof V>>;
// switch to Omit when the minimum TS version moves past 3.5
type _Omit<V, K extends string> = V extends never ? any
: Extract<K, keyof V> extends never ? Partial<V>
: Pick<V, Exclude<keyof V, K>>;
type _ChainSingle<V> = _Chain<TypeOfCollection<V>, V>;
type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;
interface Cancelable {
cancel(): void;
}
interface UnderscoreStatic {
/*******
* OOP *
*******/
/**
* Underscore OOP Wrapper, all Underscore functions that take an object
* as the first parameter can be invoked through this function.
* @param value First argument to Underscore object functions.
* @returns An Underscore wrapper around the supplied value.
*/
<V>(value: V): Underscore<TypeOfCollection<V>, V>;
/***************
* Collections *
***************/
/**
* Iterates over a `collection` of elements, yielding each in turn to
* an `iteratee`. The `iteratee` is bound to the context object, if one
* is passed.
* @param collection The collection of elements to iterate over.
* @param iteratee The iteratee to call for each element in
* `collection`.
* @param context 'this' object in `iteratee`, optional.
* @returns The original collection.
*/
each<V extends Collection<any>>(
collection: V,
iteratee: CollectionIterator<TypeOfCollection<V>, void, V>,
context?: any,
): V;
/**
* @see each
*/
forEach: UnderscoreStatic["each"];
/**
* Produces a new array of values by mapping each value in `collection`
* through a transformation `iteratee`.
* @param collection The collection to transform.
* @param iteratee The iteratee to use to transform each item in
* `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns The mapped result.
*/
map<V extends Collection<any>, I extends Iteratee<V, any>>(
collection: V,
iteratee: I,
context?: any,
): Array<IterateeResult<I, TypeOfCollection<V>>>;
/**
* @see map
*/
collect: UnderscoreStatic["map"];
/**
* Also known as inject and foldl, reduce boils down a `collection` of
* values into a single value. `memo` is the initial state of the
* reduction, and each successive step of it should be returned by
* `iteratee`.
*
* If no memo is passed to the initial invocation of reduce, `iteratee`
* is not invoked on the first element of `collection`. The first
* element is instead passed as the memo in the invocation of
* `iteratee` on the next element in `collection`.
* @param collection The collection to reduce.
* @param iteratee The function to call on each iteration to reduce the
* collection.
* @param memo The initial reduce state or undefined to use the first
* item in `collection` as initial state.
* @param context `this` object in `iteratee`, optional.
* @returns The reduced result.
*/
reduce<V extends Collection<any>, TResult>(
collection: V,
iteratee: MemoCollectionIterator<TypeOfCollection<V>, TResult, V>,
memo: TResult,
context?: any,
): TResult;
reduce<V extends Collection<any>, TResult = TypeOfCollection<V>>(
collection: V,
iteratee: MemoCollectionIterator<TypeOfCollection<V>, TResult | TypeOfCollection<V>, V>,
): TResult | TypeOfCollection<V> | undefined;
/**
* @see reduce
*/
inject: UnderscoreStatic["reduce"];
/**
* @see reduce
*/
foldl: UnderscoreStatic["reduce"];
/**
* The right-associative version of reduce.
*
* This is not as useful in JavaScript as it would be in a language
* with lazy evaluation.
* @param collection The collection to reduce.
* @param iteratee The function to call on each iteration to reduce the
* collection.
* @param memo The initial reduce state or undefined to use the first
* item in `collection` as the initial state.
* @param context `this` object in `iteratee`, optional.
* @returns The reduced result.
*/
reduceRight<V extends Collection<any>, TResult>(
collection: V,
iteratee: MemoCollectionIterator<TypeOfCollection<V>, TResult, V>,
memo: TResult,
context?: any,
): TResult;
reduceRight<V extends Collection<any>, TResult = TypeOfCollection<V>>(
collection: V,
iteratee: MemoCollectionIterator<TypeOfCollection<V>, TResult | TypeOfCollection<V>, V>,
): TResult | TypeOfCollection<V> | undefined;
/**
* @see reduceRight
*/
foldr: UnderscoreStatic["reduceRight"];
/**
* Looks through each value in `collection`, returning the first one
* that passes a truth test (`iteratee`), or undefined if no value
* passes the test. The function returns as soon as it finds an
* acceptable element, and doesn't traverse the entire collection.
* @param collection The collection to search.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns The first element in `collection` that passes the truth
* test or undefined if no elements pass.
*/
find<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): TypeOfCollection<V> | undefined;
/**
* @see find
*/
detect: UnderscoreStatic["find"];
/**
* Looks through each value in `collection`, returning an array of
* all the values that pass a truth test (`iteratee`).
* @param collection The collection to filter.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns The set of values that pass the truth test.
*/
filter<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): Array<TypeOfCollection<V>>;
/**
* @see filter
*/
select: UnderscoreStatic["filter"];
/**
* Looks through each value in `collection`, returning an array of all
* the elements that match the key-value pairs listed in `properties`.
* @param collection The collection in which to find elements that
* match `properties`.
* @param properties The properties to check for on the elements within
* `collection`.
* @returns The elements in `collection` that match `properties`.
*/
where<V extends Collection<any>>(
collection: V,
properties: Partial<TypeOfCollection<V>>,
): Array<TypeOfCollection<V>>;
/**
* Looks through `collection` and returns the first value that matches
* all of the key-value pairs listed in `properties`. If no match is
* found, or if list is empty, undefined will be returned.
* @param collection The collection in which to find an element that
* matches `properties`.
* @param properties The properties to check for on the elements within
* `collection`.
* @returns The first element in `collection` that matches `properties`
* or undefined if no match is found.
*/
findWhere<V extends Collection<any>>(
collection: V,
properties: Partial<TypeOfCollection<V>>,
): TypeOfCollection<V> | undefined;
/**
* Returns the values in `collection` without the elements that pass a
* truth test (`iteratee`).
* The opposite of filter.
* @param collection The collection to filter.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns The set of values that fail the truth test.
*/
reject<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): Array<TypeOfCollection<V>>;
/**
* Returns true if all of the values in `collection` pass the
* `iteratee` truth test. Short-circuits and stops traversing
* `collection` if a false element is found.
* @param collection The collection to evaluate.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns True if all elements pass the truth test, otherwise false.
*/
every<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): boolean;
/**
* @see every
*/
all: UnderscoreStatic["every"];
/**
* Returns true if any of the values in `collection` pass the
* `iteratee` truth test. Short-circuits and stops traversing
* `collection` if a true element is found.
* @param collection The collection to evaluate.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns True if any element passed the truth test, otherwise false.
*/
some<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): boolean;
/**
* @see some
*/
any: UnderscoreStatic["some"];
/**
* Returns true if the value is present in `collection`. Uses indexOf
* internally, if `collection` is a List. Use `fromIndex` to start your
* search at a given index.
* @param collection The collection to check for `value`.
* @param value The value to check `collection` for.
* @param fromIndex The index to start searching from, optional,
* default = 0, only used when `collection` is a List.
* @returns True if `value` is present in `collection` after
* `fromIndex`, otherwise false.
*/
contains<V extends Collection<any>>(
collection: V,
value: any,
fromIndex?: number,
): boolean;
/**
* @see contains
*/
include: UnderscoreStatic["contains"];
/**
* @see contains
*/
includes: UnderscoreStatic["contains"];
/**
* Calls the method named by `methodName` on each value in
* `collection`. Any extra arguments passed to invoke will be forwarded
* on to the method invocation.
* @param collection The collection of elements to invoke `methodName`
* on.
* @param methodName The name of the method to call on each element in
* `collection`.
* @param args Additional arguments to pass to method `methodName`.
* @returns An array containing the result of the method call for each
* item in `collection`.
*/
invoke(
list: Collection<any>,
methodName: string,
...args: any[]
): any[];
/**
* A convenient version of what is perhaps the most common use-case for
* map: extracting a list of property values.
* @param collection The collection of items.
* @param propertyName The name of a specific property to retrieve from
* all items in `collection`.
* @returns The set of values for the specified `propertyName` for each
* item in `collection`.
*/
pluck<V extends Collection<any>, K extends string | number>(
collection: V,
propertyName: K,
): Array<PropertyTypeOrAny<TypeOfCollection<V>, K>>;
/**
* Returns the maximum value in `collection`. If an `iteratee` is
* provided, it will be used on each element to generate the criterion
* by which the element is ranked. -Infinity is returned if list is
* empty. Non-numerical values returned by `iteratee` will be ignored.
* @param collection The collection in which to find the maximum value.
* @param iteratee The iteratee that provides the criterion by which
* each element is ranked, optional if evaluating a collection of
* numbers.
* @param context `this` object in `iteratee`, optional.
* @returns The maximum element within `collection` or -Infinity if
* `collection` is empty.
*/
max<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, any>,
context?: any,
): TypeOfCollection<V> | number;
/**
* Returns the minimum value in `collection`. If an `iteratee` is
* provided, it will be used on each element to generate the criterion
* by which the element is ranked. Infinity is returned if list is
* empty. Non-numerical values returned by `iteratee` will be ignored.
* @param collection The collection in which to find the minimum value.
* @param iteratee The iteratee that provides the criterion by which
* each element is ranked, optional if evaluating a collection of
* numbers.
* @param context `this` object in `iteratee`, optional.
* @returns The minimum element within `collection` or Infinity if
* `collection` is empty.
*/
min<V extends Collection<any>>(
list: V,
iteratee?: Iteratee<V, any>,
context?: any,
): TypeOfCollection<V> | number;
/**
* Returns a (stably) sorted copy of `collection`, ranked in ascending
* order by the results of running each value through `iteratee`.
* @param collection The collection to sort.
* @param iteratee An iteratee that provides the value to sort by for
* each item in `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns A sorted copy of `collection`.
*/
sortBy<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, any>,
context?: any,
): Array<TypeOfCollection<V>>;
/**
* Splits `collection` into sets that are grouped by the result of
* running each value through `iteratee`.
* @param collection The collection to group.
* @param iteratee An iteratee that provides the value to group by for
* each item in `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns A dictionary with the group names provided by `iteratee` as
* properties where each property contains the grouped elements from
* `collection`.
*/
groupBy<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, string | number>,
context?: any,
): Dictionary<Array<TypeOfCollection<V>>>;
/**
* Given a `collection` and an `iteratee` function that returns a key
* for each element in `collection`, returns an object that acts as an
* index of each item. Just like `groupBy`, but for when you know your
* keys are unique.
* @param collection The collection to index.
* @param iteratee An iteratee that provides the value to index by for
* each item in `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns A dictionary where each item in `collection` is assigned to
* the property designated by `iteratee`.
*/
indexBy<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, string | number>,
context?: any,
): Dictionary<TypeOfCollection<V>>;
/**
* Sorts `collection` into groups and returns a count for the number of
* objects in each group. Similar to `groupBy`, but instead of
* returning a list of values, returns a count for the number of values
* in that group.
* @param collection The collection to count.
* @param iteratee An iteratee that provides the value to count by for
* each item in `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns A dictionary with the group names provided by `iteratee` as
* properties where each property contains the count of the grouped
* elements from `collection`.
*/
countBy<V extends Collection<any>>(
collection: V,
iteratee?: Iteratee<V, string | number>,
context?: any,
): Dictionary<number>;
/**
* Returns a shuffled copy of `collection`, using a version of the
* Fisher-Yates shuffle.
* @param collection The collection to shuffle.
* @returns A shuffled copy of `collection`.
*/
shuffle<V extends Collection<any>>(
collection: V,
): Array<TypeOfCollection<V>>;
/**
* Produce a random sample from `collection`. Pass a number to return
* `n` random elements from `collection`. Otherwise a single random
* item will be returned.
* @param collection The collection to sample.
* @param n The number of elements to sample from `collection`.
* @returns A random sample of `n` elements from `collection` or a
* single element if `n` is not specified.
*/
sample<V extends Collection<any>>(
collection: V,
n: number,
): Array<TypeOfCollection<V>>;
sample<V extends Collection<any>>(
collection: V,
): TypeOfCollection<V> | undefined;
/**
* Creates a real Array from `collection` (anything that can be
* iterated over). Useful for transmuting the arguments object.
* @param collection The collection to transform into an array.
* @returns An array containing the elements of `collection`.
*/
toArray<V extends Collection<any>>(
collection: V,
): Array<TypeOfCollection<V>>;
/**
* Determines the number of values in `collection`.
* @param collection The collection to determine the number of values
* for.
* @returns The number of values in `collection`.
*/
size(collection: Collection<any>): number;
/**
* Splits `collection` into two arrays: one whose elements all satisfy
* `iteratee` and one whose elements all do not satisfy `iteratee`.
* @param collection The collection to partition.
* @param iteratee The iteratee that defines the partitioning scheme
* for each element in `collection`.
* @param context `this` object in `iteratee`, optional.
* @returns An array composed of two elements, where the first element
* contains the elements in `collection` that satisfied the predicate
* and the second element contains the elements that did not.
*/
partition<V extends Collection<any>>(
list: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): [Array<TypeOfCollection<V>>, Array<TypeOfCollection<V>>];
/**********
* Arrays *
**********/
/**
* Returns the first element of `list`. Passing `n` will return the
* first `n` elements of `list`.
* @param list The list to retrieve elements from.
* @param n The number of elements to retrieve, optional.
* @returns The first `n` elements of `list` or the first element if
* `n` is omitted.
*/
first<V extends List<any>>(
list: V,
): TypeOfList<V> | undefined;
first<V extends List<any>>(
list: V,
n: number,
): Array<TypeOfList<V>>;
/**
* @see first
*/
head: UnderscoreStatic["first"];
/**
* @see first
*/
take: UnderscoreStatic["first"];
/**
* Returns everything but the last entry of `list`. Especially useful
* on the arguments object. Pass `n` to exclude the last
* `n` elements from the result.
* @param list The list to retrieve elements from.
* @param n The number of elements from the end of `list` to omit,
* optional, default = 1.
* @returns The elements of `list` with the last `n` items omitted.
*/
initial<V extends List<any>>(
list: V,
n?: number,
): Array<TypeOfList<V>>;
/**
* Returns the last element of `list`. Passing `n` will return the last
* `n` elements of `list`.
* @param list The list to retrieve elements from.
* @param n The number of elements to retrieve, optional.
* @returns The last `n` elements of `list` or the last element if `n`
* is omitted.
*/
last<V extends List<any>>(
list: V,
): TypeOfList<V> | undefined;
last<V extends List<any>>(
list: V,
n: number,
): Array<TypeOfList<V>>;
/**
* Returns the rest of the elements in `list`. Pass an `index` to
* return the values of the list from that index onward.
* @param list The list to retrieve elements from.
* @param index The index to start retrieving elements from, optional,
* default = 1.
* @returns The elements of `list` from `index` to the end of the list.
*/
rest<V extends List<any>>(
list: V,
index?: number,
): Array<TypeOfList<V>>;
/**
* @see rest
*/
tail: UnderscoreStatic["rest"];
/**
* @see rest
*/
drop: UnderscoreStatic["rest"];
/**
* Returns a copy of `list` with all falsy values removed. In
* JavaScript, false, null, 0, "", undefined and NaN are all falsy.
* @param list The list to compact.
* @returns An array containing the elements of `list` without falsy
* values.
*/
compact<V extends List<any> | null | undefined>(
list: V,
): Array<Truthy<TypeOfList<V>>>;
/**
* Flattens a nested `list` (the nesting can be to any depth). If you
* pass true or 1 as the depth, the `list` will only be flattened a
* single level. Passing a greater number will cause the flattening to
* descend deeper into the nesting hierarchy. Omitting the depth
* argument, or passing false or Infinity, flattens the `list` all the
* way to the deepest nesting level.
* @param list The list to flatten.
* @param depth True to only flatten one level, optional,
* default = false.
* @returns The flattened `list`.
*/
flatten<V extends List<any>>(
list: V,
depth: 1 | true,
): Array<ListItemOrSelf<TypeOfList<V>>>;
flatten<V extends List<any>>(
list: V,
depth?: number | false,
): Array<DeepestListItemOrSelf<TypeOfList<V>>>;
/**
* Returns a copy of `list` with all instances of `values` removed.
* @param list The list to exclude `values` from.
* @param values The values to exclude from `list`.
* @returns An array that contains all elements of `list` except for
* `values`.
*/
without<V extends List<any>>(
list: V,
...values: Array<TypeOfList<V>>
): Array<TypeOfList<V>>;
/**
* Computes the union of the passed-in `lists`: the list of unique
* items, examined in order from first list to last list, that are
* present in one or more of the lists.
* @param lists The lists to compute the union of.
* @returns The union of elements within `lists`.
*/
union<T>(...lists: Array<List<T>>): T[];
/**
* Computes the list of values that are the intersection of the
* passed-in `lists`. Each value in the result is present in each of
* the lists.
* @param lists The lists to compute the intersection of.
* @returns The intersection of elements within the `lists`.
*/
intersection<T>(...lists: Array<List<T>>): T[];
/**
* Similar to without, but returns the values from `list` that are not
* present in `others`.
* @param list The starting list.
* @param others The lists of values to exclude from `list`.
* @returns The contents of `list` without the values in `others`.
*/
difference<T>(
list: List<T>,
...others: Array<List<T>>
): T[];
/**
* Produces a duplicate-free version of `list`, using === to test
* object equality. If you know in advance that `list` is sorted,
* passing true for isSorted will run a much faster algorithm. If you
* want to compute unique items based on a transformation, pass an
* iteratee function.
* @param list The list to remove duplicates from.
* @param isSorted True if `list` is already sorted, optional,
* default = false.
* @param iteratee Transform the elements of `list` before comparisons
* for uniqueness.
* @param context 'this' object in `iteratee`, optional.
* @returns An array containing only the unique elements in `list`.
*/
uniq<V extends List<any>>(
list: V,
isSorted?: boolean,
iteratee?: Iteratee<V, any>,
context?: any,
): Array<TypeOfList<V>>;
uniq<V extends List<any>>(
list: V,
iteratee?: Iteratee<V, any>,
context?: any,
): Array<TypeOfList<V>>;
/**
* @see uniq
*/
unique: UnderscoreStatic["uniq"];
/**
* Merges together the values of each of the `lists` with the values at
* the corresponding position. Useful when you have separate data
* sources that are coordinated through matching list indexes.
* @param lists The lists to zip.
* @returns The zipped version of `lists`.
*/
zip(): [];
zip<T, U, V>(...lists: [List<T>, List<U>, List<V>]): Array<[T, U, V]>;
zip<T, U>(...lists: [List<T>, List<U>]): Array<[T, U]>;
zip<T>(...lists: [List<T>]): Array<[T]>; // eslint-disable-line @definitelytyped/no-single-element-tuple-type
zip<T>(...lists: Array<List<T>>): T[][];
zip(...lists: Array<List<any>>): any[][];
/**
* The opposite of zip. Given a list of lists, returns a series of new
* arrays, the first of which contains all of the first elements in the
* input lists, the second of which contains all of the second
* elements, and so on. (alias: transpose)
* @param lists The lists to unzip.
* @returns The unzipped version of `lists`.
*/
unzip<T, U, V>(lists: List<[T, U, V]>): [T[], U[], V[]];
unzip<T, U>(lists: List<[T, U]>): [T[], U[]];
unzip<T>(lists: List<[T]>): [T[]]; // eslint-disable-line @definitelytyped/no-single-element-tuple-type
unzip<T>(lists: List<List<T>>): T[][];
unzip(lists: List<List<any>>): any[][];
unzip(): [];
transpose<T, U, V>(lists: List<[T, U, V]>): [T[], U[], V[]];
transpose<T, U>(lists: List<[T, U]>): [T[], U[]];
transpose<T>(lists: List<[T]>): [T[]]; // eslint-disable-line @definitelytyped/no-single-element-tuple-type
transpose<T>(lists: List<List<T>>): T[][];
transpose(lists: List<List<any>>): any[][];
transpose(): [];
/**
* Converts lists into objects. Pass either a single `list` of
* [key, value] pairs, or a `list` of keys and a list of `values`.
* Passing by pairs is the reverse of pairs. If duplicate keys exist,
* the last value wins.
* @param list A list of [key, value] pairs or a list of keys.
* @param values If `list` is a list of keys, a list of values
* corresponding to those keys.
* @returns An object comprised of the provided keys and values.
*/
object<TList extends List<string | number>, TValue>(
list: TList,
values: List<TValue>,
): Dictionary<TValue | undefined>;
object<TList extends List<List<any>>>(
list: TList,
): Dictionary<PairValue<TypeOfList<TList>>>;
/**
* Returns the index at which `value` can be found in `list`, or -1 if
* `value` is not present. If you're working with a large list and you
* know that the list is already sorted, pass true for
* `isSortedOrFromIndex` to use a faster binary search...or, pass a
* number in order to look for the first matching value in the list
* after the given index.
* @param list The list to search for the index of `value`.
* @param value The value to search for within `list`.
* @param isSortedOrFromIndex True if `list` is already sorted OR the
* starting index for the search, optional.
* @returns The index of the first occurrence of `value` within `list`
* or -1 if `value` is not found.
*/
indexOf<V extends List<any>>(
list: V,
value: TypeOfList<V>,
isSortedOrFromIndex?: boolean | number,
): number;
/**
* Returns the index of the last occurrence of `value` in `list`, or -1
* if `value` is not present. Pass `fromIndex` to start your search at
* a given index.
* @param list The list to search for the last occurrence of `value`.
* @param value The value to search for within `list`.
* @param fromIndex The starting index for the search, optional.
* @returns The index of the last occurrence of `value` within `list`
* or -1 if `value` is not found.
*/
lastIndexOf<V extends List<any>>(
list: V,
value: TypeOfList<V>,
fromIndex?: number,
): number;
/**
* Returns the first index of an element in `list` where the `iteratee`
* truth test passes, otherwise returns -1.
* @param list The list to search for the index of the first element
* that passes the truth test.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns The index of the first element in `list` where the
* truth test passes or -1 if no elements pass.
*/
findIndex<V extends List<any>>(
list: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): number;
/**
* Returns the last index of an element in `list` where the `iteratee`
* truth test passes, otherwise returns -1.
* @param list The list to search for the index of the last element
* that passes the truth test.
* @param iteratee The truth test to apply.
* @param context `this` object in `iteratee`, optional.
* @returns The index of the last element in `list` where the
* truth test passes or -1 if no elements pass.
*/
findLastIndex<V extends List<any>>(
list: V,
iteratee?: Iteratee<V, boolean>,
context?: any,
): number;
/**
* Uses a binary search to determine the lowest index at which the
* value should be inserted into `list` in order to maintain `list`'s
* sorted order. If an iteratee is provided, it will be used to compute
* the sort ranking of each value, including the value you pass.
* @param list A sorted list.
* @param value The value to determine an insert index for to mainain
* the sorting in `list`.
* @param iteratee Iteratee to compute the sort ranking of each
* element including `value`, optional.
* @param context `this` object in `iteratee`, optional.
* @returns The index where `value` should be inserted into `list`.
*/
sortedIndex<V extends List<any>>(
list: V,
value: TypeOfList<V>,
iteratee?: Iteratee<V | undefined, any>,
context?: any,
): number;
/**
* A function to create flexibly-numbered lists of integers, handy for
* `each` and `map` loops. Returns a list of integers from
* `startOrStop` (inclusive) to `stop` (exclusive), incremented
* (or decremented) by `step`. Note that ranges that `stop` before they
* `start` are considered to be zero-length instead of negative - if
* you'd like a negative range, use a negative `step`.
*
* If `stop` is not specified, `startOrStop` will be the number to stop
* at and the default start of 0 will be used.
* @param startOrStop If `stop` is specified, the number to start at.
* Otherwise, this is the number to stop at and the default start of 0
* will be used.
* @param stop The number to stop at.
* @param step The number to count up by each iteration, optional,
* default = 1.
* @returns An array of numbers from start to `stop` with increments
* of `step`.
*/
range(
startOrStop: number,
stop?: number,
step?: number,
): number[];
/**
* Chunks `list` into multiple arrays, each containing `length` or
* fewer items.
* @param list The list to chunk.
* @param length The maximum size of the chunks.
* @returns The contents of `list` in chunks no greater than `length`
* in size.
*/
chunk<V extends List<any>>(list: V, length: number): Array<Array<TypeOfList<V>>>;
/*************
* Functions *
*************/
/**
* Bind a function to an object, meaning that whenever the function is called, the value of this will
* be the object. Optionally, bind arguments to the function to pre-fill them, also known as partial application.
* @param func The function to bind `this` to `object`.
* @param context The `this` pointer whenever `fn` is called.
* @param arguments Additional arguments to pass to `fn` when called.
* @return `fn` with `this` bound to `object`.
*/
bind(
func: Function,
context: any,
...args: any[]
): () => any;
/**
* Binds a number of methods on the object, specified by methodNames, to be run in the context of that object
* whenever they are invoked. Very handy for binding functions that are going to be used as event handlers,
* which would otherwise be invoked with a fairly useless this. If no methodNames are provided, all of the
* object's function properties will be bound to it.
* @param object The object to bind the methods `methodName` to.
* @param methodNames The methods to bind to `object`, optional and if not provided all of `object`'s
* methods are bound.
*/
bindAll(
object: any,
...methodNames: string[]
): any;
/**
* Partially apply a function by filling in any number of its arguments, without changing its dynamic this value.
* A close cousin of bind. You may pass _ in your list of arguments to specify an argument that should not be
* pre-filled, but left open to supply at call-time.
* @param fn Function to partially fill in arguments.
* @param arguments The partial arguments.
* @return `fn` with partially filled in arguments.
*/
partial<T1, T2>(
fn: { (p1: T1): T2 },
p1: T1,
): { (): T2 };
partial<T1, T2, T3>(
fn: { (p1: T1, p2: T2): T3 },
p1: T1,
): { (p2: T2): T3 };
partial<T1, T2, T3>(
fn: { (p1: T1, p2: T2): T3 },
p1: T1,
p2: T2,
): { (): T3 };
partial<T1, T2, T3>(
fn: { (p1: T1, p2: T2): T3 },
stub1: UnderscoreStatic,
p2: T2,
): { (p1: T1): T3 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
p1: T1,
): { (p2: T2, p3: T3): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
p1: T1,
p2: T2,
): { (p3: T3): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
stub1: UnderscoreStatic,
p2: T2,
): { (p1: T1, p3: T3): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
p1: T1,
p2: T2,
p3: T3,
): { (): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
stub1: UnderscoreStatic,
p2: T2,
p3: T3,
): { (p1: T1): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
p1: T1,
stub2: UnderscoreStatic,
p3: T3,
): { (p2: T2): T4 };
partial<T1, T2, T3, T4>(
fn: { (p1: T1, p2: T2, p3: T3): T4 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,
p3: T3,
): { (p1: T1, p2: T2): T4 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
): { (p2: T2, p3: T3, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
p2: T2,
): { (p3: T3, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
p2: T2,
): { (p1: T1, p3: T3, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
p2: T2,
p3: T3,
): { (p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
p2: T2,
p3: T3,
): { (p1: T1, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
stub2: UnderscoreStatic,
p3: T3,
): { (p2: T2, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,
p3: T3,
): { (p1: T1, p2: T2, p4: T4): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
p2: T2,
p3: T3,
p4: T4,
): { (): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
p2: T2,
p3: T3,
p4: T4,
): { (p1: T1): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
stub2: UnderscoreStatic,
p3: T3,
p4: T4,
): { (p2: T2): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,
p3: T3,
p4: T4,
): { (p1: T1, p2: T2): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
p2: T2,
stub3: UnderscoreStatic,
p4: T4,
): { (p3: T3): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
p2: T2,
stub3: UnderscoreStatic,
p4: T4,
): { (p1: T1, p3: T3): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
p1: T1,
stub2: UnderscoreStatic,
stub3: UnderscoreStatic,
p4: T4,
): { (p2: T2, p3: T3): T5 };
partial<T1, T2, T3, T4, T5>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4): T5 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,
stub3: UnderscoreStatic,
p4: T4,
): { (p1: T1, p2: T2, p3: T3): T5 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
): { (p2: T2, p3: T3, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
p2: T2,
): { (p3: T3, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
stub1: UnderscoreStatic,
p2: T2,
): { (p1: T1, p3: T3, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
p2: T2,
p3: T3,
): { (p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
stub1: UnderscoreStatic,
p2: T2,
p3: T3,
): { (p1: T1, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
stub2: UnderscoreStatic,
p3: T3,
): { (p2: T2, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,
p3: T3,
): { (p1: T1, p2: T2, p4: T4, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
p2: T2,
p3: T3,
p4: T4,
): { (p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
stub1: UnderscoreStatic,
p2: T2,
p3: T3,
p4: T4,
): { (p1: T1, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
p1: T1,
stub2: UnderscoreStatic,
p3: T3,
p4: T4,
): { (p2: T2, p5: T5): T6 };
partial<T1, T2, T3, T4, T5, T6>(
fn: { (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5): T6 },
stub1: UnderscoreStatic,
stub2: UnderscoreStatic,