improve typing of element finding utils
This commit is contained in:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user