From ec87d352a29d4729b1a2ef678b0a11774850178b Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 18:27:18 -0400 Subject: [PATCH] _.find/_.detect and _.findLast definitions --- lodash/lodash-tests.ts | 18 +++++++ lodash/lodash.d.ts | 115 +++++++++++++++++++++++++++++++---------- 2 files changed, 107 insertions(+), 26 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index dd2b28d06..5ba9f9a7c 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -219,6 +219,24 @@ 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 = _.find([1, 2, 3, 4], function(num) { + return num % 2 == 0; +}); +result = _.find(foodsCombined, { 'type': 'vegetable' }); +result = _.find(foodsCombined, 'organic'); + + result = _.detect([1, 2, 3, 4], function(num) { + return num % 2 == 0; + }); + result = _.detect(foodsCombined, { 'type': 'vegetable' }); + result = _.detect(foodsCombined, 'organic'); + +result = _.findLast([1, 2, 3, 4], function(num) { + return num % 2 == 0; +}); +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 }); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 2b76340d4..a4b8a3af6 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -932,6 +932,95 @@ declare module _ { collection: Collection, whereValue: Dictionary): boolean; + /** + * Iterates over elements of a collection, returning the first element that the callback + * returns truey for. The callback is bound to thisArg and invoked with three arguments; + * (value, index|key, collection). + * + * If a property name is provided for callback the created "_.pluck" style callback will + * return the property value of the given element. + * + * If an object is provided for callback the created "_.where" style callback will return + * true for elements that have the properties of the given object, else false. + * @param collection Searches for a value in this list. + * @param callback The function called per iteration. + * @param thisArg The this binding of callback. + * @return The found element, else undefined. + **/ + export function find( + collection: Collection, + callback: ListIterator, + thisArg?: any): T; + + /** + * @see _.find + * @param _.pluck style callback + **/ + export function find( + collection: Collection, + whereValue: Dictionary): T; + + /** + * @see _.find + * @param _.where style callback + **/ + export function find( + collection: Collection, + pluckValue: string): T; + + /** + * @see _.find + **/ + export function detect( + collection: Collection, + callback: ListIterator, + thisArg?: any): T; + + /** + * @see _.find + * @param _.pluck style callback + **/ + export function detect( + collection: Collection, + whereValue: Dictionary): T; + + /** + * @see _.find + * @param _.where style callback + **/ + export function detect( + collection: Collection, + pluckValue: string): T; + + /** + * This method is like _.find except that it iterates over elements of a collection from + * right to left. + * @param collection Searches for a value in this list. + * @param callback The function called per iteration. + * @param thisArg The this binding of callback. + * @return The found element, else undefined. + **/ + export function findLast( + collection: Collection, + callback: ListIterator, + thisArg?: any): T; + + /** + * @see _.find + * @param _.pluck style callback + **/ + export function findLast( + collection: Collection, + whereValue: Dictionary): T; + + /** + * @see _.find + * @param _.where style callback + **/ + export function findLast( + collection: Collection, + pluckValue: string): T; + /** * 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, @@ -1178,32 +1267,6 @@ declare module _ { memo?: TResult, context?: any): TResult; - /** - * Looks through each value in the list, returning the first one that passes a truth - * test (iterator). The function returns as soon as it finds an acceptable element, - * and doesn't traverse the entire list. - * @param list Searches for a value in this list. - * @param iterator Search iterator function for each element in `list`. - * @param context `this` object in `iterator`, optional. - * @return The first acceptable found element in `list`, if nothing is found undefined/null is returned. - **/ - export function find( - list: Collection, - iterator: ListIterator, - context?: any): T; - - - - - - /** - * @see _.find - **/ - export function detect( - list: Collection, - iterator: ListIterator, - context?: any): T; - /** * Looks through each value in the list, returning an array of all the values that pass a truth * test (iterator). Delegates to the native filter method, if it exists.