From 8d492cb44ed62f110f80bed7d90bab90a2ce0896 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 18:43:33 -0400 Subject: [PATCH] _.forEach/_.each definitions --- lodash/lodash-tests.ts | 18 ++++++--- lodash/lodash.d.ts | 83 ++++++++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 46 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 5ba9f9a7c2..d8878c290e 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -219,6 +219,14 @@ result = <_.Dictionary>_.countBy([4.3, 6.1, 6.4], function(num) { return result = <_.Dictionary>_.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math); result = <_.Dictionary>_.countBy(['one', 'two', 'three'], 'length'); +result = _.every([true, 1, null, 'yes'], Boolean); +result = _.every(stoogesAges, 'age'); +result = _.every(stoogesAges, { 'age': 50 }); + + result = _.all([true, 1, null, 'yes'], Boolean); + result = _.all(stoogesAges, 'age'); + result = _.all(stoogesAges, { 'age': 50 }); + result = _.find([1, 2, 3, 4], function(num) { return num % 2 == 0; }); @@ -237,13 +245,11 @@ result = _.findLast([1, 2, 3, 4], function(num) { result = _.findLast(foodsCombined, { 'type': 'vegetable' }); result = _.findLast(foodsCombined, 'organic'); -result = _.every([true, 1, null, 'yes'], Boolean); -result = _.every(stoogesAges, 'age'); -result = _.every(stoogesAges, { 'age': 50 }); +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 = _.all([true, 1, null, 'yes'], Boolean); - result = _.all(stoogesAges, 'age'); - result = _.all(stoogesAges, { 'age': 50 }); + 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 = _.map([1, 2, 3], function(num) { return num * 3; }); result = _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index a4b8a3af6e..9e4853470d 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -1021,6 +1021,48 @@ declare module _ { collection: Collection, pluckValue: string): T; + /** + * 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. + **/ + export function forEach( + collection: List, + callback: ListIterator, + thisArg?: any): List; + + /** + * @see _.each + **/ + export function forEach( + object: Dictionary, + callback: ObjectIterator, + thisArg?: any): Dictionary; + + /** + * @see _.each + **/ + export function each( + collection: List, + callback: ListIterator, + thisArg?: any): List; + + /** + * @see _.each + * @param object Iterators over this object's properties. + * @param iterator Iterator function for each property on `obj`. + * @param context 'this' object in `iterator`, optional. + **/ + export function each( + object: Dictionary, + callback: ObjectIterator, + thisArg?: any): Dictionary; + + + /** * Creates an array of values by running each element in the collection through the callback. * The callback is bound to thisArg and invoked with three arguments; (value, index|key, @@ -1166,46 +1208,7 @@ declare module _ { - /** - * Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is - * bound to the context object, if one is passed. Each invocation of iterator is called with three - * arguments: (element, index, list). If list is a JavaScript object, iterator's arguments will be - * (value, key, object). Delegates to the native forEach function if it exists. - * @param list Iterates over this list of elements. - * @param iterator Iterator function for each element `list`. - * @param context 'this' object in `iterator`, optional. - **/ - export function each( - list: List, - iterator: ListIterator, - context?: any): void; - - /** - * @see _.each - * @param object Iterators over this object's properties. - * @param iterator Iterator function for each property on `obj`. - * @param context 'this' object in `iterator`, optional. - **/ - export function each( - object: Dictionary, - iterator: ObjectIterator, - context?: any): void; - - /** - * @see _.each - **/ - export function forEach( - list: List, - iterator: ListIterator, - context?: any): void; - - /** - * @see _.each - **/ - export function forEach( - object: Dictionary, - iterator: ObjectIterator, - context?: any): void; + /** * Also known as inject and foldl, reduce boils down a list of values into a single value.