diff --git a/modules/@angular/core/src/linker/query_list.ts b/modules/@angular/core/src/linker/query_list.ts index 01534a924c..1d9506a270 100644 --- a/modules/@angular/core/src/linker/query_list.ts +++ b/modules/@angular/core/src/linker/query_list.ts @@ -40,22 +40,24 @@ export class QueryList { /** * returns a new array with the passed in function applied to each element. */ - map(fn: (item: T) => U): U[] { return this._results.map(fn); } + map(fn: (item: T, index?: number) => U): U[] { return this._results.map(fn); } /** * returns a filtered array. */ - filter(fn: (item: T) => boolean): T[] { return this._results.filter(fn); } + filter(fn: (item: T, index?: number) => boolean): T[] { return this._results.filter(fn); } /** * returns a reduced value. */ - reduce(fn: (acc: U, item: T) => U, init: U): U { return this._results.reduce(fn, init); } + reduce(fn: (acc: U, item: T, index?: number) => U, init: U): U { + return this._results.reduce(fn, init); + } /** * executes function for each element in a query. */ - forEach(fn: (item: T) => void): void { this._results.forEach(fn); } + forEach(fn: (item: T, index?: number) => void): void { this._results.forEach(fn); } /** * converts QueryList into an array diff --git a/modules/@angular/core/test/linker/query_list_spec.ts b/modules/@angular/core/test/linker/query_list_spec.ts index 0e607325bd..67c45f976e 100644 --- a/modules/@angular/core/test/linker/query_list_spec.ts +++ b/modules/@angular/core/test/linker/query_list_spec.ts @@ -40,6 +40,11 @@ export function main() { expect(queryList.map((x) => x)).toEqual(['one', 'two']); }); + it('should support map with index', () => { + queryList.reset(['one', 'two']); + expect(queryList.map((x, i) => `${x}_${i}`)).toEqual(['one_0', 'two_1']); + }); + it('should support forEach', () => { queryList.reset(['one', 'two']); let join = ''; @@ -47,25 +52,40 @@ export function main() { expect(join).toEqual('onetwo'); }); + it('should support forEach with index', () => { + queryList.reset(['one', 'two']); + let join = ''; + queryList.forEach((x, i) => join = join + x + i); + expect(join).toEqual('one0two1'); + }); + if (!IS_DART) { it('should support filter', () => { queryList.reset(['one', 'two']); - expect((<_JsQueryList>queryList).filter((x: any /** TODO #9100 */) => x == 'one')).toEqual([ - 'one' - ]); + expect((<_JsQueryList>queryList).filter((x: string) => x == 'one')).toEqual(['one']); + }); + + it('should support filter with index', () => { + queryList.reset(['one', 'two']); + expect((<_JsQueryList>queryList).filter((x: string, i: number) => i == 0)).toEqual(['one']); }); it('should support reduce', () => { queryList.reset(['one', 'two']); - expect((<_JsQueryList>queryList) - .reduce((a: any /** TODO #9100 */, x: any /** TODO #9100 */) => a + x, 'start:')) + expect((<_JsQueryList>queryList).reduce((a: string, x: string) => a + x, 'start:')) .toEqual('start:onetwo'); }); + it('should support reduce with index', () => { + queryList.reset(['one', 'two']); + expect((<_JsQueryList>queryList) + .reduce((a: string, x: string, i: number) => a + x + i, 'start:')) + .toEqual('start:one0two1'); + }); + it('should support toArray', () => { queryList.reset(['one', 'two']); - expect((<_JsQueryList>queryList) - .reduce((a: any /** TODO #9100 */, x: any /** TODO #9100 */) => a + x, 'start:')) + expect((<_JsQueryList>queryList).reduce((a: string, x: string) => a + x, 'start:')) .toEqual('start:onetwo'); }); diff --git a/tools/public_api_guard/public_api_spec.ts b/tools/public_api_guard/public_api_spec.ts index 9e961eaa14..49ae7c1d94 100644 --- a/tools/public_api_guard/public_api_spec.ts +++ b/tools/public_api_guard/public_api_spec.ts @@ -414,14 +414,14 @@ const CORE = [ 'ProviderBuilder.toValue(value:any):Provider', 'QueryList.changes:Observable', 'QueryList.dirty:any', - 'QueryList.filter(fn:(item: T) => boolean):T[]', + 'QueryList.filter(fn:(item: T, index?: number) => boolean):T[]', 'QueryList.first:T', - 'QueryList.forEach(fn:(item: T) => void):void', + 'QueryList.forEach(fn:(item: T, index?: number) => void):void', 'QueryList.last:T', 'QueryList.length:number', - 'QueryList.map(fn:(item: T) => U):U[]', + 'QueryList.map(fn:(item: T, index?: number) => U):U[]', 'QueryList.notifyOnChanges():void', - 'QueryList.reduce(fn:(acc: U, item: T) => U, init:U):U', + 'QueryList.reduce(fn:(acc: U, item: T, index?: number) => U, init:U):U', 'QueryList.reset(res:Array):void', 'QueryList.setDirty():any', 'QueryList.toArray():T[]',