From 2081d4259cd8d919420760733b80f151b7d48cf3 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 15:02:57 -0400 Subject: [PATCH] initial Collection methods _.all/_.every _.any/_.some _.at --- lodash/lodash-tests.ts | 65 +++++++++++-- lodash/lodash.d.ts | 204 ++++++++++++++++++++++++++++++++--------- 2 files changed, 219 insertions(+), 50 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 62f46c074b..945da750f5 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -12,24 +12,45 @@ interface IFoodType { type: string; } -interface IStoogesQuotes { +interface IFoodCombined { + name: string; + organic: boolean; + type: string; +} + +interface IStoogesQuote { name: string; quotes: string[]; } +interface IStoogesAge { + name: string; + age: number; +} + var foodsOrganic: IFoodOrganic[] = [ - { name: 'banana', organic: true }, - { name: 'beet', organic: false }, + { name: 'banana', organic: true }, + { name: 'beet', organic: false }, ]; var foodsType: IFoodType[] = [ - { name: 'apple', type: 'fruit' }, - { name: 'banana', type: 'fruit' }, - { name: 'beet', type: 'vegetable' } + { name: 'apple', type: 'fruit' }, + { name: 'banana', type: 'fruit' }, + { name: 'beet', type: 'vegetable' } ]; -var stoogesQuotes = [ - { 'name': 'curly', 'quotes': ['Oh, a wise guy, eh?', 'Poifect!'] }, - { 'name': 'moe', 'quotes': ['Spread out!', 'You knucklehead!'] } +var foodsCombined: IFoodCombined[] = [ + { 'name': 'apple', 'organic': false, 'type': 'fruit' }, + { 'name': 'carrot', 'organic': true, 'type': 'vegetable' } ]; + +var stoogesQuotes: IStoogesQuote[] = [ + { 'name': 'curly', 'quotes': ['Oh, a wise guy, eh?', 'Poifect!'] }, + { 'name': 'moe', 'quotes': ['Spread out!', 'You knucklehead!'] } +]; +var stoogesAges: IStoogesAge[] = [ + { 'name': 'moe', 'age': 40 }, + { 'name': 'larry', 'age': 50 } +]; + var result; //Array Method Tests @@ -177,6 +198,32 @@ result = _.unzip(['moe', 'larry'], [30, 40], [true, false]); result = _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); +/* ************* + * Collections * + ************* */ + +result = _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]); +result = _.at(['moe', 'larry', 'curly'], 0, 2); + +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 = _.some([null, 0, 'yes', false], Boolean); +result = _.some(foodsCombined, 'organic'); +result = _.some(foodsCombined, { 'type': 'meat' }); + + result = _.any([null, 0, 'yes', false], Boolean); + result = _.any(foodsCombined, 'organic'); + result = _.any(foodsCombined, { 'type': 'meat' }); + + + + //////////////////////////////////////////////////////////////////////////////////////// diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 629a15fb99..b41d54b4de 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -765,6 +765,168 @@ declare module _ { /* ************* * Collections * ************* */ + + /** + * Creates an array of elements from the specified indexes, or keys, of the collection. + * Indexes may be specified as individual arguments or as arrays of indexes. + * @param collection The collection to iterate over. + * @param indexes The indexes of collection to retrieve, specified as individual indexes or + * arrays of indexes. + * @return A new array of elements corresponding to the provided indexes. + **/ + export function at( + collection: Collection, + indexes: number[]): T[]; + + /** + * @see _.at + **/ + export function at( + collection: Collection, + ...indexes: number[]): T[]; + + + /** + * Checks if the given callback returns truey value for all elements of a collection. + * 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 The collection to iterate over. + * @param callback The function called per iteration. + * @param thisArg The this binding of callback. + * @return True if all elements passed the callback check, else false. + **/ + export function every( + collection: Collection, + callback?: ListIterator, + thisArg?: any): boolean; + + /** + * @see _.every + * @param pluckValue _.pluck style callback + **/ + export function every( + collection: Collection, + pluckValue: string): boolean; + + /** + * @see _.every + * @param whereValue _.where style callback + **/ + export function every( + collection: Collection, + whereValue: Dictionary): boolean; + + /** + * @see _.every + **/ + export function all( + collection: Collection, + callback?: ListIterator, + thisArg?: any): boolean; + + /** + * @see _.every + * @param pluckValue _.pluck style callback + **/ + export function all( + collection: Collection, + pluckValue: string): boolean; + + /** + * @see _.every + * @param whereValue _.where style callback + **/ + export function all( + collection: Collection, + whereValue: Dictionary): boolean; + + /** + * Checks if the callback returns a truey value for any element of a collection. The function + * returns as soon as it finds a passing value and does not iterate over the entire collection. + * 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 The collection to iterate over. + * @param callback The function called per iteration. + * @param thisArg The this binding of callback. + * @return True if any element passed the callback check, else false. + **/ + export function some( + collection: Collection, + callback?: ListIterator, + thisArg?: any): boolean; + + /** + * @see _.some + * @param pluckValue _.pluck style callback + **/ + export function some( + collection: Collection, + pluckValue: string): boolean; + + /** + * @see _.some + * @param whereValue _.where style callback + **/ + export function some( + collection: Collection, + whereValue: Dictionary): boolean; + + /** + * @see _.some + **/ + export function any( + collection: Collection, + callback?: ListIterator, + thisArg?: any): boolean; + + /** + * @see _.some + * @param pluckValue _.pluck style callback + **/ + export function any( + collection: Collection, + pluckValue: string): boolean; + + /** + * @see _.some + * @param whereValue _.where style callback + **/ + export function any( + collection: Collection, + whereValue: Dictionary): boolean; + + + + + + + + + + + + + + + + + + + + + + /** * Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is @@ -991,47 +1153,7 @@ declare module _ { iterator: ListIterator, context?: any): T[]; - /** - * Returns true if all of the values in the list pass the iterator truth test. Delegates to the - * native method every, if present. - * @param list Truth test against all elements within this list. - * @param iterator Trust test iterator function for each element in `list`. - * @param context `this` object in `iterator`, optional. - * @return True if all elements passed the truth test, otherwise false. - **/ - export function every( - list: Collection, - iterator?: ListIterator, - context?: any): boolean; - - /** - * @see _.all - **/ - export function all( - list: Collection, - iterator?: ListIterator, - context?: any): boolean; - - /** - * Returns true if any of the values in the list pass the iterator truth test. Short-circuits and - * stops traversing the list if a true element is found. Delegates to the native method some, if present. - * @param list Truth test against all elements within this list. - * @param iterator Trust test iterator function for each element in `list`. - * @param context `this` object in `iterator`, optional. - * @return True if any elements passed the truth test, otherwise false. - **/ - export function any( - list: Collection, - iterator?: ListIterator, - context?: any): boolean; - - /** - * @see _.any - **/ - export function some( - list: Collection, - iterator?: ListIterator, - context?: any): boolean; + /** * Returns true if the value is present in the list. Uses indexOf internally,