_.groupBy definitions

This commit is contained in:
Brian Zengel
2013-10-18 19:35:17 -04:00
parent fdd3bcba57
commit fb302c7df6
2 changed files with 40 additions and 23 deletions
+4
View File
@@ -271,6 +271,10 @@ result = <_.Dictionary<number>>_.forEachRight({ 'one': 1, 'two': 2, 'three': 3 }
result = <number[]>_.eachRight([1, 2, 3], function(num) { console.log(num); });
result = <_.Dictionary<number>>_.eachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); });
result = <_.Dictionary<number[]>>_.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
result = <_.Dictionary<number[]>>_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
result = <_.Dictionary<string[]>>_.groupBy(['one', 'two', 'three'], 'length');
result = <any[]>_.map([1, 2, 3], function(num) { return num * 3; });
result = <any[]>_.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
result = <any[]>_.map(stoogesAges, 'name');
+36 -23
View File
@@ -1184,6 +1184,42 @@ declare module _ {
callback: ObjectIterator<T, void>,
thisArg?: any): Dictionary<T>;
/**
* Creates an object composed of keys generated from the results of running each element
* of a collection through the callback. The corresponding value of each key is an array
* of the elements responsible for generating the key. 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 Returns the composed aggregate object.
**/
export function groupBy<T>(
collection: List<T>,
callback?: ListIterator<T, any>,
thisArg?: any): Dictionary<T[]>;
/**
* @see _.groupBy
* @param pluckValue _.pluck style callback
**/
export function groupBy<T>(
collection: List<T>,
pluckValue: string): Dictionary<T[]>;
/**
* @see _.groupBy
* @param whereValue _.where style callback
**/
export function groupBy<T>(
collection: List<T>,
whereValue: Dictionary<any>): Dictionary<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,
@@ -1519,29 +1555,6 @@ declare module _ {
iterator: string,
context?: any): T[];
/**
* Splits a collection into sets, grouped by the result of running each value through iterator.
* If iterator is a string instead of a function, groups by the property named by iterator on
* each of the values.
* @param list Groups this list.
* @param iterator Group iterator for each element within `list`, return the key to group the element by.
* @param context `this` object in `iterator`, optional.
* @return An object with the group names as properties where each property contains the grouped elements from `list`.
**/
export function groupBy<T>(
list: List<T>,
iterator?: ListIterator<T, any>,
context?: any): Dictionary<T[]>;
/**
* @see _.groupBy
* @param iterator Property on each object to group them by.
**/
export function groupBy<T>(
list: List<T>,
iterator: string,
context?: any): Dictionary<T[]>;
/**
* Given a `list`, and an `iterator` function that returns a key for each element in the list (or a property name),
* returns an object with an index of each item. Just like _.groupBy, but for when you know your keys are unique.