feat(QueryList): support index in callbacks

Closes #9278
This commit is contained in:
Pawel Kozlowski 2016-06-17 15:38:52 +02:00 committed by Victor Berchet
parent 6c389ed32f
commit 5fe60759f9
3 changed files with 37 additions and 15 deletions

View File

@ -40,22 +40,24 @@ export class QueryList<T> {
/**
* returns a new array with the passed in function applied to each element.
*/
map<U>(fn: (item: T) => U): U[] { return this._results.map(fn); }
map<U>(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<U>(fn: (acc: U, item: T) => U, init: U): U { return this._results.reduce(fn, init); }
reduce<U>(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

View File

@ -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');
});

View File

@ -414,14 +414,14 @@ const CORE = [
'ProviderBuilder.toValue(value:any):Provider',
'QueryList.changes:Observable<any>',
'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<T|any[]>):void',
'QueryList.setDirty():any',
'QueryList.toArray():T[]',