From 7c16ef942e135ea64a69ce8025c75c140c113d83 Mon Sep 17 00:00:00 2001 From: Thomas Shafer Date: Wed, 26 Oct 2016 16:14:34 -0700 Subject: [PATCH] feat(core): add the find method to QueryList --- modules/@angular/core/src/linker/query_list.ts | 6 ++++++ modules/@angular/core/test/linker/query_list_spec.ts | 10 ++++++++++ tools/public_api_guard/core/index.d.ts | 1 + 3 files changed, 17 insertions(+) diff --git a/modules/@angular/core/src/linker/query_list.ts b/modules/@angular/core/src/linker/query_list.ts index 8657ff9ac8..708ceffac1 100644 --- a/modules/@angular/core/src/linker/query_list.ts +++ b/modules/@angular/core/src/linker/query_list.ts @@ -57,6 +57,12 @@ export class QueryList/* implements Iterable */ { 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 * [Array.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) diff --git a/modules/@angular/core/test/linker/query_list_spec.ts b/modules/@angular/core/test/linker/query_list_spec.ts index 5e88356c35..03f7b90805 100644 --- a/modules/@angular/core/test/linker/query_list_spec.ts +++ b/modules/@angular/core/test/linker/query_list_spec.ts @@ -70,6 +70,16 @@ export function main() { 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', () => { queryList.reset(['one', 'two']); expect(queryList.reduce((a: string, x: string) => a + x, 'start:')).toEqual('start:onetwo'); diff --git a/tools/public_api_guard/core/index.d.ts b/tools/public_api_guard/core/index.d.ts index c06424959d..ba0609543f 100644 --- a/tools/public_api_guard/core/index.d.ts +++ b/tools/public_api_guard/core/index.d.ts @@ -697,6 +697,7 @@ export declare class QueryList { last: T; length: number; 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; map(fn: (item: T, index: number, array: T[]) => U): U[]; notifyOnChanges(): void;