From d4f48d6f596518a9e60c7f1b74ef7edc7eb7c58e Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Fri, 25 Sep 2015 14:20:45 +0500 Subject: [PATCH] lodash: changed _.map() method (alias _.collect()) --- lodash/lodash-tests.ts | 108 +++++++++++++-- lodash/lodash.d.ts | 297 ++++++++++++++++++++++++++--------------- 2 files changed, 287 insertions(+), 118 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 8bc80cb683..b7fb7bcc6a 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -652,6 +652,51 @@ result = _([1, 2]).zipWith([1, 2], [1, 2], [1, 2], [1, 2], [1, result = _(testAtDictionary).at(0, '1', [2], ['3'], [4, '5']).value(); } +// _.collect +module TestCollect { + let array: number[]; + let list: _.List; + let dictionary: _.Dictionary; + let listIterator: {(value: number, index: number, collection: _.List): TResult}; + let dictionaryIterator: {(value: number, key: string, collection: _.Dictionary): TResult}; + { + let result: TResult[]; + result = _.collect(array); + result = _.collect(array, listIterator); + result = _.collect(array, listIterator, any); + result = _.collect(array, ''); + result = _.collect(list); + result = _.collect(list, listIterator); + result = _.collect(list, listIterator, any); + result = _.collect(list, ''); + result = _.collect(dictionary); + result = _.collect(dictionary, dictionaryIterator); + result = _.collect(dictionary, dictionaryIterator, any); + result = _.collect(dictionary, ''); + result = _(array).collect().value(); + result = _(array).collect(listIterator).value(); + result = _(array).collect(listIterator, any).value(); + result = _(array).collect('').value(); + result = _(list).collect().value(); + result = _(list).collect(listIterator).value(); + result = _(list).collect(listIterator, any).value(); + result = _(list).collect('').value(); + result = _(dictionary).collect().value(); + result = _(dictionary).collect(dictionaryIterator).value(); + result = _(dictionary).collect(dictionaryIterator, any).value(); + result = _(dictionary).collect('').value(); + } + { + let result: boolean[]; + result = _.collect(array, {}); + result = _.collect(list, {}); + result = _.collect(dictionary, {}); + result = _(array).collect<{}>({}).value(); + result = _(list).collect<{}>({}).value(); + result = _(dictionary).collect<{}>({}).value(); + } +} + result = _.contains([1, 2, 3], 1); result = _.contains([1, 2, 3], 1, 2); result = _.contains({ 'moe': 30, 'larry': 40, 'curly': 67 }, 40); @@ -797,21 +842,60 @@ result = <_.Dictionary>_.indexBy(keys, function (key) { this.fromCharCode( result = _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); result = _.invoke([123, 456], String.prototype.split, ''); -result = _.map([1, 2, 3], function (num) { return num * 3; }); -result = _.map({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { return num * 3; }); -result = _.map(stoogesAges, 'name'); +// _.map +module TestMap { + let array: number[]; + let list: _.List; + let dictionary: _.Dictionary; -result = _([1, 2, 3]).map(function (num) { return num * 3; }).value(); -result = _({ 'one': 1, 'two': 2, 'three': 3 }).map(function (num: number) { return num * 3; }).value(); -result = _(stoogesAges).map('name').value(); + let listIterator: (value: number, index: number, collection: _.List) => TResult; + let dictionaryIterator: (value: number, key: string, collection: _.Dictionary) => TResult; -result = _.collect([1, 2, 3], function (num) { return num * 3; }); -result = _.collect({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { return num * 3; }); -result = _.collect(stoogesAges, 'name'); + { + let result: TResult[]; -result = _([1, 2, 3]).collect(function (num) { return num * 3; }).value(); -result = _({ 'one': 1, 'two': 2, 'three': 3 }).collect(function (num: number) { return num * 3; }).value(); -result = _(stoogesAges).collect('name').value(); + result = _.map(array); + result = _.map(array, listIterator); + result = _.map(array, listIterator, any); + result = _.map(array, ''); + + result = _.map(list); + result = _.map(list, listIterator); + result = _.map(list, listIterator, any); + result = _.map(list, ''); + + result = _.map(dictionary); + result = _.map(dictionary, dictionaryIterator); + result = _.map(dictionary, dictionaryIterator, any); + result = _.map(dictionary, ''); + + result = _(array).map().value(); + result = _(array).map(listIterator).value(); + result = _(array).map(listIterator, any).value(); + result = _(array).map('').value(); + + result = _(list).map().value(); + result = _(list).map(listIterator).value(); + result = _(list).map(listIterator, any).value(); + result = _(list).map('').value(); + + result = _(dictionary).map().value(); + result = _(dictionary).map(dictionaryIterator).value(); + result = _(dictionary).map(dictionaryIterator, any).value(); + result = _(dictionary).map('').value(); + } + { + let result: boolean[]; + + result = _.map(array, {}); + result = _.map(list, {}); + result = _.map(dictionary, {}); + + result = _(array).map<{}>({}).value(); + result = _(list).map<{}>({}).value(); + result = _(dictionary).map<{}>({}).value(); + } +} // _.ceil result = _.ceil(4.006); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 172a250506..23175e5363 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -2172,6 +2172,107 @@ declare module _ { at(...props: Array>): LoDashArrayWrapper; } + //_.collect + interface LoDashStatic { + /** + * @see _.map + */ + collect( + collection: List, + iteratee?: ListIterator, + thisArg?: any + ): TResult[]; + + /** + * @see _.map + */ + collect( + collection: Dictionary, + iteratee?: DictionaryIterator, + thisArg?: any + ): TResult[]; + + /** + * @see _.map + */ + collect( + collection: List, + iteratee?: string + ): TResult[]; + + /** + * @see _.map + */ + collect( + collection: Dictionary, + iteratee?: string + ): TResult[]; + + /** + * @see _.map + */ + collect( + collection: List, + iteratee?: TObject + ): boolean[]; + + /** + * @see _.map + */ + collect( + collection: Dictionary, + iteratee?: TObject + ): boolean[]; + } + + interface LoDashArrayWrapper { + /** + * @see _.map + */ + collect( + iteratee?: ListIterator, + thisArg?: any + ): LoDashArrayWrapper; + + /** + * @see _.map + */ + collect( + iteratee?: string + ): LoDashArrayWrapper; + + /** + * @see _.map + */ + collect( + iteratee?: TObject + ): LoDashArrayWrapper; + } + + interface LoDashObjectWrapper { + /** + * @see _.map + */ + collect( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashArrayWrapper; + + /** + * @see _.map + */ + collect( + iteratee?: string + ): LoDashArrayWrapper; + + /** + * @see _.map + */ + collect( + iteratee?: TObject + ): LoDashArrayWrapper; + } + //_.contains interface LoDashStatic { /** @@ -3632,143 +3733,127 @@ declare module _ { //_.map interface LoDashStatic { /** - * Creates an array of values by running each element in the collection through the callback. - * 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 theArg The this binding of callback. - * @return The mapped array result. - **/ - map( - collection: Array, - callback: ListIterator, - thisArg?: any): TResult[]; - - /** - * @see _.map - **/ + * Creates an array of values by running each element in collection through iteratee. The iteratee 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. + * + * Many lodash methods are guarded to work as iteratees for methods like _.every, _.filter, _.map, _.mapValues, + * _.reject, and _.some. + * + * The guarded methods are: + * ary, callback, chunk, clone, create, curry, curryRight, drop, dropRight, every, fill, flatten, invert, max, + * min, parseInt, slice, sortBy, take, takeRight, template, trim, trimLeft, trimRight, trunc, random, range, + * sample, some, sum, uniq, and words + * + * @alias _.collect + * + * @param collection The collection to iterate over. + * @param iteratee The function invoked per iteration. + * @param thisArg The this binding of iteratee. + * @return Returns the new mapped array. + */ map( collection: List, - callback: ListIterator, - thisArg?: any): TResult[]; + iteratee?: ListIterator, + thisArg?: any + ): TResult[]; /** - * @see _.map - * @param object The object to iterate over. - * @param callback The function called per iteration. - * @param thisArg `this` object in `iterator`, optional. - * @return The mapped object result. - **/ + * @see _.map + */ map( - object: Dictionary, - callback: DictionaryIterator, - thisArg?: any): TResult[]; + collection: Dictionary, + iteratee?: DictionaryIterator, + thisArg?: any + ): TResult[]; /** - * @see _.map - * @param pluckValue _.pluck style callback - **/ - map( - collection: Array, - pluckValue: string): TResult[]; - - /** - * @see _.map - * @param pluckValue _.pluck style callback - **/ + * @see _.map + */ map( collection: List, - pluckValue: string): TResult[]; + iteratee?: string + ): TResult[]; /** - * @see _.map - **/ - collect( - collection: Array, - callback: ListIterator, - thisArg?: any): TResult[]; + * @see _.map + */ + map( + collection: Dictionary, + iteratee?: string + ): TResult[]; /** - * @see _.map - **/ - collect( + * @see _.map + */ + map( collection: List, - callback: ListIterator, - thisArg?: any): TResult[]; + iteratee?: TObject + ): boolean[]; /** - * @see _.map - **/ - collect( - object: Dictionary, - callback: DictionaryIterator, - thisArg?: any): TResult[]; - - /** - * @see _.map - **/ - collect( - collection: Array, - pluckValue: string): TResult[]; - - /** - * @see _.map - **/ - collect( - collection: List, - pluckValue: string): TResult[]; + * @see _.map + */ + map( + collection: Dictionary, + iteratee?: TObject + ): boolean[]; } interface LoDashArrayWrapper { /** - * @see _.map - **/ + * @see _.map + */ map( - callback: ListIterator, - thisArg?: any): LoDashArrayWrapper; + iteratee?: ListIterator, + thisArg?: any + ): LoDashArrayWrapper; /** - * @see _.map - * @param pluckValue _.pluck style callback - **/ + * @see _.map + */ map( - pluckValue: string): LoDashArrayWrapper; + iteratee?: string + ): LoDashArrayWrapper; /** - * @see _.map - **/ - collect( - callback: ListIterator, - thisArg?: any): LoDashArrayWrapper; - - /** - * @see _.map - **/ - collect( - pluckValue: string): LoDashArrayWrapper; + * @see _.map + */ + map( + iteratee?: TObject + ): LoDashArrayWrapper; } interface LoDashObjectWrapper { /** - * @see _.map - **/ - map( - callback: ObjectIterator, - thisArg?: any): LoDashArrayWrapper; + * @see _.map + */ + map( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashArrayWrapper; /** - * @see _.map - **/ - collect( - callback: ObjectIterator, - thisArg?: any): LoDashArrayWrapper; + * @see _.map + */ + map( + iteratee?: string + ): LoDashArrayWrapper; + + /** + * @see _.map + */ + map( + iteratee?: TObject + ): LoDashArrayWrapper; } //_.ceil @@ -8712,7 +8797,7 @@ declare module _ { } interface ListIterator { - (value: T, index: number, collection: T[]): TResult; + (value: T, index: number, collection: List): TResult; } interface DictionaryIterator {