diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 470a13a91..1d5525300 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -2028,17 +2028,71 @@ interface TestPickFn { result = _(testSetObject).set([testSetPath], any).value(); } -result = _.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; -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(array); + result = _.transform(array, iterator); + result = _.transform(array, iterator, accumulator); + result = _.transform(array, iterator, accumulator, any); + + result = _(array).transform().value(); + result = _(array).transform(iterator).value(); + result = _(array).transform(iterator, accumulator).value(); + result = _(array).transform(iterator, accumulator, any).value(); + } + + { + let iterator: (acc: _.Dictionary, curr: number, index?: number, arr?: number[]) => void; + let accumulator: _.Dictionary; + let result: _.Dictionary; + + result = _.transform(array, iterator); + result = _.transform(array, iterator, accumulator); + result = _.transform(array, iterator, accumulator, any); + + result = _(array).transform(iterator).value(); + result = _(array).transform(iterator, accumulator).value(); + result = _(array).transform(iterator, accumulator, any).value(); + } + + { + let iterator: (acc: _.Dictionary, curr: number, key?: string, dict?: _.Dictionary) => void; + let accumulator: _.Dictionary; + let result: _.Dictionary; + + result = _.transform(dictionary); + result = _.transform(dictionary, iterator); + result = _.transform(dictionary, iterator, accumulator); + result = _.transform(dictionary, iterator, accumulator, any); + + result = _(dictionary).transform().value(); + result = _(dictionary).transform(iterator).value(); + result = _(dictionary).transform(iterator, accumulator).value(); + result = _(dictionary).transform(iterator, accumulator, any).value(); + } + + { + let iterator: (acc: TResult[], curr: number, key?: string, dict?: _.Dictionary) => void; + let accumulator: TResult[]; + let result: TResult[]; + + result = _.transform(dictionary, iterator); + result = _.transform(dictionary, iterator, accumulator); + result = _.transform(dictionary, iterator, accumulator, any); + + result = _(dictionary).transform(iterator).value(); + result = _(dictionary).transform(iterator, accumulator).value(); + result = _(dictionary).transform(iterator, accumulator, any).value(); + } +} // _.values class TestValues { diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 918a24d35..102811a16 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -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( - collection: Array, - callback: MemoVoidIterator, - 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( + object: T[], + iteratee?: MemoVoidArrayIterator, + accumulator?: TResult[], + thisArg?: any + ): TResult[]; /** - * @see _.transform - **/ - transform( - collection: List, - callback: MemoVoidIterator, - accumulator: Acc, - thisArg?: any): Acc; + * @see _.transform + */ + transform( + object: T[], + iteratee?: MemoVoidArrayIterator>, + accumulator?: Dictionary, + thisArg?: any + ): Dictionary; /** - * @see _.transform - **/ - transform( - collection: Dictionary, - callback: MemoVoidIterator, - accumulator: Acc, - thisArg?: any): Acc; + * @see _.transform + */ + transform( + object: Dictionary, + iteratee?: MemoVoidDictionaryIterator>, + accumulator?: Dictionary, + thisArg?: any + ): Dictionary; /** - * @see _.transform - **/ - transform( - collection: Array, - callback?: MemoVoidIterator, - thisArg?: any): Acc; + * @see _.transform + */ + transform( + object: Dictionary, + iteratee?: MemoVoidDictionaryIterator, + accumulator?: TResult[], + thisArg?: any + ): TResult[]; + } + + interface LoDashArrayWrapper { + /** + * @see _.transform + */ + transform( + iteratee?: MemoVoidArrayIterator, + accumulator?: TResult[], + thisArg?: any + ): LoDashArrayWrapper; /** - * @see _.transform - **/ - transform( - collection: List, - callback?: MemoVoidIterator, - thisArg?: any): Acc; + * @see _.transform + */ + transform( + iteratee?: MemoVoidArrayIterator>, + accumulator?: Dictionary, + thisArg?: any + ): LoDashObjectWrapper>; + } + + interface LoDashObjectWrapper { + /** + * @see _.transform + */ + transform( + iteratee?: MemoVoidDictionaryIterator>, + accumulator?: Dictionary, + thisArg?: any + ): LoDashObjectWrapper>; /** - * @see _.transform - **/ - transform( - collection: Dictionary, - callback?: MemoVoidIterator, - thisArg?: any): Acc; + * @see _.transform + */ + transform( + iteratee?: MemoVoidDictionaryIterator, + accumulator?: TResult[], + thisArg?: any + ): LoDashArrayWrapper; } //_.values @@ -8767,14 +8796,13 @@ declare module _ { interface MemoIterator { (prev: TResult, curr: T, indexOrKey?: any, list?: T[]): TResult; } - /* - interface MemoListIterator { - (prev: TResult, curr: T, index: number, list?: T[]): TResult; + + interface MemoVoidArrayIterator { + (acc: TResult, curr: T, index?: number, arr?: T[]): void; } - interface MemoObjectIterator { - (prev: TResult, curr: T, index: string, object?: Dictionary): TResult; + interface MemoVoidDictionaryIterator { + (acc: TResult, curr: T, key?: string, dict?: Dictionary): void; } - */ //interface Collection {}