Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@types/underscore] Collection and Array Tests - Each, ToArray, and Partition #46120

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
69dab4d
Updating type definitions for each, partition, and toArray and adding…
reubenrybnik Jul 15, 2020
f06ef34
Adding a multi-call chain test.
reubenrybnik Jul 16, 2020
190ade1
Fixing some issues around type unions and implicit any errors for eac…
reubenrybnik Jul 16, 2020
7fbaab6
Adding tests around issues fixed by the change to CollectionIterator …
reubenrybnik Jul 16, 2020
3e8d0e7
Adding identity iteratee tests for partition and making the identity …
reubenrybnik Jul 17, 2020
2a60294
Removing IterateePropertyShorthand because the last thing that was us…
reubenrybnik Jul 17, 2020
9355160
Update summary comments to reflect that the iteratee for `each` is al…
reubenrybnik Jul 17, 2020
018f6b2
Moving EnumerableKey and CollectionKey up above the iterators.
reubenrybnik Jul 17, 2020
b94628f
Updating CollectionIterator and MemoCollectionIterator to be interfac…
reubenrybnik Jul 17, 2020
3b624ab
Updating Array<EnumerableKey> to EnumerableKey[] to match other array…
reubenrybnik Jul 17, 2020
08218f8
Adding tests for the more complicated Iteratee and IterateeResult types.
reubenrybnik Jul 17, 2020
c131040
Dropping extra overloads of reduce and reduceRight since getting rid …
reubenrybnik Jul 17, 2020
53d6759
Dropping MemoIterator and MemoObjectIterator since they're no longer …
reubenrybnik Jul 17, 2020
11a4a6c
While I'm cleaning up reduce and reduceRight anyway, also updating th…
reubenrybnik Jul 17, 2020
090cc12
Revert "Dropping MemoIterator and MemoObjectIterator since they're no…
reubenrybnik Jul 17, 2020
9a58d18
Updating non-collection iterator types to be aliases of collection it…
reubenrybnik Jul 17, 2020
d523e62
Putting better constraints on iterators and temporarily reverting bac…
reubenrybnik Jul 17, 2020
8bbe474
Adding missing $ExpectTypes in function Iteratee tests.
reubenrybnik Jul 17, 2020
6ec881b
Adding null to Iteratee and IterateeResult and moving undefined to th…
reubenrybnik Jul 17, 2020
5ef8fe2
Adding null iteratee tests.
reubenrybnik Jul 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
214 changes: 105 additions & 109 deletions types/underscore/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ declare module _ {

type EnumerableKey = string | number;

type CollectionIterator<T, TResult, V> =
V extends List<T> ? ListIterator<T, TResult, V>
: V extends Dictionary<T> ? ObjectIterator<T, TResult, V>
reubenrybnik marked this conversation as resolved.
Show resolved Hide resolved
type CollectionKey<V> = V extends List<any> ? number
: V extends Dictionary<any> ? string
: never;

type CollectionIterator<T, TResult, V> = (element: T, key: CollectionKey<V>, collection: V) => TResult;

type Iteratee<V, R, T extends TypeOfCollection<V> = TypeOfCollection<V>> =
undefined |
CollectionIterator<T, R, V> |
Expand Down Expand Up @@ -179,45 +180,26 @@ declare module _ {
************* */

/**
* Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is
* bound to the context object, if one is passed. Each invocation of iterator is called with three
* arguments: (element, index, list). If list is a JavaScript object, iterator's arguments will be
* (value, key, object). Delegates to the native forEach function if it exists.
* @param list Iterates over this list of elements.
* @param iterator Iterator function for each element `list`.
* @param context 'this' object in `iterator`, optional.
**/
each<T>(
list: _.List<T>,
iterator: _.ListIterator<T, void>,
context?: any): _.List<T>;

/**
* @see _.each
* @param object Iterates over properties of this object.
* @param iterator Iterator function for each property on `object`.
* @param context 'this' object in `iterator`, optional.
**/
each<T>(
object: _.Dictionary<T>,
iterator: _.ObjectIterator<T, void>,
context?: any): _.Dictionary<T>;

/**
* @see _.each
**/
forEach<T>(
list: _.List<T>,
iterator: _.ListIterator<T, void>,
context?: any): _.List<T>;
* 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. If iteratee is a function, Each invocation is called with
* three arguments: (value, key, collection).
reubenrybnik marked this conversation as resolved.
Show resolved Hide resolved
* @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<T>(
object: _.Dictionary<T>,
iterator: _.ObjectIterator<T, void>,
context?: any): _.Dictionary<T>;
* @see each
**/
forEach: UnderscoreStatic['each'];

/**
* Produces a new array of values by mapping each value in the collection through a transformation function
Expand Down Expand Up @@ -714,12 +696,12 @@ declare module _ {
sample<V extends Collection<any>>(collection: V): TypeOfCollection<V> | undefined;

/**
* Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting
* the arguments object.
* @param list object to transform into an array.
* @return `list` as an array.
**/
toArray<T>(list: _.Collection<T>): T[];
* Creates a real Array from the 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): TypeOfCollection<V>[];

/**
* Return the number of values in the list.
Expand All @@ -729,17 +711,21 @@ declare module _ {
size<T>(list: _.Collection<T>): number;

/**
* Split array into two arrays:
* one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.
* @param array Array to split in two.
* @param iterator Filter iterator function for each element in `array`.
* @param context `this` object in `iterator`, optional.
* @return Array where Array[0] are the elements in `array` that satisfies the predicate, and Array[1] the elements that did not.
**/
partition<T>(
array: Array<T>,
iterator: _.ListIterator<T, boolean>,
context?: any): T[][];
* 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>,
reubenrybnik marked this conversation as resolved.
Show resolved Hide resolved
context?: any
): [TypeOfCollection<V>[], TypeOfCollection<V>[]];

