diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index dd3ee3b21..ff3061a1f 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -4390,9 +4390,150 @@ module TestIncludes { } } -result = <_.Dictionary>_.indexBy(keys, 'dir'); -result = <_.Dictionary>_.indexBy(keys, function (key) { return String.fromCharCode(key.code); }); -result = <_.Dictionary>_.indexBy(keys, function (key) { this.fromCharCode(key.code); }, String); +// _.indexBy +module TestIndexBy { + type SampleObject = {a: number; b: string; c: boolean;}; + + let array: SampleObject[]; + let list: _.List; + let dictionary: _.Dictionary; + let numericDictionary: _.NumericDictionary; + + let stringIterator: (value: string, index: number, collection: string) => any; + let listIterator: (value: SampleObject, index: number, collection: _.List) => any; + let dictionaryIterator: (value: SampleObject, key: string, collection: _.Dictionary) => any; + let numericDictionaryIterator: (value: SampleObject, key: number, collection: _.NumericDictionary) => any; + + { + let result: _.Dictionary; + + result = _.indexBy('abcd'); + result = _.indexBy('abcd', stringIterator); + result = _.indexBy('abcd', stringIterator, any); + } + + { + let result: _.Dictionary; + + result = _.indexBy(array); + result = _.indexBy(array, listIterator); + result = _.indexBy(array, listIterator, any); + result = _.indexBy(array, 'a'); + result = _.indexBy(array, 'a', any); + result = _.indexBy<{a: number}, SampleObject>(array, {a: 42}); + result = _.indexBy(array, {a: 42}); + + result = _.indexBy(list); + result = _.indexBy(list, listIterator); + result = _.indexBy(list, listIterator, any); + result = _.indexBy(list, 'a'); + result = _.indexBy(list, 'a', any); + result = _.indexBy<{a: number}, SampleObject>(list, {a: 42}); + result = _.indexBy(list, {a: 42}); + + result = _.indexBy(numericDictionary); + result = _.indexBy(numericDictionary, numericDictionaryIterator); + result = _.indexBy(numericDictionary, numericDictionaryIterator, any); + result = _.indexBy(numericDictionary, 'a'); + result = _.indexBy(numericDictionary, 'a', any); + result = _.indexBy<{a: number}, SampleObject>(numericDictionary, {a: 42}); + result = _.indexBy(numericDictionary, {a: 42}); + + result = _.indexBy(dictionary); + result = _.indexBy(dictionary, dictionaryIterator); + result = _.indexBy(dictionary, dictionaryIterator, any); + result = _.indexBy(dictionary, 'a'); + result = _.indexBy(dictionary, 'a', any); + result = _.indexBy<{a: number}, SampleObject>(dictionary, {a: 42}); + result = _.indexBy(dictionary, {a: 42}); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.Dictionary>; + + result = _('abcd').indexBy(); + result = _('abcd').indexBy(stringIterator); + result = _('abcd').indexBy(stringIterator, any); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.Dictionary>; + + result = _(array).indexBy(); + result = _(array).indexBy(listIterator); + result = _(array).indexBy(listIterator, any); + result = _(array).indexBy('a'); + result = _(array).indexBy('a', any); + result = _(array).indexBy<{a: number}>({a: 42}); + + result = _(list).indexBy(); + result = _(list).indexBy(listIterator); + result = _(list).indexBy(listIterator, any); + result = _(list).indexBy('a'); + result = _(list).indexBy('a', any); + result = _(list).indexBy<{a: number}, SampleObject>({a: 42}); + result = _(list).indexBy({a: 42}); + + result = _(numericDictionary).indexBy(); + result = _(numericDictionary).indexBy(numericDictionaryIterator); + result = _(numericDictionary).indexBy(numericDictionaryIterator, any); + result = _(numericDictionary).indexBy('a'); + result = _(numericDictionary).indexBy('a', any); + result = _(numericDictionary).indexBy<{a: number}, SampleObject>({a: 42}); + result = _(numericDictionary).indexBy({a: 42}); + + result = _(dictionary).indexBy(); + result = _(dictionary).indexBy(dictionaryIterator); + result = _(dictionary).indexBy(dictionaryIterator, any); + result = _(dictionary).indexBy('a'); + result = _(dictionary).indexBy('a', any); + result = _(dictionary).indexBy<{a: number}, SampleObject>({a: 42}); + result = _(dictionary).indexBy({a: 42}); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.Dictionary>; + + result = _('abcd').chain().indexBy(); + result = _('abcd').chain().indexBy(stringIterator); + result = _('abcd').chain().indexBy(stringIterator, any); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.Dictionary>; + + result = _(array).chain().indexBy(); + result = _(array).chain().indexBy(listIterator); + result = _(array).chain().indexBy(listIterator, any); + result = _(array).chain().indexBy('a'); + result = _(array).chain().indexBy('a', any); + result = _(array).chain().indexBy<{a: number}>({a: 42}); + + result = _(list).chain().indexBy(); + result = _(list).chain().indexBy(listIterator); + result = _(list).chain().indexBy(listIterator, any); + result = _(list).chain().indexBy('a'); + result = _(list).chain().indexBy('a', any); + result = _(list).chain().indexBy<{a: number}, SampleObject>({a: 42}); + result = _(list).chain().indexBy({a: 42}); + + result = _(numericDictionary).chain().indexBy(); + result = _(numericDictionary).chain().indexBy(numericDictionaryIterator); + result = _(numericDictionary).chain().indexBy(numericDictionaryIterator, any); + result = _(numericDictionary).chain().indexBy('a'); + result = _(numericDictionary).chain().indexBy('a', any); + result = _(numericDictionary).chain().indexBy<{a: number}, SampleObject>({a: 42}); + result = _(numericDictionary).chain().indexBy({a: 42}); + + result = _(dictionary).chain().indexBy(); + result = _(dictionary).chain().indexBy(dictionaryIterator); + result = _(dictionary).chain().indexBy(dictionaryIterator, any); + result = _(dictionary).chain().indexBy('a'); + result = _(dictionary).chain().indexBy('a', any); + result = _(dictionary).chain().indexBy<{a: number}, SampleObject>({a: 42}); + result = _(dictionary).chain().indexBy({a: 42}); + } +} result = _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); result = _.invoke([123, 456], String.prototype.split, ''); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 24737cb36..7354d0e63 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -6710,65 +6710,207 @@ declare module _ { //_.indexBy interface LoDashStatic { /** - * Creates an object composed of keys generated from the results of running each element - * of the collection through the given callback. The corresponding value of each key is - * the last element responsible for generating the key. The callback is bound to thisArg - * and invoked with three arguments; (value, index|key, collection). - * - * If a property name is provided for callback the created "_.pluck" style callback will - * return the property value of the given element. - * - * If an object is provided for callback the created "_.where" style callback will return - * true for elements that have the properties of the given object, else false. - * @param collection The collection to iterate over. - * @param callback The function called per iteration. - * @param thisArg The this binding of callback. - * @return Returns the composed aggregate object. - **/ - indexBy( - list: Array, - iterator: ListIterator, - context?: any): Dictionary; - - /** - * @see _.indexBy - **/ - indexBy( - list: List, - iterator: ListIterator, - context?: any): Dictionary; - - /** - * @see _.indexBy - * @param pluckValue _.pluck style callback - **/ - indexBy( - collection: Array, - pluckValue: string): Dictionary; - - /** - * @see _.indexBy - * @param pluckValue _.pluck style callback - **/ + * Creates an object composed of keys generated from the results of running each element of collection through + * iteratee. The corresponding value of each key is the last element responsible for generating the key. The + * iteratee function is bound to thisArg and invoked with three arguments: + * (value, index|key, collection). + * + * If a property name is provided for iteratee the created _.property style callback returns the property + * value of the given element. + * + * If a value is also provided for thisArg the created _.matchesProperty style callback returns true for + * elements that have a matching property value, else false. + * + * If an object is provided for iteratee the created _.matches style callback returns true for elements that + * have the properties of the given object, else false. + * + * @param collection The collection to iterate over. + * @param iteratee The function invoked per iteration. + * @param thisArg The this binding of iteratee. + * @return Returns the composed aggregate object. + */ indexBy( collection: List, - pluckValue: string): Dictionary; + iteratee?: ListIterator, + thisArg?: any + ): Dictionary; /** - * @see _.indexBy - * @param whereValue _.where style callback - **/ - indexBy( - collection: Array, - whereValue: W): Dictionary; + * @see _.indexBy + */ + indexBy( + collection: NumericDictionary, + iteratee?: NumericDictionaryIterator, + thisArg?: any + ): Dictionary; /** - * @see _.indexBy - * @param whereValue _.where style callback - **/ - indexBy( - collection: List, - whereValue: W): Dictionary; + * @see _.indexBy + */ + indexBy( + collection: Dictionary, + iteratee?: DictionaryIterator, + thisArg?: any + ): Dictionary; + + /** + * @see _.indexBy + */ + indexBy( + collection: List|NumericDictionary|Dictionary, + iteratee?: string, + thisArg?: any + ): Dictionary; + + /** + * @see _.indexBy + */ + indexBy( + collection: List|NumericDictionary|Dictionary, + iteratee?: W + ): Dictionary; + + /** + * @see _.indexBy + */ + indexBy( + collection: List|NumericDictionary|Dictionary, + iteratee?: Object + ): Dictionary; + } + + interface LoDashImplicitWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator, + thisArg?: any + ): LoDashImplicitObjectWrapper>; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator, + thisArg?: any + ): LoDashImplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: string, + thisArg?: any + ): LoDashImplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: W + ): LoDashImplicitObjectWrapper>; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator|NumericDictionaryIterator|DictionaryIterator, + thisArg?: any + ): LoDashImplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: string, + thisArg?: any + ): LoDashImplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: W + ): LoDashImplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: Object + ): LoDashImplicitObjectWrapper>; + } + + interface LoDashExplicitWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator, + thisArg?: any + ): LoDashExplicitObjectWrapper>; + } + + interface LoDashExplicitArrayWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator, + thisArg?: any + ): LoDashExplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: string, + thisArg?: any + ): LoDashExplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: W + ): LoDashExplicitObjectWrapper>; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.indexBy + */ + indexBy( + iteratee?: ListIterator|NumericDictionaryIterator|DictionaryIterator, + thisArg?: any + ): LoDashExplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: string, + thisArg?: any + ): LoDashExplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: W + ): LoDashExplicitObjectWrapper>; + + /** + * @see _.indexBy + */ + indexBy( + iteratee?: Object + ): LoDashExplicitObjectWrapper>; } //_.invoke