lodash: changed _.flatMap

This commit is contained in:
Ilya Mochalov
2016-03-25 06:09:14 +05:00
parent a44529fcb1
commit 61608b7a18
2 changed files with 457 additions and 28 deletions
+191
View File
@@ -3834,6 +3834,197 @@ result = <number>_([1, 2, 3, 4]).findLast(function (num) {
result = <IFoodCombined>_(foodsCombined).findLast({ 'type': 'vegetable' });
result = <IFoodCombined>_(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<number|number[]> = {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<number|number[]> = {a: 1, b: [2, 3]};
let objDictionary: _.Dictionary<{a: number}|{a: number}[]> = {a: {a: 1}, b: [{a: 2}, {a: 3}]};
let numNumericDictionary: _.NumericDictionary<number|number[]> = {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|string[];
let listIterator: (value: number, index: number, collection: _.List<number|number[]>) => number|number[];
let dictionaryIterator: (value: number, key: number, collection: _.Dictionary<number|number[]>) => number|number[];
let numericDictionaryIterator: (value: number, key: number, collection: _.NumericDictionary<number|number[]>) => number|number[];
{
let result: string[];
result = _.flatMap<string>('abc');
result = _.flatMap<string>('abc');
result = _.flatMap<string, string>('abc', stringIterator);
result = _.flatMap<string>('abc', stringIterator);
}
{
let result: number[];
result = _.flatMap<number|number[], number>(numArray);
result = _.flatMap<number>(numArray);
result = _.flatMap<number|number[], number>(numArray, listIterator);
result = _.flatMap<number>(numArray, listIterator);
result = _.flatMap<({a: number}|{a: number}[])[], number>(objArray, 'a');
result = _.flatMap<number>(objArray, 'a');
result = _.flatMap<number|number[], number>(numList);
result = _.flatMap<number>(numList);
result = _.flatMap<number|number[], number>(numList, listIterator);
result = _.flatMap<number>(numList, listIterator);
result = _.flatMap<_.List<{a: number}|{a: number}[]>, number>(objList, 'a');
result = _.flatMap<number>(objList, 'a');
result = _.flatMap<number|number[], number>(numDictionary);
result = _.flatMap<number>(numDictionary);
result = _.flatMap<number|number[], number>(numDictionary, dictionaryIterator);
result = _.flatMap<number>(numDictionary, dictionaryIterator);
result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, number>(objDictionary, 'a');
result = _.flatMap<number>(objDictionary, 'a');
result = _.flatMap<number|number[], number>(numNumericDictionary);
result = _.flatMap<number>(numNumericDictionary);
result = _.flatMap<number|number[], number>(numNumericDictionary, numericDictionaryIterator);
result = _.flatMap<number>(numNumericDictionary, numericDictionaryIterator);
result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, number>(objNumericDictionary, 'a');
result = _.flatMap<number>(objNumericDictionary, 'a');
}
{
let result: boolean[];
result = _.flatMap<({a: number}|{a: number}[])[], boolean>(objArray, ['a', 42]);
result = _.flatMap<boolean>(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<boolean>(objArray, {'a': 42});
result = _.flatMap<_.List<{a: number}|{a: number}[]>, boolean>(objList, ['a', 42]);
result = _.flatMap<boolean>(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<boolean>(objList, {'a': 42});
result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, boolean>(objDictionary, ['a', 42]);
result = _.flatMap<boolean>(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<boolean>(objDictionary, {'a': 42});
result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, boolean>(objNumericDictionary, ['a', 42]);
result = _.flatMap<boolean>(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<boolean>(objNumericDictionary, {'a': 42});
}
{
let result: _.LoDashImplicitArrayWrapper<string>;
result = _('abc').flatMap();
result = _('abc').flatMap<string>(stringIterator);
}
{
let result: _.LoDashImplicitArrayWrapper<number>;
result = _(numArray).flatMap<number>();
result = _(numArray).flatMap<number>(listIterator);
result = _(objArray).flatMap<number>('a');
result = _(numList).flatMap<number>();
result = _(numList).flatMap<number|number[], number>(listIterator);
result = _(objList).flatMap<number>('a');
result = _(numDictionary).flatMap<number>();
result = _(numDictionary).flatMap<number|number[], number>(dictionaryIterator);
result = _(objDictionary).flatMap<number>('a');
result = _(numNumericDictionary).flatMap<number>();
result = _(numNumericDictionary).flatMap<number|number[], number>(numericDictionaryIterator);
result = _(objNumericDictionary).flatMap<number>('a');
}
{
let result: _.LoDashImplicitArrayWrapper<boolean>;
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<string>;
result = _('abc').chain().flatMap();
result = _('abc').chain().flatMap<string>(stringIterator);
}
{
let result: _.LoDashExplicitArrayWrapper<number>;
result = _(numArray).chain().flatMap<number>();
result = _(numArray).chain().flatMap<number>(listIterator);
result = _(objArray).chain().flatMap<number>('a');
result = _(numList).chain().flatMap<number>();
result = _(numList).chain().flatMap<number|number[], number>(listIterator);
result = _(objList).chain().flatMap<number>('a');
result = _(numDictionary).chain().flatMap<number>();
result = _(numDictionary).chain().flatMap<number|number[], number>(dictionaryIterator);
result = _(objDictionary).chain().flatMap<number>('a');
result = _(numNumericDictionary).chain().flatMap<number>();
result = _(numNumericDictionary).chain().flatMap<number|number[], number>(numericDictionaryIterator);
result = _(objNumericDictionary).chain().flatMap<number>('a');
}
{
let result: _.LoDashExplicitArrayWrapper<boolean>;
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[];
+266 -28
View File
@@ -1894,34 +1894,6 @@ declare module _ {
interface RecursiveArray<T> extends Array<T|RecursiveArray<T>> {}
interface ListOfRecursiveArraysOrValues<T> extends List<T|RecursiveArray<T>> {}
//_.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<any>,
...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<T, TResult>(
collection: List<T>,
iteratee?: ListIterator<T, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: List<any>,
iteratee?: ListIterator<any, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<T, TResult>(
collection: Dictionary<T>,
iteratee?: DictionaryIterator<T, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: Dictionary<any>,
iteratee?: DictionaryIterator<any, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<T, TResult>(
collection: NumericDictionary<T>,
iteratee?: NumericDictionaryIterator<T, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: NumericDictionary<any>,
iteratee?: NumericDictionaryIterator<any, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TObject extends Object, TResult>(
collection: TObject,
iteratee?: ObjectIterator<any, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: Object,
iteratee?: ObjectIterator<any, TResult|TResult[]>
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TWhere extends Object, TObject extends Object>(
collection: TObject,
iteratee: TWhere
): boolean[];
/**
* @see _.flatMap
*/
flatMap<TObject extends Object, TResult>(
collection: TObject,
iteratee: Object|string
): TResult[];
/**
* @see _.flatMap
*/
flatMap<TObject extends Object>(
collection: TObject,
iteratee: [string, any]
): boolean[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: string
): string[];
/**
* @see _.flatMap
*/
flatMap<TResult>(
collection: Object,
iteratee?: Object|string
): TResult[];
}
interface LoDashImplicitWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ListIterator<string, TResult|TResult[]>
): LoDashImplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap(): LoDashImplicitArrayWrapper<string>;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ListIterator<T, TResult|TResult[]>|string
): LoDashImplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TWhere extends Object>(
iteratee: TWhere
): LoDashImplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap(
iteratee: [string, any]
): LoDashImplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap<TResult>(): LoDashImplicitArrayWrapper<TResult>;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<T, TResult>(
iteratee: ListIterator<T, TResult|TResult[]>|DictionaryIterator<T, TResult|TResult[]>|NumericDictionaryIterator<T, TResult|TResult[]>
): LoDashImplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ObjectIterator<any, TResult|TResult[]>|string
): LoDashImplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TWhere extends Object>(
iteratee: TWhere
): LoDashImplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap(
iteratee: [string, any]
): LoDashImplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap<TResult>(): LoDashImplicitArrayWrapper<TResult>;
}
interface LoDashExplicitWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ListIterator<string, TResult|TResult[]>
): LoDashExplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap(): LoDashExplicitArrayWrapper<string>;
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ListIterator<T, TResult|TResult[]>|string
): LoDashExplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TWhere extends Object>(
iteratee: TWhere
): LoDashExplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap(
iteratee: [string, any]
): LoDashExplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap<TResult>(): LoDashExplicitArrayWrapper<TResult>;
}
interface LoDashExplicitObjectWrapper<T> {
/**
* @see _.flatMap
*/
flatMap<T, TResult>(
iteratee: ListIterator<T, TResult|TResult[]>|DictionaryIterator<T, TResult|TResult[]>|NumericDictionaryIterator<T, TResult|TResult[]>
): LoDashExplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TResult>(
iteratee: ObjectIterator<any, TResult|TResult[]>|string
): LoDashExplicitArrayWrapper<TResult>;
/**
* @see _.flatMap
*/
flatMap<TWhere extends Object>(
iteratee: TWhere
): LoDashExplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap(
iteratee: [string, any]
): LoDashExplicitArrayWrapper<boolean>;
/**
* @see _.flatMap
*/
flatMap<TResult>(): LoDashExplicitArrayWrapper<TResult>;
}
//_.forEach
interface LoDashStatic {
/**