1
0

initial commit

This commit is contained in:
2024-05-04 01:19:44 +02:00
commit a172e6a50f
16 changed files with 6514 additions and 0 deletions

121
src/utils.ts Normal file
View File

@@ -0,0 +1,121 @@
import { AsyncComparer, MaybeAsyncComparer, MaybeAsyncIterator } from "./async.js";
import { Comparer } from "./sync.js";
export type AsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => Promise<Awaited<R>> : never;
export type MaybePromise<T> = T | Promise<T>;
export type MaybeAsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => MaybePromise<R> : never;
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 isEquals<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 === null ? "null" : a.toString();
const bStr = b === null ? "null" : b.toString();
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: T, b: T) => comparer(b, a);
}
export function reverseAsyncComparer<T>(comparer: MaybeAsyncComparer<T>): AsyncComparer<T> {
return async (a: T, b: T) => await comparer(b, a);
}
export function* asGenerator<T>(iterator: Iterator<T>) {
while (true) {
const next = iterator.next();
if (next.done) {
break;
}
yield next.value;
}
}
export async function* asAsyncGenerator<T>(iterator: MaybeAsyncIterator<T>) {
while (true) {
const next = await iterator.next();
if (next.done) {
break;
}
yield next.value;
}
}
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 wrapAsIterable<T>(iterator: Iterator<T>): Iterable<T> {
return new WrappedIterator(iterator);
}