Skip to content

Commit bfbf18d

Browse files
committed
feat(query): add filter and reduce to QueryList
Closes angular#4710
1 parent 9fc24b9 commit bfbf18d

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

modules/angular2/src/core/linker/query_list.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ class QueryList<T> extends Object
2222
return _results.toString();
2323
}
2424

25-
List map(fn(T)) {
26-
// Note: we need to return a list instead of iterable to match JS.
27-
return this._results.map(fn).toList();
28-
}
29-
3025
/** @internal */
3126
void reset(List<T> newList) {
3227
_results = newList;

modules/angular2/src/core/linker/query_list.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,25 @@ export class QueryList<T> {
3737
get last(): T { return ListWrapper.last(this._results); }
3838

3939
/**
40-
* returns a new list with the passsed in function applied to each element.
40+
* returns a new array with the passed in function applied to each element.
4141
*/
4242
map<U>(fn: (item: T) => U): U[] { return this._results.map(fn); }
4343

44+
/**
45+
* returns a filtered array.
46+
*/
47+
filter(fn: (item: T) => boolean): T[] { return this._results.filter(fn); }
48+
49+
/**
50+
* returns a reduced value.
51+
*/
52+
reduce<U>(fn: (acc: U, item: T) => U, init: U): U { return this._results.reduce(fn, init); }
53+
54+
/**
55+
* converts QueryList into an array
56+
*/
57+
toArray(): T[] { return ListWrapper.clone(this._results); }
58+
4459
[getSymbolIterator()](): any { return this._results[getSymbolIterator()](); }
4560

4661
toString(): string { return this._results.toString(); }

modules/angular2/test/core/linker/query_list_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
fakeAsync,
1111
tick
1212
} from 'angular2/testing_internal';
13+
import {IS_DART} from '../../platform';
1314

1415
import {MapWrapper, ListWrapper, iterateListLike} from 'angular2/src/core/facade/collection';
1516
import {StringWrapper} from 'angular2/src/core/facade/lang';
@@ -46,6 +47,28 @@ export function main() {
4647
expect(queryList.map((x) => x)).toEqual(['one', 'two']);
4748
});
4849

50+
if (!IS_DART) {
51+
it('should support filter', () => {
52+
queryList.reset(['one', 'two']);
53+
expect((<any>queryList).filter((x) => x == "one")).toEqual(['one']);
54+
});
55+
56+
it('should support reduce', () => {
57+
queryList.reset(["one", "two"]);
58+
expect((<any>queryList).reduce((a, x) => a + x, "start:")).toEqual("start:onetwo");
59+
});
60+
61+
it('should support toArray', () => {
62+
queryList.reset(["one", "two"]);
63+
expect((<any>queryList).reduce((a, x) => a + x, "start:")).toEqual("start:onetwo");
64+
});
65+
66+
it('should support toArray', () => {
67+
queryList.reset(["one", "two"]);
68+
expect((<any>queryList).toArray()).toEqual(["one", "two"]);
69+
});
70+
}
71+
4972
it('should support toString', () => {
5073
queryList.reset(['one', 'two']);
5174
var listString = queryList.toString();

0 commit comments

Comments
 (0)