feat(core): add the find method to QueryList

This commit is contained in:
Thomas Shafer 2016-10-26 16:14:34 -07:00 committed by vsavkin
parent a318b57257
commit 7c16ef942e
3 changed files with 17 additions and 0 deletions

View File

@ -57,6 +57,12 @@ export class QueryList<T>/* implements Iterable<T> */ {
return this._results.filter(fn); return this._results.filter(fn);
} }
/**
* See
* [Array.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
*/
find(fn: (item: T, index: number, array: T[]) => boolean): T { return this._results.find(fn); }
/** /**
* See * See
* [Array.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) * [Array.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

View File

@ -70,6 +70,16 @@ export function main() {
expect(queryList.filter((x: string, i: number) => i == 0)).toEqual(['one']); expect(queryList.filter((x: string, i: number) => i == 0)).toEqual(['one']);
}); });
it('should support find', () => {
queryList.reset(['one', 'two']);
expect(queryList.find((x: string) => x == 'two')).toEqual('two');
});
it('should support find with index', () => {
queryList.reset(['one', 'two']);
expect(queryList.find((x: string, i: number) => i == 1)).toEqual('two');
});
it('should support reduce', () => { it('should support reduce', () => {
queryList.reset(['one', 'two']); queryList.reset(['one', 'two']);
expect(queryList.reduce((a: string, x: string) => a + x, 'start:')).toEqual('start:onetwo'); expect(queryList.reduce((a: string, x: string) => a + x, 'start:')).toEqual('start:onetwo');

View File

@ -697,6 +697,7 @@ export declare class QueryList<T> {
last: T; last: T;
length: number; length: number;
filter(fn: (item: T, index: number, array: T[]) => boolean): T[]; filter(fn: (item: T, index: number, array: T[]) => boolean): T[];
find(fn: (item: T, index: number, array: T[]) => boolean): T;
forEach(fn: (item: T, index: number, array: T[]) => void): void; forEach(fn: (item: T, index: number, array: T[]) => void): void;
map<U>(fn: (item: T, index: number, array: T[]) => U): U[]; map<U>(fn: (item: T, index: number, array: T[]) => U): U[];
notifyOnChanges(): void; notifyOnChanges(): void;