diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index ae5ab6f7ee..ae641fde52 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -283,24 +283,28 @@ _(['test', 'test']).pick(['test2', 'test2']); //////////////// Chain Tests function chain_tests() { // https://typescript.codeplex.com/workitem/1960 - var list:number[] = _.chain([1, 2, 3, 4, 5, 6, 7, 8]) - .filter(n => n % 2 == 0) - .map(n => n * n) - .value(); - - _([1, 2, 3, 4]) - .chain() - .filter((num: number) => { - return num % 2 == 0; - }).tap(alert) - .map((num: number) => { - return num * num; - }) + var numArray: number[] = _.chain([1, 2, 3, 4, 5, 6, 7, 8]) + .filter(num => num % 2 == 0) + .map(num => num * num) .value(); - _.chain([1, 2, 3, 200]) - .filter(function (num: number) { return num % 2 == 0; }) + var strArray: string[] = _([1, 2, 3, 4]) + .chain() + .filter(num => num % 2 == 0) .tap(alert) - .map(function (num: number) { return num * num }) + .map(num => "string" + num) + .value(); + + var n : number = _.chain([1, 2, 3, 200]) + .filter(num => num % 2 == 0) + .tap(alert) + .map(num => num * num) + .max() + .value(); + + var t2 : number = _([1, 2, 3]).chain() + .map(num=> [num, num + 1]) + .flatten() + .find(num => num % 2 == 0) .value(); } diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 4382f7bea7..53bbdffa4e 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1364,7 +1364,8 @@ interface UnderscoreStatic { * @param obj Object to chain. * @return Wrapped `obj`. **/ - chain(obj: any): _Chain; + chain(obj: T[]): _Chain; + chain(obj: T): _Chain; /** * Extracts the value of a wrapped object. @@ -2194,13 +2195,25 @@ interface _Chain { * Wrapped type `any[]`. * @see _.map **/ - map(iterator: _.ListIterator, context?: any): _Chain; + map(iterator: (value: T, index: number, list: T[]) => TArray[], context?: any): _ChainOfArrays; + //Not sure why this won't work, might be a TypeScript error? map(iterator: _.ListIterator, context?: any): _ChainOfArrays; + /** + * Wrapped type `any[]`. + * @see _.map + **/ + map(iterator: _.ListIterator, context?: any): _Chain; /** * Wrapped type `any[]`. * @see _.map **/ - map(iterator: _.ObjectIterator, context?: any): _Chain; + map(iterator: (element: T, key: string, list: any) => TArray[], context?: any): _ChainOfArrays; + //Not sure why this won't work, might be a TypeScript error? //map(iterator: _.ObjectIterator, context?: any): _ChainOfArrays; + /** + * Wrapped type `any[]`. + * @see _.map + **/ + map(iterator: _.ObjectIterator, context?: any): _Chain; /** * @see _.map @@ -2243,7 +2256,7 @@ interface _Chain { * Wrapped type `any[]`. * @see _.find **/ - find(iterator: _.ListIterator, context?: any): _Chain; + find(iterator: _.ListIterator, context?: any): _ChainSingle; /** * @see _.find @@ -2271,7 +2284,7 @@ interface _Chain { * Wrapped type `any[]`. * @see _.findWhere **/ - findWhere(properties: U): _Chain; + findWhere(properties: U): _ChainSingle; /** * Wrapped type `any[]`. @@ -2323,43 +2336,43 @@ interface _Chain { * Wrapped type `any[]`. * @see _.pluck **/ - pluck(propertyName: string): _Chain; + pluck(propertyName: string): _Chain; /** * Wrapped type `number[]`. * @see _.max **/ - max(): _Chain; + max(): _ChainSingle; /** * Wrapped type `any[]`. * @see _.max **/ - max(iterator: _.ListIterator, context?: any): _Chain; + max(iterator: _.ListIterator, context?: any): _ChainSingle; /** * Wrapped type `any[]`. * @see _.max **/ - max(iterator?: _.ListIterator, context?: any): _Chain; + max(iterator?: _.ListIterator, context?: any): _ChainSingle; /** * Wrapped type `number[]`. * @see _.min **/ - min(): _Chain; + min(): _ChainSingle; /** * Wrapped type `any[]`. * @see _.min **/ - min(iterator: _.ListIterator, context?: any): _Chain; + min(iterator: _.ListIterator, context?: any): _ChainSingle; /** * Wrapped type `any[]`. * @see _.min **/ - min(iterator?: _.ListIterator, context?: any): _Chain; + min(iterator?: _.ListIterator, context?: any): _ChainSingle; /** * Wrapped type `any[]`. @@ -2518,7 +2531,7 @@ interface _Chain { * Wrapped type `any`. * @see _.flatten **/ - flatten(shallow?: boolean): _Chain; + flatten(shallow?: boolean): _Chain; /** * Wrapped type `any[]`. @@ -2947,7 +2960,13 @@ interface _Chain { * Wrapped type `any`. * @see _.value **/ - value(): TResult; + value(): T[]; +} +interface _ChainSingle { + value(): T; +} +interface _ChainOfArrays extends _Chain { + flatten(): _Chain; } declare var _: UnderscoreStatic;