Merge pull request #6023 from chrootsu/lodash-transform

lodash: changed _.transform() method
This commit is contained in:
Masahiro Wakame
2015-09-27 13:42:21 +09:00
2 changed files with 146 additions and 64 deletions
+64 -10
View File
@@ -2028,17 +2028,71 @@ interface TestPickFn {
result = _(testSetObject).set([testSetPath], any).value();
}
result = <number[]>_.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function (r: number[], num: number) {
num *= num;
if (num % 2) {
return r.push(num) < 3;
}
});
// → [1, 9, 25]
// _.transform
module TestTransform {
let array: number[];
let dictionary: _.Dictionary<number>;
result = <{ a: number; b: number; c: number; }>_.transform(<{ [index: string]: number; }>{ 'a': 1, 'b': 2, 'c': 3 }, function (r: any, num: number, key: string) {
r[key] = num * 3;
});
{
let iterator: (acc: TResult[], curr: number, index?: number, arr?: number[]) => void;
let accumulator: TResult[];
let result: TResult[];
result = _.transform<number, TResult>(array);
result = _.transform<number, TResult>(array, iterator);
result = _.transform<number, TResult>(array, iterator, accumulator);
result = _.transform<number, TResult>(array, iterator, accumulator, any);
result = _<number>(array).transform<TResult>().value();
result = _<number>(array).transform<TResult>(iterator).value();
result = _<number>(array).transform<TResult>(iterator, accumulator).value();
result = _<number>(array).transform<TResult>(iterator, accumulator, any).value();
}
{
let iterator: (acc: _.Dictionary<TResult>, curr: number, index?: number, arr?: number[]) => void;
let accumulator: _.Dictionary<TResult>;
let result: _.Dictionary<TResult>;
result = _.transform<number, TResult>(array, iterator);
result = _.transform<number, TResult>(array, iterator, accumulator);
result = _.transform<number, TResult>(array, iterator, accumulator, any);
result = _<number>(array).transform<TResult>(iterator).value();
result = _<number>(array).transform<TResult>(iterator, accumulator).value();
result = _<number>(array).transform<TResult>(iterator, accumulator, any).value();
}
{
let iterator: (acc: _.Dictionary<TResult>, curr: number, key?: string, dict?: _.Dictionary<number>) => void;
let accumulator: _.Dictionary<TResult>;
let result: _.Dictionary<TResult>;
result = _.transform<number, TResult>(dictionary);
result = _.transform<number, TResult>(dictionary, iterator);
result = _.transform<number, TResult>(dictionary, iterator, accumulator);
result = _.transform<number, TResult>(dictionary, iterator, accumulator, any);
result = _(dictionary).transform<number, TResult>().value();
result = _(dictionary).transform<number, TResult>(iterator).value();
result = _(dictionary).transform<number, TResult>(iterator, accumulator).value();
result = _(dictionary).transform<number, TResult>(iterator, accumulator, any).value();
}
{
let iterator: (acc: TResult[], curr: number, key?: string, dict?: _.Dictionary<number>) => void;
let accumulator: TResult[];
let result: TResult[];
result = _.transform<number, TResult>(dictionary, iterator);
result = _.transform<number, TResult>(dictionary, iterator, accumulator);
result = _.transform<number, TResult>(dictionary, iterator, accumulator, any);
result = _(dictionary).transform<number, TResult>(iterator).value();
result = _(dictionary).transform<number, TResult>(iterator, accumulator).value();
result = _(dictionary).transform<number, TResult>(iterator, accumulator, any).value();
}
}
// _.values
class TestValues {
+82 -54
View File
@@ -7728,64 +7728,93 @@ declare module _ {
//_.transform
interface LoDashStatic {
/**
* An alternative to _.reduce this method transforms object to a new accumulator object which is
* the result of running each of its elements through a callback, with each callback execution
* potentially mutating the accumulator object. The callback is bound to thisArg and invoked with
* four arguments; (accumulator, value, key, object). Callbacks may exit iteration early by
* explicitly returning false.
* @param collection The collection to iterate over.
* @param callback The function called per iteration.
* @param accumulator The custom accumulator value.
* @param thisArg The this binding of callback.
* @return The accumulated value.
**/
transform<T, Acc>(
collection: Array<T>,
callback: MemoVoidIterator<T, Acc>,
accumulator: Acc,
thisArg?: any): Acc;
* An alternative to _.reduce; this method transforms object to a new accumulator object which is the result of
* running each of its own enumerable properties through iteratee, with each invocation potentially mutating
* the accumulator object. The iteratee is bound to thisArg and invoked with four arguments: (accumulator,
* value, key, object). Iteratee functions may exit iteration early by explicitly returning false.
*
* @param object The object to iterate over.
* @param iteratee The function invoked per iteration.
* @param accumulator The custom accumulator value.
* @param thisArg The this binding of iteratee.
* @return Returns the accumulated value.
*/
transform<T, TResult>(
object: T[],
iteratee?: MemoVoidArrayIterator<T, TResult[]>,
accumulator?: TResult[],
thisArg?: any
): TResult[];
/**
* @see _.transform
**/
transform<T, Acc>(
collection: List<T>,
callback: MemoVoidIterator<T, Acc>,
accumulator: Acc,
thisArg?: any): Acc;
* @see _.transform
*/
transform<T, TResult>(
object: T[],
iteratee?: MemoVoidArrayIterator<T, Dictionary<TResult>>,
accumulator?: Dictionary<TResult>,
thisArg?: any
): Dictionary<TResult>;
/**
* @see _.transform
**/
transform<T, Acc>(
collection: Dictionary<T>,
callback: MemoVoidIterator<T, Acc>,
accumulator: Acc,
thisArg?: any): Acc;
* @see _.transform
*/
transform<T, TResult>(
object: Dictionary<T>,
iteratee?: MemoVoidDictionaryIterator<T, Dictionary<TResult>>,
accumulator?: Dictionary<TResult>,
thisArg?: any
): Dictionary<TResult>;
/**
* @see _.transform
**/
transform<T, Acc>(
collection: Array<T>,
callback?: MemoVoidIterator<T, Acc>,
thisArg?: any): Acc;
* @see _.transform
*/
transform<T, TResult>(
object: Dictionary<T>,
iteratee?: MemoVoidDictionaryIterator<T, TResult[]>,
accumulator?: TResult[],
thisArg?: any
): TResult[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.transform
*/
transform<TResult>(
iteratee?: MemoVoidArrayIterator<T, TResult[]>,
accumulator?: TResult[],
thisArg?: any
): LoDashArrayWrapper<TResult>;
/**
* @see _.transform
**/
transform<T, Acc>(
collection: List<T>,
callback?: MemoVoidIterator<T, Acc>,
thisArg?: any): Acc;
* @see _.transform
*/
transform<TResult>(
iteratee?: MemoVoidArrayIterator<T, Dictionary<TResult>>,
accumulator?: Dictionary<TResult>,
thisArg?: any
): LoDashObjectWrapper<Dictionary<TResult>>;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.transform
*/
transform<T, TResult>(
iteratee?: MemoVoidDictionaryIterator<T, Dictionary<TResult>>,
accumulator?: Dictionary<TResult>,
thisArg?: any
): LoDashObjectWrapper<Dictionary<TResult>>;
/**
* @see _.transform
**/
transform<T, Acc>(
collection: Dictionary<T>,
callback?: MemoVoidIterator<T, Acc>,
thisArg?: any): Acc;
* @see _.transform
*/
transform<T, TResult>(
iteratee?: MemoVoidDictionaryIterator<T, TResult[]>,
accumulator?: TResult[],
thisArg?: any
): LoDashArrayWrapper<TResult>;
}
//_.values
@@ -8767,14 +8796,13 @@ declare module _ {
interface MemoIterator<T, TResult> {
(prev: TResult, curr: T, indexOrKey?: any, list?: T[]): TResult;
}
/*
interface MemoListIterator<T, TResult> {
(prev: TResult, curr: T, index: number, list?: T[]): TResult;
interface MemoVoidArrayIterator<T, TResult> {
(acc: TResult, curr: T, index?: number, arr?: T[]): void;
}
interface MemoObjectIterator<T, TResult> {
(prev: TResult, curr: T, index: string, object?: Dictionary<T>): TResult;
interface MemoVoidDictionaryIterator<T, TResult> {
(acc: TResult, curr: T, key?: string, dict?: Dictionary<T>): void;
}
*/
//interface Collection<T> {}