From b3833f87dde453ca7b8da3ea865ad4aba70575bb Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Tue, 3 Nov 2015 07:09:19 +0500 Subject: [PATCH] lodash: signatures of the method _.forEach have been changed --- lodash/lodash-tests.ts | 204 ++++++++++++++++++++++++++++--- lodash/lodash.d.ts | 272 ++++++++++++++++++++++++++++------------- 2 files changed, 375 insertions(+), 101 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index f12ca23c42..05a1f9c14b 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -2267,6 +2267,101 @@ module TestDetect { result = _(dictionary).detect<{a: number}, TResult>({a: 42}); } +// _.each +module TestEach { + let array: TResult[]; + let list: _.List; + let dictionary: _.Dictionary; + + let stringIterator: (char: string, index: number, string: string) => boolean|void; + let listIterator: (value: TResult, index: number, collection: _.List) => boolean|void; + let dictionaryIterator: (value: TResult, key: string, collection: _.Dictionary) => boolean|void; + + { + let result: string; + + _.each('', stringIterator); + _.each('', stringIterator, any); + } + + { + let result: TResult[]; + + _.each(array, listIterator); + _.each(array, listIterator, any); + } + + { + let result: _.List; + + _.each(list, listIterator); + _.each(list, listIterator, any); + } + + { + let result: _.Dictionary; + + _.each(dictionary, dictionaryIterator); + _.each(dictionary, dictionaryIterator, any); + } + + { + let result: _.LoDashImplicitWrapper; + + _('').each(stringIterator); + _('').each(stringIterator, any); + } + + { + let result: _.LoDashImplicitArrayWrapper; + + _(array).each(listIterator); + _(array).each(listIterator, any); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.List>; + + _(list).each(listIterator); + _(list).each(listIterator, any); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.Dictionary>; + + _(dictionary).each(dictionaryIterator); + _(dictionary).each(dictionaryIterator, any); + } + + { + let result: _.LoDashExplicitWrapper; + + _('').chain().each(stringIterator); + _('').chain().each(stringIterator, any); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + _(array).chain().each(listIterator); + _(array).chain().each(listIterator, any); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.List>; + + _(list).chain().each(listIterator); + _(list).chain().each(listIterator, any); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.Dictionary>; + + _(dictionary).chain().each(dictionaryIterator); + _(dictionary).chain().each(dictionaryIterator, any); + } +} + // _.every module TestEvery { let array: TResult[]; @@ -2422,19 +2517,100 @@ result = _([1, 2, 3, 4]).findLast(function (num) { result = _(foodsCombined).findLast({ 'type': 'vegetable' }); result = _(foodsCombined).findLast('organic'); -result = _.forEach([1, 2, 3], function (num) { console.log(num); }); -result = <_.Dictionary>_.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { console.log(num); }); -result = _.forEach({ name: 'apple', type: 'fruit' }, function (value, key) { console.log(value, key) }); +// _.forEach +module TestForEach { + let array: TResult[]; + let list: _.List; + let dictionary: _.Dictionary; -result = _.each([1, 2, 3], function (num) { console.log(num); }); -result = <_.Dictionary>_.each({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { console.log(num); }); -result = _.each({ name: 'apple', type: 'fruit' }, function (value, key) { console.log(value, key) }); + let stringIterator: (char: string, index: number, string: string) => boolean|void; + let listIterator: (value: TResult, index: number, collection: _.List) => boolean|void; + let dictionaryIterator: (value: TResult, key: string, collection: _.Dictionary) => boolean|void; -result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3]).forEach(function (num) { console.log(num); }); -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_(<{ [index: string]: number; }>{ 'one': 1, 'two': 2, 'three': 3 }).forEach(function (num) { console.log(num); }); + { + let result: string; -result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3]).each(function (num) { console.log(num); }); -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_(<{ [index: string]: number; }>{ 'one': 1, 'two': 2, 'three': 3 }).each(function (num) { console.log(num); }); + _.forEach('', stringIterator); + _.forEach('', stringIterator, any); + } + + { + let result: TResult[]; + + _.forEach(array, listIterator); + _.forEach(array, listIterator, any); + } + + { + let result: _.List; + + _.forEach(list, listIterator); + _.forEach(list, listIterator, any); + } + + { + let result: _.Dictionary; + + _.forEach(dictionary, dictionaryIterator); + _.forEach(dictionary, dictionaryIterator, any); + } + + { + let result: _.LoDashImplicitWrapper; + + _('').forEach(stringIterator); + _('').forEach(stringIterator, any); + } + + { + let result: _.LoDashImplicitArrayWrapper; + + _(array).forEach(listIterator); + _(array).forEach(listIterator, any); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.List>; + + _(list).forEach(listIterator); + _(list).forEach(listIterator, any); + } + + { + let result: _.LoDashImplicitObjectWrapper<_.Dictionary>; + + _(dictionary).forEach(dictionaryIterator); + _(dictionary).forEach(dictionaryIterator, any); + } + + { + let result: _.LoDashExplicitWrapper; + + _('').chain().forEach(stringIterator); + _('').chain().forEach(stringIterator, any); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + _(array).chain().forEach(listIterator); + _(array).chain().forEach(listIterator, any); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.List>; + + _(list).chain().forEach(listIterator); + _(list).chain().forEach(listIterator, any); + } + + { + let result: _.LoDashExplicitObjectWrapper<_.Dictionary>; + + _(dictionary).chain().forEach(dictionaryIterator); + _(dictionary).chain().forEach(dictionaryIterator, any); + } +} result = _.forEachRight([1, 2, 3], function (num) { console.log(num); }); result = <_.Dictionary>_.forEachRight({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { console.log(num); }); @@ -2788,18 +2964,10 @@ done = _.after(saves.length, function () { console.log('Done saving!'); }); -_.forEach(saves, function (type) { - asyncSave({ 'type': type, 'complete': done }); -}); - done = _(saves.length).after(function () { console.log('Done saving!'); }).value(); -_.forEach(saves, function (type) { - asyncSave({ 'type': type, 'complete': done }); -}); - // _.ary result = ['6', '8', '10'].map(_.ary<(s: string) => number>(parseInt, 1)); result = ['6', '8', '10'].map(_(parseInt).ary<(s: string) => number>(1).value()); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 141ad993cd..1d92b9df11 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -3804,6 +3804,105 @@ declare module _ { ): TResult; } + //_.each + interface LoDashStatic { + /** + * @see _.forEach + */ + each( + collection: string, + iteratee?: StringIterator, + thisArg?: any + ): string; + + /** + * @see _.forEach + */ + each( + collection: T[], + iteratee?: ListIterator, + thisArg?: any + ): T[]; + + /** + * @see _.forEach + */ + each( + collection: List, + iteratee?: ListIterator, + thisArg?: any + ): List; + + /** + * @see _.forEach + */ + each( + collection: Dictionary, + iteratee?: DictionaryIterator, + thisArg?: any + ): Dictionary; + } + + interface LoDashImplicitWrapper { + /** + * @see _.forEach + */ + each( + iteratee: StringIterator, + thisArg?: any + ): LoDashImplicitWrapper; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.forEach + */ + each( + iteratee: ListIterator, + thisArg?: any + ): LoDashImplicitArrayWrapper; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.forEach + */ + each( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashImplicitObjectWrapper; + } + + interface LoDashExplicitWrapper { + /** + * @see _.forEach + */ + each( + iteratee: StringIterator, + thisArg?: any + ): LoDashExplicitWrapper; + } + + interface LoDashExplicitArrayWrapper { + /** + * @see _.forEach + */ + each( + iteratee: ListIterator, + thisArg?: any + ): LoDashExplicitArrayWrapper; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.forEach + */ + each( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashExplicitObjectWrapper; + } + //_.every interface LoDashStatic { /** @@ -4520,108 +4619,111 @@ declare module _ { //_.forEach interface LoDashStatic { /** - * Iterates over elements of a collection, executing the callback for each element. - * The callback is bound to thisArg and invoked with three arguments; (value, index|key, - * collection). Callbacks may exit iteration early by explicitly returning false. - * @param collection The collection to iterate over. - * @param callback The function called per iteration. - * @param thisArg The this binding of callback. - **/ - forEach( - collection: Array, - callback: ListIterator, - thisArg?: any): Array; + * Iterates over elements of collection invoking iteratee for each element. The iteratee is bound to thisArg + * and invoked with three arguments: + * (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false. + * + * Note: As with other "Collections" methods, objects with a "length" property are iterated like arrays. To + * avoid this behavior _.forIn or _.forOwn may be used for object iteration. + * + * @alias _.each + * + * @param collection The collection to iterate over. + * @param iteratee The function invoked per iteration. + * @param thisArg The this binding of iteratee. + */ + forEach( + collection: string, + iteratee?: StringIterator, + thisArg?: any + ): string; /** - * @see _.forEach - **/ + * @see _.forEach + */ + forEach( + collection: T[], + iteratee?: ListIterator, + thisArg?: any + ): T[]; + + /** + * @see _.forEach + */ forEach( collection: List, - callback: ListIterator, - thisArg?: any): List; + iteratee?: ListIterator, + thisArg?: any + ): List; /** - * @see _.forEach - **/ - forEach( - object: Dictionary, - callback: DictionaryIterator, - thisArg?: any): Dictionary; + * @see _.forEach + */ + forEach( + collection: Dictionary, + iteratee?: DictionaryIterator, + thisArg?: any + ): Dictionary; + } + interface LoDashImplicitWrapper { /** - * @see _.each - **/ - forEach( - object: T, - callback: ObjectIterator, - thisArg?: any): T - - /** - * @see _.forEach - **/ - each( - collection: Array, - callback: ListIterator, - thisArg?: any): Array; - - /** - * @see _.forEach - **/ - each( - collection: List, - callback: ListIterator, - thisArg?: any): List; - - /** - * @see _.forEach - * @param object The object to iterate over - * @param callback The function called per iteration. - * @param thisArg The this binding of callback. - **/ - each( - object: Dictionary, - callback: DictionaryIterator, - thisArg?: any): Dictionary; - - /** - * @see _.each - **/ - each( - object: T, - callback: ObjectIterator, - thisArg?: any): T + * @see _.forEach + */ + forEach( + iteratee: StringIterator, + thisArg?: any + ): LoDashImplicitWrapper; } interface LoDashImplicitArrayWrapper { /** - * @see _.forEach - **/ + * @see _.forEach + */ forEach( - callback: ListIterator, - thisArg?: any): LoDashImplicitArrayWrapper; - - /** - * @see _.forEach - **/ - each( - callback: ListIterator, - thisArg?: any): LoDashImplicitArrayWrapper; + iteratee: ListIterator, + thisArg?: any + ): LoDashImplicitArrayWrapper; } interface LoDashImplicitObjectWrapper { /** - * @see _.forEach - **/ - forEach( - callback: ObjectIterator, - thisArg?: any): LoDashImplicitObjectWrapper; + * @see _.forEach + */ + forEach( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashImplicitObjectWrapper; + } + interface LoDashExplicitWrapper { /** - * @see _.forEach - **/ - each( - callback: ObjectIterator, - thisArg?: any): LoDashImplicitObjectWrapper; + * @see _.forEach + */ + forEach( + iteratee: StringIterator, + thisArg?: any + ): LoDashExplicitWrapper; + } + + interface LoDashExplicitArrayWrapper { + /** + * @see _.forEach + */ + forEach( + iteratee: ListIterator, + thisArg?: any + ): LoDashExplicitArrayWrapper; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.forEach + */ + forEach( + iteratee?: ListIterator|DictionaryIterator, + thisArg?: any + ): LoDashExplicitObjectWrapper; } //_.forEachRight @@ -11043,6 +11145,10 @@ declare module _ { (element: T, key?: string, collection?: any): TResult; } + interface StringIterator { + (char: string, index?: number, string?: string): TResult; + } + interface MemoVoidIterator { (prev: TResult, curr: T, indexOrKey?: any, list?: T[]): void; }