Merge pull request #2282 from bczengel/lodash

Lodash - Added chainable definitions for a handful of methods
This commit is contained in:
Masahiro Wakame
2014-06-05 11:40:47 +09:00
2 changed files with 497 additions and 3 deletions
+89 -3
View File
@@ -183,6 +183,14 @@ result = <number[]>_.first([1, 2, 3], function (num) {
result = <IFoodOrganic[]>_.first(foodsOrganic, 'organic');
result = <IFoodType[]>_.first(foodsType, { 'type': 'fruit' });
result = <number>_([1, 2, 3]).first();
result = <number[]>_([1, 2, 3]).first(2).value();
result = <number[]>_([1, 2, 3]).first(function (num) {
return num < 3;
}).value();
result = <IFoodOrganic[]>_(foodsOrganic).first('organic').value();
result = <IFoodType[]>_(foodsType).first({ 'type': 'fruit' }).value();
result = <number>_.head([1, 2, 3]);
result = <number[]>_.head([1, 2, 3], 2);
result = <number[]>_.head([1, 2, 3], function (num) {
@@ -191,12 +199,28 @@ result = <number[]>_.head([1, 2, 3], function (num) {
result = <IFoodOrganic[]>_.head(foodsOrganic, 'organic');
result = <IFoodType[]>_.head(foodsType, { 'type': 'fruit' });
result = <number>_([1, 2, 3]).head();
result = <number[]>_([1, 2, 3]).head(2).value();
result = <number[]>_([1, 2, 3]).head(function (num) {
return num < 3;
}).value();
result = <IFoodOrganic[]>_(foodsOrganic).head('organic').value();
result = <IFoodType[]>_(foodsType).head({ 'type': 'fruit' }).value();
result = <number>_.take([1, 2, 3]);
result = <number[]>_.take([1, 2, 3], 2);
result = <number[]>_.take([1, 2, 3], (num) => num < 3);
result = <IFoodOrganic[]>_.take(foodsOrganic, 'organic');
result = <IFoodType[]>_.take(foodsType, { 'type': 'fruit' });
result = <number>_([1, 2, 3]).take();
result = <number[]>_([1, 2, 3]).take(2).value();
result = <number[]>_([1, 2, 3]).take(function (num) {
return num < 3;
}).value();
result = <IFoodOrganic[]>_(foodsOrganic).take('organic').value();
result = <IFoodType[]>_(foodsType).take({ 'type': 'fruit' }).value();
result = <number[]>_.flatten([1, [2], [3, [[4]]]]);
result = <any[]>_.flatten([1, [2], [3, [[4]]]], true);
var result: any
@@ -277,6 +301,22 @@ result = <string[]>_.unique(['A', 'b', 'C', 'a', 'B', 'c'], function (letter) {
result = <number[]>_.unique([1, 2.5, 3, 1.5, 2, 3.5], function (num) { return this.floor(num); }, Math);
result = <{ x: number; }[]>_.unique([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
result = <number[]>_([1, 2, 1, 3, 1]).uniq().value();
result = <number[]>_([1, 1, 2, 2, 3]).uniq(true).value();
result = <string[]>_(['A', 'b', 'C', 'a', 'B', 'c']).uniq(function (letter) {
return letter.toLowerCase();
}).value();
result = <number[]>_([1, 2.5, 3, 1.5, 2, 3.5]).uniq(function (num) { return this.floor(num); }, Math).value();
result = <{ x: number; }[]>_([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }]).uniq('x').value();
result = <number[]>_([1, 2, 1, 3, 1]).unique().value();
result = <number[]>_([1, 1, 2, 2, 3]).unique(true).value();
result = <string[]>_(['A', 'b', 'C', 'a', 'B', 'c']).unique(function (letter) {
return letter.toLowerCase();
}).value();
result = <number[]>_([1, 2.5, 3, 1.5, 2, 3.5]).unique(function (num) { return this.floor(num); }, Math).value();
result = <{ x: number; }[]>_([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }]).unique('x').value();
result = <number[]>_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
result = <any[][]>_.zip(['moe', 'larry'], [30, 40], [true, false]);
@@ -357,9 +397,11 @@ result = <IFoodCombined>_.findLast(foodsCombined, 'organic');
result = <number[]>_.forEach([1, 2, 3], function (num) { console.log(num); });
result = <_.Dictionary<number>>_.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { console.log(num); });
result = <IFoodType>_.forEach<IFoodType, string>({ name: 'apple', type: 'fruit' }, function (value, key) { console.log(value, key) });
result = <number[]>_.each([1, 2, 3], function (num) { console.log(num); });
result = <_.Dictionary<number>>_.each({ 'one': 1, 'two': 2, 'three': 3 }, function (num) { console.log(num); });
result = <IFoodType>_.each<IFoodType, string>({ name: 'apple', type: 'fruit' }, function (value, key) { console.log(value, key) });
result = <_.LoDashArrayWrapper<number>>_([1, 2, 3]).forEach(function (num) { console.log(num); });
result = <_.LoDashObjectWrapper<_.Dictionary<number>>>_(<{ [index: string]: number; }>{ 'one': 1, 'two': 2, 'three': 3 }).forEach(function (num) { console.log(num); });
@@ -419,16 +461,18 @@ result = <IStoogesAge>_.min(stoogesAges, function (stooge) { return stooge.age;
result = <IStoogesAge>_.min(stoogesAges, 'age');
result = <string[]>_.pluck(stoogesAges, 'name');
result = <string[]>_(stoogesAges).pluck('name').value();
result = <number>_.reduce<number, number>([1, 2, 3], function (sum: number, num: number) {
return sum + num;
});
interface ABC {
[index: string]: number;
a: number;
b: number;
c: number;
}
result = <number>_.reduce<number, number>([1, 2, 3], function (sum: number, num: number) {
return sum + num;
});
result = <ABC>_.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function (r: ABC, num: number, key: string) {
r[key] = num * 3;
return r;
@@ -450,6 +494,30 @@ result = <ABC>_.inject({ 'a': 1, 'b': 2, 'c': 3 }, function (r: ABC, num: number
return r;
}, {});
result = <number>_([1, 2, 3]).reduce<number>(function (sum: number, num: number) {
return sum + num;
});
result = <ABC>_({ 'a': 1, 'b': 2, 'c': 3 }).reduce<number, ABC>(function (r: ABC, num: number, key: string) {
r[key] = num * 3;
return r;
}, {});
result = <number>_([1, 2, 3]).foldl<number>(function (sum: number, num: number) {
return sum + num;
});
result = <ABC>_({ 'a': 1, 'b': 2, 'c': 3 }).foldl<number, ABC>(function (r: ABC, num: number, key: string) {
r[key] = num * 3;
return r;
}, {});
result = <number>_([1, 2, 3]).inject<number>(function (sum: number, num: number) {
return sum + num;
});
result = <ABC>_({ 'a': 1, 'b': 2, 'c': 3 }).inject<number, ABC>(function (r: ABC, num: number, key: string) {
r[key] = num * 3;
return r;
}, {});
result = <number[]>_.reduceRight([[0, 1], [2, 3], [4, 5]], function (a: number[], b: number[]) { return a.concat(b); }, <number[]>[]);
result = <number[]>_.foldr([[0, 1], [2, 3], [4, 5]], function (a: number[], b: number[]) { return a.concat(b); }, <number[]>[]);
@@ -457,6 +525,10 @@ result = <number[]>_.reject([1, 2, 3, 4, 5, 6], function (num) { return num % 2
result = <IFoodCombined[]>_.reject(foodsCombined, 'organic');
result = <IFoodCombined[]>_.reject(foodsCombined, { 'type': 'fruit' });
result = <number[]>_([1, 2, 3, 4, 5, 6]).reject(function (num) { return num % 2 == 0; }).value();
result = <IFoodCombined[]>_(foodsCombined).reject('organic').value();
result = <IFoodCombined[]>_(foodsCombined).reject({ 'type': 'fruit' }).value();
result = <number>_.sample([1, 2, 3, 4]);
result = <number[]>_.sample([1, 2, 3, 4], 2);
@@ -480,11 +552,18 @@ result = <number[]>_.sortBy([1, 2, 3], function (num) { return Math.sin(num); })
result = <number[]>_.sortBy([1, 2, 3], function (num) { return this.sin(num); }, Math);
result = <string[]>_.sortBy(['banana', 'strawberry', 'apple'], 'length');
result = <number[]>_([1, 2, 3]).sortBy(function (num) { return Math.sin(num); }).value();
result = <number[]>_([1, 2, 3]).sortBy(function (num) { return this.sin(num); }, Math).value();
result = <string[]>_(['banana', 'strawberry', 'apple']).sortBy('length').value();
(function (a: number, b: number, c: number, d: number) { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
result = <IStoogesCombined[]>_.where(stoogesCombined, { 'age': 40 });
result = <IStoogesCombined[]>_.where(stoogesCombined, { 'quotes': ['Poifect!'] });
result = <IStoogesCombined[]>_(stoogesCombined).where({ 'age': 40 }).value();
result = <IStoogesCombined[]>_(stoogesCombined).where({ 'quotes': ['Poifect!'] }).value();
/*************
* Functions *
*************/
@@ -834,6 +913,7 @@ result = <boolean>_.isString('moe');
result = <boolean>_.isUndefined(void 0);
result = <string[]>_.keys({ 'one': 1, 'two': 2, 'three': 3 });
result = <string[]>_({ 'one': 1, 'two': 2, 'three': 3 }).keys().value();
var mergeNames = {
'stooges': [
@@ -879,8 +959,14 @@ result = <HasName>_.omit({ 'name': 'moe', 'age': 40 }, ['age']);
result = <HasName>_.omit({ 'name': 'moe', 'age': 40 }, function (value) {
return typeof value == 'number';
});
result = <HasName>_({ 'name': 'moe', 'age': 40 }).omit('age').value();
result = <HasName>_({ 'name': 'moe', 'age': 40 }).omit(['age']).value();
result = <HasName>_({ 'name': 'moe', 'age': 40 }).omit(function (value) {
return typeof value == 'number';
}).value();
result = <any[][]>_.pairs({ 'moe': 30, 'larry': 40 });
result = <any[][]>_({ 'moe': 30, 'larry': 40 }).pairs().value();
result = <HasName>_.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name');
result = <HasName>_.pick({ 'name': 'moe', '_userid': 'moe1' }, ['name']);
+408
View File
@@ -644,6 +644,104 @@ declare module _ {
whereValue: W): T[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.first
**/
first(): T;
/**
* @see _.first
* @param n The number of elements to return.
**/
first(n: number): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
first(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
first(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
first<W>(whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.first
**/
head(): T;
/**
* @see _.first
* @param n The number of elements to return.
**/
head(n: number): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
head(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
head(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
head<W>(whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.first
**/
take(): T;
/**
* @see _.first
* @param n The number of elements to return.
**/
take(n: number): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
take(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
take(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
take<W>(whereValue: W): LoDashArrayWrapper<T>;
}
//_.flatten
interface LoDashStatic {
/**
@@ -1750,6 +1848,106 @@ declare module _ {
whereValue?: W): T[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.uniq
**/
uniq<TSort>(isSorted?: boolean): LoDashArrayWrapper<T>;
/**
* @see _.uniq
**/
uniq<TSort>(
isSorted: boolean,
callback: ListIterator<T, TSort>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.uniq
**/
uniq<TSort>(
callback: ListIterator<T, TSort>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param pluckValue _.pluck style callback
**/
uniq(
isSorted: boolean,
pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param pluckValue _.pluck style callback
**/
uniq(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param whereValue _.where style callback
**/
uniq<W>(
isSorted: boolean,
whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param whereValue _.where style callback
**/
uniq<W>(
whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.uniq
**/
unique<TSort>(isSorted?: boolean): LoDashArrayWrapper<T>;
/**
* @see _.uniq
**/
unique<TSort>(
isSorted: boolean,
callback: ListIterator<T, TSort>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.uniq
**/
unique<TSort>(
callback: ListIterator<T, TSort>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param pluckValue _.pluck style callback
**/
unique(
isSorted: boolean,
pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param pluckValue _.pluck style callback
**/
unique(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param whereValue _.where style callback
**/
unique<W>(
isSorted: boolean,
whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.uniq
* @param whereValue _.where style callback
**/
unique<W>(
whereValue: W): LoDashArrayWrapper<T>;
}
//_.without
interface LoDashStatic {
/**
@@ -2741,6 +2939,14 @@ declare module _ {
callback: ObjectIterator<T, void>,
thisArg?: any): Dictionary<T>;
/**
* @see _.each
**/
forEach<T extends {}, TValue>(
object: T,
callback: ObjectIterator<TValue, void>,
thisArg?: any): T
/**
* @see _.forEach
**/
@@ -2767,6 +2973,14 @@ declare module _ {
object: Dictionary<T>,
callback: ObjectIterator<T, void>,
thisArg?: any): Dictionary<T>;
/**
* @see _.each
**/
each<T extends {}, TValue>(
object: T,
callback: ObjectIterator<TValue, void>,
thisArg?: any): T
}
interface LoDashArrayWrapper<T> {
@@ -3444,6 +3658,22 @@ declare module _ {
property: string): any[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.pluck
**/
pluck<TResult>(
property: string): LoDashArrayWrapper<TResult>;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.pluck
**/
pluck<TResult>(
property: string): LoDashArrayWrapper<TResult>;
}
//_.reduce
interface LoDashStatic {
/**
@@ -3609,6 +3839,100 @@ declare module _ {
thisArg?: any): TResult;
}
interface LoDashArrayWrapper<T> {
/**
* @see _.reduce
**/
reduce<TResult>(
callback: MemoIterator<T, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
reduce<TResult>(
callback: MemoIterator<T, TResult>,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
inject<TResult>(
callback: MemoIterator<T, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
inject<TResult>(
callback: MemoIterator<T, TResult>,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
foldl<TResult>(
callback: MemoIterator<T, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
foldl<TResult>(
callback: MemoIterator<T, TResult>,
thisArg?: any): TResult;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.reduce
**/
reduce<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
reduce<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
inject<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
inject<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
foldl<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
accumulator: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
foldl<TValue, TResult>(
callback: MemoIterator<TValue, TResult>,
thisArg?: any): TResult;
}
//_.reduceRight
interface LoDashStatic {
/**
@@ -3806,6 +4130,27 @@ declare module _ {
whereValue: W): T[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.reject
**/
reject(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.reject
* @param pluckValue _.pluck style callback
**/
reject(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.reject
* @param whereValue _.where style callback
**/
reject<W>(whereValue: W): LoDashArrayWrapper<T>;
}
//_.sample
interface LoDashStatic {
/**
@@ -4134,6 +4479,27 @@ declare module _ {
whereValue: W): T[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.sortBy
**/
sortBy<TSort>(
callback?: ListIterator<T, TSort>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.sortBy
* @param pluckValue _.pluck style callback
**/
sortBy(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.sortBy
* @param whereValue _.where style callback
**/
sortBy<W>(whereValue: W): LoDashArrayWrapper<T>;
}
//_.toArray
interface LoDashStatic {
/**
@@ -4182,6 +4548,13 @@ declare module _ {
properties: U): T[];
}
interface LoDashArrayWrapper<T> {
/**
* @see _.where
**/
where<T, U extends {}>(properties: U): LoDashArrayWrapper<T>;
}
/*************
* Functions *
*************/
@@ -5296,6 +5669,13 @@ declare module _ {
keys(object: any): string[];
}
interface LoDashObjectWrapper<T> {
/**
* @see _.keys
**/
keys(): LoDashArrayWrapper<string>
}
//_.mapValues
interface LoDashStatic {
/**
@@ -5407,6 +5787,27 @@ declare module _ {
thisArg?: any): Omitted;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.omit
**/
omit<Omitted>(
...keys: string[]): LoDashObjectWrapper<Omitted>;
/**
* @see _.omit
**/
omit<Omitted>(
keys: string[]): LoDashObjectWrapper<Omitted>;
/**
* @see _.omit
**/
omit<Omitted>(
callback: ObjectIterator<any, boolean>,
thisArg?: any): LoDashObjectWrapper<Omitted>;
}
//_.pairs
interface LoDashStatic {
/**
@@ -5418,6 +5819,13 @@ declare module _ {
pairs(object: any): any[][];
}
interface LoDashObjectWrapper<T> {
/**
* @see _.pairs
**/
pairs(): LoDashArrayWrapper<any[]>;
}
//_.picks
interface LoDashStatic {
/**