initial Collection methods

_.all/_.every
_.any/_.some
_.at
This commit is contained in:
Brian Zengel
2013-10-18 15:02:57 -04:00
parent bba7a768ce
commit 2081d4259c
2 changed files with 219 additions and 50 deletions
+56 -9
View File
@@ -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 = <any[][]>_.unzip(['moe', 'larry'], [30, 40], [true, false]);
result = <number[]>_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
/* *************
* Collections *
************* */
result = <string>_.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
result = <string>_.at(['moe', 'larry', 'curly'], 0, 2);
result = <boolean>_.every([true, 1, null, 'yes'], Boolean);
result = <boolean>_.every(stoogesAges, 'age');
result = <boolean>_.every(stoogesAges, { 'age': 50 });
result = <boolean>_.all([true, 1, null, 'yes'], Boolean);
result = <boolean>_.all(stoogesAges, 'age');
result = <boolean>_.all(stoogesAges, { 'age': 50 });
result = <boolean>_.some([null, 0, 'yes', false], Boolean);
result = <boolean>_.some(foodsCombined, 'organic');
result = <boolean>_.some(foodsCombined, { 'type': 'meat' });
result = <boolean>_.any([null, 0, 'yes', false], Boolean);
result = <boolean>_.any(foodsCombined, 'organic');
result = <boolean>_.any(foodsCombined, { 'type': 'meat' });
////////////////////////////////////////////////////////////////////////////////////////
+163 -41
View File
@@ -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<T>(
collection: Collection<T>,
indexes: number[]): T[];
/**
* @see _.at
**/
export function at<T>(
collection: Collection<T>,
...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<T>(
collection: Collection<T>,
callback?: ListIterator<T, boolean>,
thisArg?: any): boolean;
/**
* @see _.every
* @param pluckValue _.pluck style callback
**/
export function every<T>(
collection: Collection<T>,
pluckValue: string): boolean;
/**
* @see _.every
* @param whereValue _.where style callback
**/
export function every<T>(
collection: Collection<T>,
whereValue: Dictionary<any>): boolean;
/**
* @see _.every
**/
export function all<T>(
collection: Collection<T>,
callback?: ListIterator<T, boolean>,
thisArg?: any): boolean;
/**
* @see _.every
* @param pluckValue _.pluck style callback
**/
export function all<T>(
collection: Collection<T>,
pluckValue: string): boolean;
/**
* @see _.every
* @param whereValue _.where style callback
**/
export function all<T>(
collection: Collection<T>,
whereValue: Dictionary<any>): 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<T>(
collection: Collection<T>,
callback?: ListIterator<T, boolean>,
thisArg?: any): boolean;
/**
* @see _.some
* @param pluckValue _.pluck style callback
**/
export function some<T>(
collection: Collection<T>,
pluckValue: string): boolean;
/**
* @see _.some
* @param whereValue _.where style callback
**/
export function some<T>(
collection: Collection<T>,
whereValue: Dictionary<any>): boolean;
/**
* @see _.some
**/
export function any<T>(
collection: Collection<T>,
callback?: ListIterator<T, boolean>,
thisArg?: any): boolean;
/**
* @see _.some
* @param pluckValue _.pluck style callback
**/
export function any<T>(
collection: Collection<T>,
pluckValue: string): boolean;
/**
* @see _.some
* @param whereValue _.where style callback
**/
export function any<T>(
collection: Collection<T>,
whereValue: Dictionary<any>): 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<T, boolean>,
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<T>(
list: Collection<T>,
iterator?: ListIterator<T, boolean>,
context?: any): boolean;
/**
* @see _.all
**/
export function all<T>(
list: Collection<T>,
iterator?: ListIterator<T, boolean>,
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<T>(
list: Collection<T>,
iterator?: ListIterator<T, boolean>,
context?: any): boolean;
/**
* @see _.any
**/
export function some<T>(
list: Collection<T>,
iterator?: ListIterator<T, boolean>,
context?: any): boolean;
/**
* Returns true if the value is present in the list. Uses indexOf internally,