diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 79c46010d..a73cc6702 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -28,6 +28,11 @@ interface IStoogesAge { age: number; } +interface IKey { + dir: string; + code: number; +} + var foodsOrganic: IFoodOrganic[] = [ { name: 'banana', organic: true }, { name: 'beet', organic: false }, @@ -51,6 +56,11 @@ var stoogesAges: IStoogesAge[] = [ { 'name': 'larry', 'age': 50 } ]; +var keys: IKey[] = [ + { 'dir': 'left', 'code': 97 }, + { 'dir': 'right', 'code': 100 } +]; + var result; //Array Method Tests @@ -275,6 +285,10 @@ result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { retu result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math); result = <_.Dictionary>_.groupBy(['one', 'two', 'three'], 'length'); +result = <_.Dictionary>_.indexBy(keys, 'dir'); +result = <_.Dictionary>_.indexBy(keys, function(key) { return String.fromCharCode(key.code); }); +result = <_.Dictionary>_.indexBy(keys, function(key) { this.fromCharCode(key.code); }, String); + 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'); @@ -328,14 +342,6 @@ result = _.some(foodsCombined, { 'type': 'meat' }); //WHAT'S LEFT //////////////////////////////////////////////////////////////////////////////////////// -var sum = _.reduce([1, 2, 3], (memo, num) => memo + num, 0); -sum = _.reduce([1, 2, 3], (memo, num) => memo + num); // memo is optional #issue 5 github - -var list = [[0, 1], [2, 3], [4, 5]]; -var flat = _.reduceRight(list, (a, b) => a.concat(b), []); - -var even = _.find([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0); - var listOfPlays = [{ title: "Cymbeline", author: "Shakespeare", year: 1611 }, { title: "The Tempest", author: "Shakespeare", year: 1611 }, { title: "Other", author: "Not Shakespeare", year: 2012 }]; _.where(listOfPlays, { author: "Shakespeare", year: 1611 }); @@ -356,10 +362,6 @@ _.min(numbers); _.sortBy([1, 2, 3, 4, 5, 6], (num) => Math.sin(num)); -_([1.3, 2.1, 2.4]).groupBy((e) => Math.floor(e)); -_.groupBy([1.3, 2.1, 2.4], (num: number) => Math.floor(num).toString()); -_.groupBy(['one', 'two', 'three'], 'length'); - _.indexBy(stooges, 'age')['40'].age; _(stooges).indexBy('age')['40'].name; _(stooges) diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 3e26ce0cb..ebf5d80a8 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -1220,6 +1220,43 @@ declare module _ { collection: List, whereValue: Dictionary): Dictionary; + /** + * Creates an object composed of keys generated from the results of running each element + * of the collection through the given callback. The corresponding value of each key is + * the last element 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 indexBy( + list: List, + iterator: ListIterator, + context?: any): Dictionary; + + /** + * @see _.indexBy + * @param pluckValue _.pluck style callback + **/ + export function indexBy( + collection: List, + pluckValue: string): Dictionary; + + /** + * @see _.indexBy + * @param whereValue _.where style callback + **/ + export function indexBy( + 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, @@ -1555,24 +1592,6 @@ declare module _ { iterator: string, context?: any): 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. - **/ - export function indexBy( - list: List, - iterator: ListIterator, - context?: any): Dictionary; - - /** - * @see _.indexBy - * @param iterator Property on each object to index them by. - **/ - export function indexBy( - list: List, - iterator: string, - context?: any): Dictionary; - /** * Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle. * @param list List to shuffle.