diff --git a/src/async/impl.ts b/src/async/impl.ts index 5ef65ca..8c25e07 100644 --- a/src/async/impl.ts +++ b/src/async/impl.ts @@ -11,7 +11,7 @@ import { AsyncRandomOptions } from "../random/types.js"; import { selectionSorter } from "../sorting.js"; import { Sequence } from "../sync/types.js"; import { MaybeAsyncAnyPredicate, MaybeAsyncConverter, MaybeAsyncBiConverter, MaybeAsyncAccumulator, MaybeAsyncAction, MaybePromiseLike, MaybeAsyncGenerator, MaybePromise, MaybeAsyncIterable, MaybeAsyncTypePredicate } from "../types.js"; -import { asAsyncIterable } from "../utils.js"; +import { asAsyncIterable, FindElementResult } from "../utils.js"; import { array, empty, wrap } from "./index.js"; import { AsyncSequence, AsyncSequencePipeline, GroupedAsyncSequence, OrderedAsyncSequence } from "./types.js"; @@ -173,14 +173,14 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return n >= 0 ? n : Infinity; } - async #tryGetFirst(predicate?: MaybeAsyncAnyPredicate) { + async #tryGetFirst(predicate?: MaybeAsyncAnyPredicate): Promise> { if (predicate) { for await (const element of this) { if (await predicate(element)) { return { found: true, element - } as const; + }; } } } else { @@ -190,13 +190,13 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found: true, element: next.value - } as const; + }; } } return { found: false - } as const; + }; } async first(predicate?: MaybeAsyncAnyPredicate) { @@ -215,7 +215,7 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return result.found ? result.element : def; } - async #tryGetLast(predicate?: MaybeAsyncAnyPredicate) { + async #tryGetLast(predicate?: MaybeAsyncAnyPredicate): Promise> { let found = false; let result: TElement | undefined = undefined; @@ -236,14 +236,14 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found, element: result - }; + } as FindElementResult; } async last(predicate?: MaybeAsyncAnyPredicate) { const result = await this.#tryGetLast(predicate); if (result.found) { - return result.element!; + return result.element; } throw new Error("No element was found."); @@ -255,9 +255,9 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return result.found ? result.element : def; } - async #tryGetSingle(predicate?: MaybeAsyncAnyPredicate) { + async #tryGetSingle(predicate?: MaybeAsyncAnyPredicate): Promise> { if (predicate) { - let result: { found: true; element: TElement; } | undefined = undefined; + let result: FindElementResult | undefined = undefined; for await (const element of this) { if (await predicate(element)) { @@ -265,13 +265,13 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found: false, reason: 2 - } as const; + }; } result = { found: true, element - } as const; + }; } } } else { @@ -279,10 +279,10 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im let next = await iterator.next(); if (!next.done) { - const result = { + const result: FindElementResult = { found: true, element: next.value - } as const; + }; next = await iterator.next(); @@ -293,20 +293,20 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found: false, reason: 2 - } as const; + }; } } return { found: false, reason: 1 - } as const; + }; } async single(predicate?: MaybeAsyncAnyPredicate) { const result = await this.#tryGetSingle(predicate); - if (result.found == true) { + if (result.found) { return result.element; } @@ -330,7 +330,7 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return result.found ? result.element : def; } - async #tryElementAt(index: number) { + async #tryElementAt(index: number): Promise> { let i = index; for await (const element of this) { @@ -338,7 +338,7 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found: true, element - } as const; + }; } i--; @@ -346,7 +346,7 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return { found: false - } as const; + }; } async elementAt(index: number) { @@ -693,18 +693,12 @@ export abstract class BaseAsyncSequence extends AsyncSequenceMarker im return new CacheAsyncSequence(this); } - async asArray(): Promise { - return await this.toArray(); + async asArray() { + return await Array.fromAsync(this); } async toArray() { - const array: TElement[] = []; - - for await (const element of this) { - array.push(element); - } - - return array; + return await Array.fromAsync(this); } toMap(keySelector: MaybeAsyncConverter): Promise>; @@ -1137,13 +1131,9 @@ abstract class BaseOrderedAsyncSequence extends BaseAsyncSequence extends SequenceMarker implements S return n >= 0 ? n : Infinity; } - #tryGetFirst(predicate?: AnyPredicate): { found: boolean, element?: TElement | undefined; } { + #tryGetFirst(predicate?: AnyPredicate): FindElementResult { if (predicate) { for (const element of this) { if (predicate(element)) { @@ -211,7 +212,7 @@ export abstract class BaseSequence extends SequenceMarker implements S const result = this.#tryGetFirst(predicate); if (result.found) { - return result.element!; + return result.element; } throw new Error("No element was found."); @@ -223,7 +224,7 @@ export abstract class BaseSequence extends SequenceMarker implements S return result.found ? result.element : def; } - #tryGetLast(predicate?: AnyPredicate): { found: boolean, element?: TElement; } { + #tryGetLast(predicate?: AnyPredicate): FindElementResult { let found = false; let result: TElement | undefined = undefined; @@ -244,14 +245,14 @@ export abstract class BaseSequence extends SequenceMarker implements S return { found, element: result - }; + } as FindElementResult; } last(predicate?: AnyPredicate) { const result = this.#tryGetLast(predicate); if (result.found) { - return result.element!; + return result.element; } throw new Error("No element was found."); @@ -263,9 +264,9 @@ export abstract class BaseSequence extends SequenceMarker implements S return result.found ? result.element : def; } - #tryGetSingle(predicate?: AnyPredicate): { found: boolean, element?: TElement, reason?: number; } { + #tryGetSingle(predicate?: AnyPredicate): FindElementResult { if (predicate) { - let result: { found: boolean; element: TElement; } | undefined = undefined; + let result: FindElementResult | undefined = undefined; for (const element of this) { if (predicate(element)) { @@ -287,7 +288,7 @@ export abstract class BaseSequence extends SequenceMarker implements S let next = iterator.next(); if (!next.done) { - const result = { + const result: FindElementResult = { found: true, element: next.value }; @@ -315,11 +316,11 @@ export abstract class BaseSequence extends SequenceMarker implements S const result = this.#tryGetSingle(predicate); if (result.found) { - return result.element!; + return result.element; } let reason: string; - switch (result.reason!) { + switch (result.reason) { case 1: reason = "No element was found."; break; @@ -338,7 +339,7 @@ export abstract class BaseSequence extends SequenceMarker implements S return result.found ? result.element : def; } - #tryElementAt(index: number) { + #tryElementAt(index: number): FindElementResult { let i = index; for (const element of this) { @@ -346,7 +347,7 @@ export abstract class BaseSequence extends SequenceMarker implements S return { found: true, element - } as const; + }; } i--; @@ -354,7 +355,7 @@ export abstract class BaseSequence extends SequenceMarker implements S return { found: false - } as const; + }; } elementAt(index: number) { @@ -1164,11 +1165,7 @@ export abstract class BaseOrderedSequence extends BaseSequence(): IterableIterator { return _emptyIterableIterator; } + +type FindElementSuccess = { + found: true; + element: T; +}; +type FindElementFail = { + found: false; + reason?: number; +}; +export type FindElementResult = FindElementSuccess | FindElementFail;