1
0
This commit is contained in:
2025-05-24 12:11:44 +02:00
parent 9af08e71fc
commit c98e462142
24 changed files with 1702 additions and 688 deletions

View File

@@ -1,95 +1,17 @@
import { Comparer, MaybeAsyncComparer, AsyncComparer } from "./types.js";
export type Nullable<T> = T | null | undefined;
export function isDefined<T = any>(obj: T): obj is NonNullable<T> {
return obj !== undefined && obj !== null;
}
export function isIterable<T = any>(obj: any): obj is Iterable<T> {
return isDefined(obj) && typeof obj[Symbol.iterator] === "function";
}
export function isAsyncIterable<T = any>(obj: any): obj is AsyncIterable<T> {
return isDefined(obj) && typeof obj[Symbol.asyncIterator] === "function";
}
export function identity<T>(obj: T) {
return obj;
}
export function looseEquals<T>(a: T, b: T) {
return a == b;
}
export function strictEquals<T>(a: T, b: T) {
return a === b;
}
export function referenceEquals<T>(a: T, b: T) {
return Object.is(a, b);
}
export function numberCompare<T extends number | bigint>(a: T, b: T) {
return a - b;
}
export function operatorCompare(a: any, b: any) {
return a < b ? -1 : a > b ? 1 : 0;
}
export function defaultArrayComparer<T>(a: T, b: T) {
if (a === undefined) {
if (b === undefined) {
return 0;
}
return 1;
}
if (b === undefined) {
return -1;
}
const aStr = `${a}`, bStr = `${b}`;
return aStr > bStr ? 1 : aStr < bStr ? -1 : 0;
}
export function combineComparers<T>(first: Comparer<T>, second: Comparer<T>): Comparer<T> {
return (a, b) => first(a, b) || second(a, b);
}
export function combineAsyncComparers<T>(first: MaybeAsyncComparer<T>, second: MaybeAsyncComparer<T>): AsyncComparer<T> {
return async (a, b) => await first(a, b) || await second(a, b);
}
export function reverseComparer<T>(comparer: Comparer<T>): Comparer<T> {
return (a, b) => comparer(b, a);
}
export function reverseAsyncComparer<T>(comparer: MaybeAsyncComparer<T>): AsyncComparer<T> {
return async (a, b) => await comparer(b, a);
}
export function asArray<T>(iterable: Iterable<T>) {
return Array.isArray(iterable) ? <T[]>iterable : Array.from(iterable);
}
class WrappedIterator<T> implements Iterable<T> {
readonly #iterator: Iterator<T>;
constructor(iterator: Iterator<T>) {
this.#iterator = iterator;
}
[Symbol.iterator]() {
return this.#iterator;
}
}
export function asIterable<T>(iterator: Iterator<T>): Iterable<T> {
return isIterable<T>(iterator) ? iterator : new WrappedIterator(iterator);
}
class WrappedAsyncIterator<T> implements AsyncIterable<T> {
readonly #iterator: AsyncIterator<T>;
@@ -115,14 +37,14 @@ const _emptyIterableIterator = new class EmptyIterableIterator implements Iterab
return { done: true, value: undefined };
}
return(value?: any): IteratorResult<any, any> {
return(_value?: any): IteratorResult<any, any> {
throw new Error("Method not implemented.");
}
throw(e?: any): IteratorResult<any, any> {
throw(_e?: any): IteratorResult<any, any> {
throw new Error("Method not implemented.");
}
}
};
export function emptyIterableIterator<T>(): IterableIterator<T> {
return _emptyIterableIterator;