1
0

improve typing of element finding utils

This commit is contained in:
2025-05-24 13:22:09 +02:00
parent 75219bc716
commit afa4c8a684
3 changed files with 50 additions and 53 deletions

View File

@@ -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<TElement> extends AsyncSequenceMarker im
return n >= 0 ? n : Infinity;
}
async #tryGetFirst(predicate?: MaybeAsyncAnyPredicate<TElement>) {
async #tryGetFirst(predicate?: MaybeAsyncAnyPredicate<TElement>): Promise<FindElementResult<TElement>> {
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<TElement> extends AsyncSequenceMarker im
return {
found: true,
element: next.value
} as const;
};
}
}
return {
found: false
} as const;
};
}
async first(predicate?: MaybeAsyncAnyPredicate<TElement>) {
@@ -215,7 +215,7 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return result.found ? result.element : def;
}
async #tryGetLast(predicate?: MaybeAsyncAnyPredicate<TElement>) {
async #tryGetLast(predicate?: MaybeAsyncAnyPredicate<TElement>): Promise<FindElementResult<TElement>> {
let found = false;
let result: TElement | undefined = undefined;
@@ -236,14 +236,14 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return {
found,
element: result
};
} as FindElementResult<TElement>;
}
async last(predicate?: MaybeAsyncAnyPredicate<TElement>) {
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<TElement> extends AsyncSequenceMarker im
return result.found ? result.element : def;
}
async #tryGetSingle(predicate?: MaybeAsyncAnyPredicate<TElement>) {
async #tryGetSingle(predicate?: MaybeAsyncAnyPredicate<TElement>): Promise<FindElementResult<TElement>> {
if (predicate) {
let result: { found: true; element: TElement; } | undefined = undefined;
let result: FindElementResult<TElement> | undefined = undefined;
for await (const element of this) {
if (await predicate(element)) {
@@ -265,13 +265,13 @@ export abstract class BaseAsyncSequence<TElement> 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<TElement> extends AsyncSequenceMarker im
let next = await iterator.next();
if (!next.done) {
const result = {
const result: FindElementResult<TElement> = {
found: true,
element: next.value
} as const;
};
next = await iterator.next();
@@ -293,20 +293,20 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return {
found: false,
reason: 2
} as const;
};
}
}
return {
found: false,
reason: 1
} as const;
};
}
async single(predicate?: MaybeAsyncAnyPredicate<TElement>) {
const result = await this.#tryGetSingle(predicate);
if (result.found == true) {
if (result.found) {
return result.element;
}
@@ -330,7 +330,7 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return result.found ? result.element : def;
}
async #tryElementAt(index: number) {
async #tryElementAt(index: number): Promise<FindElementResult<TElement>> {
let i = index;
for await (const element of this) {
@@ -338,7 +338,7 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return {
found: true,
element
} as const;
};
}
i--;
@@ -346,7 +346,7 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return {
found: false
} as const;
};
}
async elementAt(index: number) {
@@ -693,18 +693,12 @@ export abstract class BaseAsyncSequence<TElement> extends AsyncSequenceMarker im
return new CacheAsyncSequence<TElement>(this);
}
async asArray(): Promise<TElement[]> {
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<TKey>(keySelector: MaybeAsyncConverter<TElement, TKey>): Promise<Map<TKey, TElement>>;
@@ -1137,13 +1131,9 @@ abstract class BaseOrderedAsyncSequence<TElement> extends BaseAsyncSequence<TEle
}
override async *iterator() {
const arr: TElement[] = [];
const arr = await Array.fromAsync(this.#sequence);
for await (const obj of this.#sequence) {
arr.push(obj);
}
await selectionSorter.sort(arr, this.#descending, this.#sorter.comparison());
await selectionSorter.sort(arr, this.#descending, this.#sorter);
yield* arr;
}

View File

@@ -11,6 +11,7 @@ import { createQueue } from "../queue.js";
import { getRandomElement } from "../random/index.js";
import { RandomOptions } from "../random/types.js";
import { AnyPredicate, Converter, TypePredicate, BiConverter, Accumulator, Action } from "../types.js";
import { FindElementResult } from "../utils.js";
import { array, empty, wrap } from "./index.js";
import { Sequence, GroupedSequence, OrderedSequence, SequencePipeline } from "./types.js";
@@ -181,7 +182,7 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
return n >= 0 ? n : Infinity;
}
#tryGetFirst(predicate?: AnyPredicate<TElement>): { found: boolean, element?: TElement | undefined; } {
#tryGetFirst(predicate?: AnyPredicate<TElement>): FindElementResult<TElement> {
if (predicate) {
for (const element of this) {
if (predicate(element)) {
@@ -211,7 +212,7 @@ export abstract class BaseSequence<TElement> 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<TElement> extends SequenceMarker implements S
return result.found ? result.element : def;
}
#tryGetLast(predicate?: AnyPredicate<TElement>): { found: boolean, element?: TElement; } {
#tryGetLast(predicate?: AnyPredicate<TElement>): FindElementResult<TElement> {
let found = false;
let result: TElement | undefined = undefined;
@@ -244,14 +245,14 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
return {
found,
element: result
};
} as FindElementResult<TElement>;
}
last(predicate?: AnyPredicate<TElement>) {
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<TElement> extends SequenceMarker implements S
return result.found ? result.element : def;
}
#tryGetSingle(predicate?: AnyPredicate<TElement>): { found: boolean, element?: TElement, reason?: number; } {
#tryGetSingle(predicate?: AnyPredicate<TElement>): FindElementResult<TElement> {
if (predicate) {
let result: { found: boolean; element: TElement; } | undefined = undefined;
let result: FindElementResult<TElement> | undefined = undefined;
for (const element of this) {
if (predicate(element)) {
@@ -287,7 +288,7 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
let next = iterator.next();
if (!next.done) {
const result = {
const result: FindElementResult<TElement> = {
found: true,
element: next.value
};
@@ -315,11 +316,11 @@ export abstract class BaseSequence<TElement> 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<TElement> extends SequenceMarker implements S
return result.found ? result.element : def;
}
#tryElementAt(index: number) {
#tryElementAt(index: number): FindElementResult<TElement> {
let i = index;
for (const element of this) {
@@ -346,7 +347,7 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
return {
found: true,
element
} as const;
};
}
i--;
@@ -354,7 +355,7 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
return {
found: false
} as const;
};
}
elementAt(index: number) {
@@ -1164,11 +1165,7 @@ export abstract class BaseOrderedSequence<TElement> extends BaseSequence<TElemen
}
override *iterator() {
const arr: TElement[] = [];
for (const obj of this.#sequence) {
arr.push(obj);
}
const arr = Array.from(this.#sequence);
if (this.#sorter) {
arr.sort((this.#descending ? this.#sorter.reverse() : this.#sorter).comparison());

View File

@@ -49,3 +49,13 @@ const _emptyIterableIterator = new class EmptyIterableIterator implements Iterab
export function emptyIterableIterator<T>(): IterableIterator<T> {
return _emptyIterableIterator;
}
type FindElementSuccess<T> = {
found: true;
element: T;
};
type FindElementFail = {
found: false;
reason?: number;
};
export type FindElementResult<T> = FindElementSuccess<T> | FindElementFail;