From a965589eb8f723abe6f8d11364134e0673d1ac0c Mon Sep 17 00:00:00 2001 From: Issei Horie Date: Mon, 4 May 2020 02:04:56 +0900 Subject: [PATCH] feat(core): adds get method to QueryList (#36907) This commit adds get method to QueryList. The method returns an item of the internal results by index number. PR Close #29467 PR Close #36907 --- goldens/public-api/core/core.d.ts | 1 + packages/core/src/linker/query_list.ts | 7 +++++++ packages/core/test/linker/query_list_spec.ts | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/goldens/public-api/core/core.d.ts b/goldens/public-api/core/core.d.ts index 5a861d8adb..712fd46125 100644 --- a/goldens/public-api/core/core.d.ts +++ b/goldens/public-api/core/core.d.ts @@ -756,6 +756,7 @@ export declare class QueryList implements Iterable { filter(fn: (item: T, index: number, array: T[]) => boolean): T[]; find(fn: (item: T, index: number, array: T[]) => boolean): T | undefined; forEach(fn: (item: T, index: number, array: T[]) => void): void; + get(index: number): T | undefined; map(fn: (item: T, index: number, array: T[]) => U): U[]; notifyOnChanges(): void; reduce(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U; diff --git a/packages/core/src/linker/query_list.ts b/packages/core/src/linker/query_list.ts index 2ada13c579..24f7f87b06 100644 --- a/packages/core/src/linker/query_list.ts +++ b/packages/core/src/linker/query_list.ts @@ -63,6 +63,13 @@ export class QueryList implements Iterable { if (!proto[symbol]) proto[symbol] = symbolIterator; } + /** + * Returns the QueryList entry at `index`. + */ + get(index: number): T|undefined { + return this._results[index]; + } + /** * See * [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) diff --git a/packages/core/test/linker/query_list_spec.ts b/packages/core/test/linker/query_list_spec.ts index 1952621096..f79b137548 100644 --- a/packages/core/test/linker/query_list_spec.ts +++ b/packages/core/test/linker/query_list_spec.ts @@ -51,6 +51,11 @@ import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testin expect(queryList.length).toEqual(2); }); + it('should support get', () => { + queryList.reset(['one', 'two']); + expect(queryList.get(1)).toEqual('two'); + }); + it('should support map', () => { queryList.reset(['one', 'two']); expect(queryList.map((x) => x)).toEqual(['one', 'two']);