From bfbf18d98337495f23f8887bc10d25c8b79d78c5 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 13 Oct 2015 16:50:46 -0700 Subject: [PATCH] feat(query): add filter and reduce to QueryList Closes #4710 --- .../angular2/src/core/linker/query_list.dart | 5 ---- .../angular2/src/core/linker/query_list.ts | 17 +++++++++++++- .../test/core/linker/query_list_spec.ts | 23 +++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/modules/angular2/src/core/linker/query_list.dart b/modules/angular2/src/core/linker/query_list.dart index 0ec6c8212bc5d..298a5bffa2ffc 100644 --- a/modules/angular2/src/core/linker/query_list.dart +++ b/modules/angular2/src/core/linker/query_list.dart @@ -22,11 +22,6 @@ class QueryList extends Object return _results.toString(); } - List map(fn(T)) { - // Note: we need to return a list instead of iterable to match JS. - return this._results.map(fn).toList(); - } - /** @internal */ void reset(List newList) { _results = newList; diff --git a/modules/angular2/src/core/linker/query_list.ts b/modules/angular2/src/core/linker/query_list.ts index 469daafc17ae1..cc80d2c8bc15d 100644 --- a/modules/angular2/src/core/linker/query_list.ts +++ b/modules/angular2/src/core/linker/query_list.ts @@ -37,10 +37,25 @@ export class QueryList { get last(): T { return ListWrapper.last(this._results); } /** - * returns a new list with the passsed in function applied to each element. + * returns a new array with the passed in function applied to each element. */ map(fn: (item: T) => U): U[] { return this._results.map(fn); } + /** + * returns a filtered array. + */ + filter(fn: (item: T) => 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); } + + /** + * converts QueryList into an array + */ + toArray(): T[] { return ListWrapper.clone(this._results); } + [getSymbolIterator()](): any { return this._results[getSymbolIterator()](); } toString(): string { return this._results.toString(); } diff --git a/modules/angular2/test/core/linker/query_list_spec.ts b/modules/angular2/test/core/linker/query_list_spec.ts index 558d1124049a1..b6d46b99173db 100644 --- a/modules/angular2/test/core/linker/query_list_spec.ts +++ b/modules/angular2/test/core/linker/query_list_spec.ts @@ -10,6 +10,7 @@ import { fakeAsync, tick } from 'angular2/testing_internal'; +import {IS_DART} from '../../platform'; import {MapWrapper, ListWrapper, iterateListLike} from 'angular2/src/core/facade/collection'; import {StringWrapper} from 'angular2/src/core/facade/lang'; @@ -46,6 +47,28 @@ export function main() { expect(queryList.map((x) => x)).toEqual(['one', 'two']); }); + if (!IS_DART) { + it('should support filter', () => { + queryList.reset(['one', 'two']); + expect((queryList).filter((x) => x == "one")).toEqual(['one']); + }); + + it('should support reduce', () => { + queryList.reset(["one", "two"]); + expect((queryList).reduce((a, x) => a + x, "start:")).toEqual("start:onetwo"); + }); + + it('should support toArray', () => { + queryList.reset(["one", "two"]); + expect((queryList).reduce((a, x) => a + x, "start:")).toEqual("start:onetwo"); + }); + + it('should support toArray', () => { + queryList.reset(["one", "two"]); + expect((queryList).toArray()).toEqual(["one", "two"]); + }); + } + it('should support toString', () => { queryList.reset(['one', 'two']); var listString = queryList.toString();