/*********
* Arrays *
Expand Down Expand Up @@ -4078,25 +4064,21 @@ declare module _ {
************* */

/**
* Wrapped type `any[]`.
* @see _.each
**/
each(iterator: _.ListIterator<T, void>, context?: any): _.List<T>;

/**
* @see _.each
**/
each(iterator: _.ObjectIterator<T, void>, context?: any): _.List<T>;

/**
* @see _.each
**/
forEach(iterator: _.ListIterator<T, void>, context?: any): _.List<T>;
* Iterates over the wrapped collection of elements, yielding each in
* turn to an iteratee. The iteratee is bound to the context object, if
* one is passed. If iteratee is a function, Each invocation is called
* with three arguments: (value, key, collection).
* @param iteratee The iteratee to call for each element in the wrapped
* collection.
* @param context 'this' object in `iteratee`, optional.
* @returns The originally wrapped collection.
**/
each(iteratee: CollectionIterator<T, void, V>, context?: any): V;

/**
* @see _.each
**/
forEach(iterator: _.ObjectIterator<T, void>, context?: any): _.List<T>;
* @see each
**/
forEach: Underscore<T, V>['each'];

/**
* Produces a new array of values by mapping each value in the wrapped collection through a transformation function
Expand Down Expand Up @@ -4382,9 +4364,10 @@ declare module _ {
sample(): T | undefined;

/**
* Wrapped type `any`.
* @see _.toArray
**/
* Creates a real Array from the wrapped collection (anything that can
* be iterated over). Useful for transmuting the arguments object.
* @returns An array containing the elements of the wrapped collection.
**/
toArray(): T[];

/**
Expand All @@ -4393,6 +4376,19 @@ declare module _ {
**/
size(): number;

/**
* Splits the wrapped collection into two arrays: one whose elements
* all satisfy `iteratee` and one whose elements all do not satisfy
* `iteratee`.
* @param iteratee The iteratee that defines the partitioning scheme
* for each element in the wrapped collection.
* @param context `this` object in `iteratee`, optional.
* @returns An array composed of two elements, where the first element
* contains the elements in the wrapped collection that satisfied the
* predicate and the second element contains the elements that did not.
**/
partition(iteratee: Iteratee<V, boolean>, context?: any): [T[], T[]];

/*********
* Arrays *
**********/
Expand Down Expand Up @@ -4487,12 +4483,6 @@ declare module _ {
**/
without(...values: T[]): T[];

/**
* Wrapped type `any[]`.
* @see _.partition
**/
partition(iterator: _.ListIterator<T, boolean>, context?: any): T[][];

/**
* Wrapped type `any[][]`.
* @see _.union
Expand Down Expand Up @@ -5071,25 +5061,21 @@ declare module _ {
************* */

/**
* Wrapped type `any[]`.
* @see _.each
**/
each(iterator: _.ListIterator<T, void>, context?: any): _Chain<T>;

/**
* @see _.each
**/
each(iterator: _.ObjectIterator<T, void>, context?: any): _Chain<T>;

/**
* @see _.each
**/
forEach(iterator: _.ListIterator<T, void>, context?: any): _Chain<T>;
* Iterates over the wrapped collection of elements, yielding each in
* turn to an iteratee. The iteratee is bound to the context object, if
* one is passed. If iteratee is a function, Each invocation is called
* with three arguments: (value, key, collection).
* @param iteratee The iteratee to call for each element in the wrapped
* collection.
* @param context 'this' object in `iteratee`, optional.
* @returns A chain wrapper around the originally wrapped collection.
**/
each(iteratee: CollectionIterator<T, void, V>, context?: any): _Chain<T, V>;

/**
* @see _.each
* @see each
**/
forEach(iterator: _.ObjectIterator<T, void>, context?: any): _Chain<T>;
forEach: _Chain<T, V>['each'];

/**
* Produces a new array of values by mapping each value in the wrapped collection through a transformation function
Expand Down Expand Up @@ -5378,17 +5364,33 @@ declare module _ {
sample(): _ChainSingle<T | undefined>;

/**
* Wrapped type `any`.
* @see _.toArray
**/
toArray(): _Chain<T>;
* Creates a real Array from the wrapped collection (anything that can
* be iterated over). Useful for transmuting the arguments object.
* @returns A chain wrapper around an array containing the elements
* of the wrapped collection.
**/
toArray(): _Chain<T, T[]>;

/**
* Wrapped type `any`.
* @see _.size
**/
size(): _ChainSingle<number>;

/**
* Splits the wrapped collection into two arrays: one whose elements
* all satisfy `iteratee` and one whose elements all do not satisfy
* `iteratee`.
* @param iteratee The iteratee that defines the partitioning scheme
* for each element in the wrapped collection.
* @param context `this` object in `iteratee`, optional.
* @returns A chain wrapper around an array composed of two elements,
* where the first element contains the elements in the wrapped
* collection that satisfied the predicate and the second element
* contains the elements that did not.
**/
partition(iteratee: _ChainIteratee<V, boolean, T>, context?: any): _Chain<T[], [T[], T[]]>;

/*********
* Arrays *
**********/
Expand Down Expand Up @@ -5483,12 +5485,6 @@ declare module _ {
**/
without(...values: T[]): _Chain<T, T[]>;

/**
* Wrapped type `any[]`.
* @see _.partition
**/
partition(iterator: _.ListIterator<T, boolean>, context?: any): _Chain<T[]>;

/**
* Wrapped type `any[][]`.
* @see _.union
Expand Down