From fb302c7df62ccbcf817e3e61bc91c39545973a6b Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 19:35:17 -0400 Subject: [PATCH] _.groupBy definitions --- lodash/lodash-tests.ts | 4 +++ lodash/lodash.d.ts | 59 ++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index f1fa2c014..79c46010d 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -271,6 +271,10 @@ result = <_.Dictionary>_.forEachRight({ 'one': 1, 'two': 2, 'three': 3 } result = _.eachRight([1, 2, 3], function(num) { console.log(num); }); result = <_.Dictionary>_.eachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); +result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); }); +result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math); +result = <_.Dictionary>_.groupBy(['one', 'two', 'three'], 'length'); + result = _.map([1, 2, 3], function(num) { return num * 3; }); result = _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); result = _.map(stoogesAges, 'name'); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index fc431ae37..3e26ce0cb 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -1184,6 +1184,42 @@ declare module _ { callback: ObjectIterator, thisArg?: any): Dictionary; + /** + * 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( + collection: List, + callback?: ListIterator, + thisArg?: any): Dictionary; + + /** + * @see _.groupBy + * @param pluckValue _.pluck style callback + **/ + export function groupBy( + collection: List, + pluckValue: string): Dictionary; + + /** + * @see _.groupBy + * @param whereValue _.where style callback + **/ + export function groupBy( + collection: List, + whereValue: Dictionary): 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, @@ -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( - list: List, - iterator?: ListIterator, - context?: any): Dictionary; - - /** - * @see _.groupBy - * @param iterator Property on each object to group them by. - **/ - export function groupBy( - list: List, - iterator: string, - context?: any): Dictionary; - /** * 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.