feat(core): add the find method to QueryList
This commit is contained in:
@ -57,6 +57,12 @@ export class QueryList<T>/* implements Iterable<T> */ {
|
||||
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)
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user