feat(QueryList): implement some() (#9464)

closes #9443
This commit is contained in:
Victor Berchet 2016-06-22 13:13:31 -07:00 committed by GitHub
parent 3d5bb23184
commit f6a410a4a8
4 changed files with 67 additions and 55 deletions

View File

@ -2,8 +2,6 @@ import {EventEmitter, Observable} from '../facade/async';
import {ListWrapper} from '../facade/collection'; import {ListWrapper} from '../facade/collection';
import {getSymbolIterator} from '../facade/lang'; import {getSymbolIterator} from '../facade/lang';
/** /**
* An unmodifiable list of items that Angular keeps up to date when the state * An unmodifiable list of items that Angular keeps up to date when the state
* of the application changes. * of the application changes.
@ -27,47 +25,56 @@ import {getSymbolIterator} from '../facade/lang';
* ``` * ```
* @stable * @stable
*/ */
export class QueryList<T> { export class QueryList<T>/* implements Iterable<T> */ {
private _dirty = true; private _dirty = true;
private _results: Array<T> = []; private _results: Array<T> = [];
private _emitter = new EventEmitter(); private _emitter = new EventEmitter();
get changes(): Observable<any> { return this._emitter; } get changes(): Observable<any> { return this._emitter; }
get length(): number { return this._results.length; } get length(): number { return this._results.length; }
get first(): T { return ListWrapper.first(this._results); } get first(): T { return this._results[0]; }
get last(): T { return ListWrapper.last(this._results); } get last(): T { return this._results[this.length - 1]; }
/** /**
* returns a new array with the passed in function applied to each element. * See
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
*/ */
map<U>(fn: (item: T, index?: number) => U): U[] { return this._results.map(fn); } map<U>(fn: (item: T, index: number, array: T[]) => U): U[] { return this._results.map(fn); }
/** /**
* returns a filtered array. * See
* [Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
*/ */
filter(fn: (item: T, index?: number) => boolean): T[] { return this._results.filter(fn); } filter(fn: (item: T, index: number, array: T[]) => boolean): T[] {
return this._results.filter(fn);
}
/** /**
* returns a reduced value. * See
* [Array.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
*/ */
reduce<U>(fn: (acc: U, item: T, index?: number) => U, init: U): U { reduce<U>(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U {
return this._results.reduce(fn, init); return this._results.reduce(fn, init);
} }
/** /**
* executes function for each element in a query. * See
* [Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
*/ */
forEach(fn: (item: T, index?: number) => void): void { this._results.forEach(fn); } forEach(fn: (item: T, index: number, array: T[]) => void): void { this._results.forEach(fn); }
/** /**
* converts QueryList into an array * See
* [Array.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)
*/ */
toArray(): T[] { return ListWrapper.clone(this._results); } some(fn: (value: T, index: number, array: T[]) => boolean): boolean {
return this._results.some(fn);
[getSymbolIterator()](): any {
return (this._results as any /** TODO #???? */)[getSymbolIterator()]();
} }
toArray(): T[] { return this._results.slice(); }
[getSymbolIterator()](): Iterator<T> { return (this._results as any)[getSymbolIterator()](); }
toString(): string { return this._results.toString(); } toString(): string { return this._results.toString(); }
reset(res: Array<T|any[]>): void { reset(res: Array<T|any[]>): void {

View File

@ -1,7 +1,7 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit,} from '@angular/core/testing/testing_internal'; import {describe, it, expect, beforeEach, ddescribe, iit, xit,} from '@angular/core/testing/testing_internal';
import {fakeAsync, tick,} from '@angular/core/testing'; import {fakeAsync, tick,} from '@angular/core/testing';
import {MapWrapper, ListWrapper, iterateListLike} from '../../src/facade/collection'; import {iterateListLike} from '../../src/facade/collection';
import {IS_DART, StringWrapper} from '../../src/facade/lang'; import {StringWrapper} from '../../src/facade/lang';
import {ObservableWrapper} from '../../src/facade/async'; import {ObservableWrapper} from '../../src/facade/async';
import {QueryList} from '@angular/core/src/linker/query_list'; import {QueryList} from '@angular/core/src/linker/query_list';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
@ -59,7 +59,6 @@ export function main() {
expect(join).toEqual('one0two1'); expect(join).toEqual('one0two1');
}); });
if (!IS_DART) {
it('should support filter', () => { it('should support filter', () => {
queryList.reset(['one', 'two']); queryList.reset(['one', 'two']);
expect((<_JsQueryList>queryList).filter((x: string) => x == 'one')).toEqual(['one']); expect((<_JsQueryList>queryList).filter((x: string) => x == 'one')).toEqual(['one']);
@ -93,7 +92,6 @@ export function main() {
queryList.reset(['one', 'two']); queryList.reset(['one', 'two']);
expect((<_JsQueryList>queryList).toArray()).toEqual(['one', 'two']); expect((<_JsQueryList>queryList).toArray()).toEqual(['one', 'two']);
}); });
}
it('should support toString', () => { it('should support toString', () => {
queryList.reset(['one', 'two']); queryList.reset(['one', 'two']);
@ -108,6 +106,12 @@ export function main() {
expect(queryList.last).toEqual('three'); expect(queryList.last).toEqual('three');
}); });
it('should support some', () => {
queryList.reset(['one', 'two', 'three']);
expect(queryList.some(item => item === 'one')).toEqual(true);
expect(queryList.some(item => item === 'four')).toEqual(false);
});
if (getDOM().supportsDOMEvents()) { if (getDOM().supportsDOMEvents()) {
describe('simple observable interface', () => { describe('simple observable interface', () => {
it('should fire callbacks on change', fakeAsync(() => { it('should fire callbacks on change', fakeAsync(() => {

View File

@ -266,7 +266,7 @@ export class ListWrapper {
} }
static flatten<T>(list: Array<T|T[]>): T[] { static flatten<T>(list: Array<T|T[]>): T[] {
var target: any[] /** TODO #???? */ = []; var target: any[] = [];
_flattenArray(list, target); _flattenArray(list, target);
return target; return target;
} }

View File

@ -417,16 +417,17 @@ const CORE = [
'ProviderBuilder.toValue(value:any):Provider', 'ProviderBuilder.toValue(value:any):Provider',
'QueryList.changes:Observable<any>', 'QueryList.changes:Observable<any>',
'QueryList.dirty:any', 'QueryList.dirty:any',
'QueryList.filter(fn:(item: T, index?: number) => boolean):T[]', 'QueryList.filter(fn:(item: T, index: number, array: T[]) => boolean):T[]',
'QueryList.first:T', 'QueryList.first:T',
'QueryList.forEach(fn:(item: T, index?: number) => void):void', 'QueryList.forEach(fn:(item: T, index: number, array: T[]) => void):void',
'QueryList.last:T', 'QueryList.last:T',
'QueryList.length:number', 'QueryList.length:number',
'QueryList.map(fn:(item: T, index?: number) => U):U[]', 'QueryList.map(fn:(item: T, index: number, array: T[]) => U):U[]',
'QueryList.notifyOnChanges():void', 'QueryList.notifyOnChanges():void',
'QueryList.reduce(fn:(acc: U, item: T, index?: number) => U, init:U):U', 'QueryList.reduce(fn:(prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init:U):U',
'QueryList.reset(res:Array<T|any[]>):void', 'QueryList.reset(res:Array<T|any[]>):void',
'QueryList.setDirty():any', 'QueryList.setDirty():any',
'QueryList.some(fn:(value: T, index: number, array: T[]) => boolean):boolean',
'QueryList.toArray():T[]', 'QueryList.toArray():T[]',
'QueryList.toString():string', 'QueryList.toString():string',
'QueryList<T>', 'QueryList<T>',