_.indexBy definitions

This commit is contained in:
Brian Zengel
2013-10-18 19:45:46 -04:00
parent fb302c7df6
commit a951b7d410
2 changed files with 51 additions and 30 deletions
+14 -12
View File
@@ -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<number[]>>_.groupBy([4.2, 6.1, 6.4], function(num) { retu
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 = <_.Dictionary<IKey>>_.indexBy(keys, 'dir');
result = <_.Dictionary<IKey>>_.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
result = <_.Dictionary<IKey>>_.indexBy(keys, function(key) { this.fromCharCode(key.code); }, String);
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');
@@ -328,14 +342,6 @@ result = <boolean>_.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)
+37 -18
View File
@@ -1220,6 +1220,43 @@ declare module _ {
collection: List<T>,
whereValue: Dictionary<any>): Dictionary<T[]>;
/**
* 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<T>(
list: List<T>,
iterator: ListIterator<T, any>,
context?: any): Dictionary<T>;
/**
* @see _.indexBy
* @param pluckValue _.pluck style callback
**/
export function indexBy<T>(
collection: List<T>,
pluckValue: string): Dictionary<T>;
/**
* @see _.indexBy
* @param whereValue _.where style callback
**/
export function indexBy<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,
@@ -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<T>(
list: List<T>,
iterator: ListIterator<T, any>,
context?: any): Dictionary<T>;
/**
* @see _.indexBy
* @param iterator Property on each object to index them by.
**/
export function indexBy<T>(
list: List<T>,
iterator: string,
context?: any): Dictionary<T>;
/**
* Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle.
* @param list List to shuffle.