From 61608b7a18c5f875e11b67b04665be078fca62df Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Fri, 25 Mar 2016 04:20:47 +0500 Subject: [PATCH] lodash: changed _.flatMap --- lodash/lodash-tests.ts | 191 ++++++++++++++++++++++++++ lodash/lodash.d.ts | 294 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 457 insertions(+), 28 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index eb74bd0bb..30e77d39c 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -3834,6 +3834,197 @@ result = _([1, 2, 3, 4]).findLast(function (num) { result = _(foodsCombined).findLast({ 'type': 'vegetable' }); result = _(foodsCombined).findLast('organic'); +// _.flatMap +namespace TestFlatMap { + let numArray: (number|number[])[] = [1, [2, 3]]; + let objArray: ({a: number}|{a: number}[])[] = [{a: 1}, [{a: 2}, {a: 3}]]; + + let numList: _.List = {0: 1, 1: [2, 3], length: 2}; + let objList: _.List<{a: number}|{a: number}[]> = {0: {a: 1}, 1: [{a: 2}, {a: 3}], length: 2}; + + let numDictionary: _.Dictionary = {a: 1, b: [2, 3]}; + let objDictionary: _.Dictionary<{a: number}|{a: number}[]> = {a: {a: 1}, b: [{a: 2}, {a: 3}]}; + + let numNumericDictionary: _.NumericDictionary = {0: 1, 1: [2, 3]}; + let objNumericDictionary: _.NumericDictionary<{a: number}|{a: number}[]> = {0: {a: 1}, 1: [{a: 2}, {a: 3}]}; + + let stringIterator: (value: string, index: number, collection: _.List) => string|string[]; + + let listIterator: (value: number, index: number, collection: _.List) => number|number[]; + + let dictionaryIterator: (value: number, key: number, collection: _.Dictionary) => number|number[]; + + let numericDictionaryIterator: (value: number, key: number, collection: _.NumericDictionary) => number|number[]; + + { + let result: string[]; + + result = _.flatMap('abc'); + result = _.flatMap('abc'); + + result = _.flatMap('abc', stringIterator); + result = _.flatMap('abc', stringIterator); + } + + { + let result: number[]; + + result = _.flatMap(numArray); + result = _.flatMap(numArray); + + result = _.flatMap(numArray, listIterator); + result = _.flatMap(numArray, listIterator); + + result = _.flatMap<({a: number}|{a: number}[])[], number>(objArray, 'a'); + result = _.flatMap(objArray, 'a'); + + result = _.flatMap(numList); + result = _.flatMap(numList); + + result = _.flatMap(numList, listIterator); + result = _.flatMap(numList, listIterator); + + result = _.flatMap<_.List<{a: number}|{a: number}[]>, number>(objList, 'a'); + result = _.flatMap(objList, 'a'); + + result = _.flatMap(numDictionary); + result = _.flatMap(numDictionary); + + result = _.flatMap(numDictionary, dictionaryIterator); + result = _.flatMap(numDictionary, dictionaryIterator); + + result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, number>(objDictionary, 'a'); + result = _.flatMap(objDictionary, 'a'); + + result = _.flatMap(numNumericDictionary); + result = _.flatMap(numNumericDictionary); + + result = _.flatMap(numNumericDictionary, numericDictionaryIterator); + result = _.flatMap(numNumericDictionary, numericDictionaryIterator); + + result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, number>(objNumericDictionary, 'a'); + result = _.flatMap(objNumericDictionary, 'a'); + } + + { + let result: boolean[]; + + result = _.flatMap<({a: number}|{a: number}[])[], boolean>(objArray, ['a', 42]); + result = _.flatMap(objArray, ['a', 42]); + + result = _.flatMap<{a: number}, ({a: number}|{a: number}[])[]>(objArray, {'a': 42}); + result = _.flatMap<({a: number}|{a: number}[])[], boolean>(objArray, {'a': 42}); + result = _.flatMap(objArray, {'a': 42}); + + result = _.flatMap<_.List<{a: number}|{a: number}[]>, boolean>(objList, ['a', 42]); + result = _.flatMap(objList, ['a', 42]); + + result = _.flatMap<{a: number}, _.List<{a: number}|{a: number}[]>>(objList, {'a': 42}); + result = _.flatMap<_.List<{a: number}|{a: number}[]>, boolean>(objList, {'a': 42}); + result = _.flatMap(objList, {'a': 42}); + + result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, boolean>(objDictionary, ['a', 42]); + result = _.flatMap(objDictionary, ['a', 42]); + + result = _.flatMap<{a: number}, _.Dictionary<{a: number}|{a: number}[]>>(objDictionary, {'a': 42}); + result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, boolean>(objDictionary, {'a': 42}); + result = _.flatMap(objDictionary, {'a': 42}); + + result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, boolean>(objNumericDictionary, ['a', 42]); + result = _.flatMap(objNumericDictionary, ['a', 42]); + + result = _.flatMap<{a: number}, _.NumericDictionary<{a: number}|{a: number}[]>>(objNumericDictionary, {'a': 42}); + result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, boolean>(objNumericDictionary, {'a': 42}); + result = _.flatMap(objNumericDictionary, {'a': 42}); + } + + { + let result: _.LoDashImplicitArrayWrapper; + + result = _('abc').flatMap(); + result = _('abc').flatMap(stringIterator); + } + + { + let result: _.LoDashImplicitArrayWrapper; + + result = _(numArray).flatMap(); + result = _(numArray).flatMap(listIterator); + result = _(objArray).flatMap('a'); + + result = _(numList).flatMap(); + result = _(numList).flatMap(listIterator); + result = _(objList).flatMap('a'); + + result = _(numDictionary).flatMap(); + result = _(numDictionary).flatMap(dictionaryIterator); + result = _(objDictionary).flatMap('a'); + + result = _(numNumericDictionary).flatMap(); + result = _(numNumericDictionary).flatMap(numericDictionaryIterator); + result = _(objNumericDictionary).flatMap('a'); + } + + { + let result: _.LoDashImplicitArrayWrapper; + + result = _(objArray).flatMap(['a', 42]); + result = _(objArray).flatMap<{a: number}>({a: 42}); + + result = _(objList).flatMap(['a', 42]); + result = _(objList).flatMap<{a: number}>({a: 42}); + + result = _(objDictionary).flatMap(['a', 42]); + result = _(objDictionary).flatMap<{a: number}>({a: 42}); + + result = _(objNumericDictionary).flatMap(['a', 42]); + result = _(objNumericDictionary).flatMap<{a: number}>({a: 42}); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + result = _('abc').chain().flatMap(); + result = _('abc').chain().flatMap(stringIterator); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + result = _(numArray).chain().flatMap(); + result = _(numArray).chain().flatMap(listIterator); + result = _(objArray).chain().flatMap('a'); + + result = _(numList).chain().flatMap(); + result = _(numList).chain().flatMap(listIterator); + result = _(objList).chain().flatMap('a'); + + result = _(numDictionary).chain().flatMap(); + result = _(numDictionary).chain().flatMap(dictionaryIterator); + result = _(objDictionary).chain().flatMap('a'); + + result = _(numNumericDictionary).chain().flatMap(); + result = _(numNumericDictionary).chain().flatMap(numericDictionaryIterator); + result = _(objNumericDictionary).chain().flatMap('a'); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + result = _(objArray).chain().flatMap(['a', 42]); + result = _(objArray).chain().flatMap<{a: number}>({a: 42}); + + result = _(objList).chain().flatMap(['a', 42]); + result = _(objList).chain().flatMap<{a: number}>({a: 42}); + + result = _(objDictionary).chain().flatMap(['a', 42]); + result = _(objDictionary).chain().flatMap<{a: number}>({a: 42}); + + result = _(objNumericDictionary).chain().flatMap(['a', 42]); + result = _(objNumericDictionary).chain().flatMap<{a: number}>({a: 42}); + } +} + // _.forEach namespace TestForEach { let array: TResult[]; diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 28c29ad9d..8f0497310 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -1894,34 +1894,6 @@ declare module _ { interface RecursiveArray extends Array> {} interface ListOfRecursiveArraysOrValues extends List> {} - //_.flatMap DUMMY - interface LoDashStatic { - /** - * Creates an array of flattened values by running each element in `array` - * through `iteratee` and concating its result to the other mapped values. - * The iteratee is invoked with three arguments: (value, index|key, array). - * - * @static - * @memberOf _ - * @category Array - * @param {Array} array The array to iterate over. - * @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration. - * @returns {Array} Returns the new array. - * @example - * - * function duplicate(n) { - * return [n, n]; - * } - * - * _.flatMap([1, 2], duplicate); - * // => [1, 1, 2, 2] - */ - flatMap( - array: any[]|List, - ...values: any[] - ): any[]; - } - //_.flatten interface LoDashStatic { /** @@ -7049,6 +7021,272 @@ declare module _ { pluckValue: string): T; } + //_.flatMap + interface LoDashStatic { + /** + * Creates an array of flattened values by running each element in collection through iteratee + * and concating its result to the other mapped values. The iteratee is invoked with three arguments: + * (value, index|key, collection). + * + * @param collection The collection to iterate over. + * @param iteratee The function invoked per iteration. + * @return Returns the new flattened array. + */ + flatMap( + collection: List, + iteratee?: ListIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: List, + iteratee?: ListIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: Dictionary, + iteratee?: DictionaryIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: Dictionary, + iteratee?: DictionaryIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: NumericDictionary, + iteratee?: NumericDictionaryIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: NumericDictionary, + iteratee?: NumericDictionaryIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: TObject, + iteratee?: ObjectIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: Object, + iteratee?: ObjectIterator + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: TObject, + iteratee: TWhere + ): boolean[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: TObject, + iteratee: Object|string + ): TResult[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: TObject, + iteratee: [string, any] + ): boolean[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: string + ): string[]; + + /** + * @see _.flatMap + */ + flatMap( + collection: Object, + iteratee?: Object|string + ): TResult[]; + } + + interface LoDashImplicitWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashImplicitArrayWrapper; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator|string + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: TWhere + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: [string, any] + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashImplicitArrayWrapper; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator|DictionaryIterator|NumericDictionaryIterator + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: ObjectIterator|string + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: TWhere + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: [string, any] + ): LoDashImplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashImplicitArrayWrapper; + } + + interface LoDashExplicitWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashExplicitArrayWrapper; + } + + interface LoDashExplicitArrayWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator|string + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: TWhere + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: [string, any] + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashExplicitArrayWrapper; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.flatMap + */ + flatMap( + iteratee: ListIterator|DictionaryIterator|NumericDictionaryIterator + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: ObjectIterator|string + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: TWhere + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap( + iteratee: [string, any] + ): LoDashExplicitArrayWrapper; + + /** + * @see _.flatMap + */ + flatMap(): LoDashExplicitArrayWrapper; + } + //_.forEach interface LoDashStatic { /**