Skip to content

Commit

Permalink
feat(core): adds get method to QueryList (#36907)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Issei Horie authored and AndrewKushnir committed Nov 19, 2020
1 parent ce604b9 commit a965589
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions goldens/public-api/core/core.d.ts
Expand Up @@ -756,6 +756,7 @@ export declare class QueryList<T> implements Iterable<T> {
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<U>(fn: (item: T, index: number, array: T[]) => U): U[];
notifyOnChanges(): void;
reduce<U>(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U;
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/linker/query_list.ts
Expand Up @@ -63,6 +63,13 @@ export class QueryList<T> implements Iterable<T> {
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)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/linker/query_list_spec.ts
Expand Up @@ -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']);
Expand Down

0 comments on commit a965589

Please sign in to comment.