change project structure to remove namespaces
This commit is contained in:
101
src/async/index.ts
Normal file
101
src/async/index.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { wrap as wrapSync } from "../sync/index.js";
|
||||
import { Enumerable } from "../sync/types.js";
|
||||
import { MaybeAsyncIterable } from "../types.js";
|
||||
import { isAsyncIterable } from "../utils.js";
|
||||
import { WrappedEnumerable, WrappedAsyncIterable, EmptyAsyncEnumerable, WrappedObjectAsync, WrappedArrayAsync, WrappedArrayLikeAsync, FunctionAsyncEnumerable, GeneratorAsyncEnumerable, RangeAsyncEnumerable, RepeatForeverAsyncEnumerable, RepeatAsyncEnumerable, AsyncEnumerableMarker } from "./impl.js";
|
||||
import { AsyncEnumerable } from "./types.js";
|
||||
|
||||
export function asAsync<T>(enumerable: Enumerable<T>): AsyncEnumerable<T> {
|
||||
return new WrappedEnumerable(enumerable);
|
||||
}
|
||||
|
||||
export function wrap<T>(iterable: MaybeAsyncIterable<T>): AsyncEnumerable<T> {
|
||||
if (isAsyncEnumerable<T>(iterable)) {
|
||||
return iterable;
|
||||
}
|
||||
|
||||
if (isAsyncIterable(iterable)) {
|
||||
return sequence(iterable);
|
||||
}
|
||||
|
||||
return asAsync(wrapSync(iterable));
|
||||
}
|
||||
|
||||
export function sequence<T>(iterable: AsyncIterable<T>): AsyncEnumerable<T> {
|
||||
return new WrappedAsyncIterable(iterable);
|
||||
}
|
||||
|
||||
export function empty<T>(): AsyncEnumerable<T> {
|
||||
return EmptyAsyncEnumerable.INSTANCE;
|
||||
}
|
||||
|
||||
export function single<T>(obj: T | PromiseLike<T>): AsyncEnumerable<T> {
|
||||
return new WrappedObjectAsync(obj);
|
||||
}
|
||||
|
||||
export function array<T>(array: (T | PromiseLike<T>)[]): AsyncEnumerable<T> {
|
||||
return new WrappedArrayAsync(array);
|
||||
}
|
||||
|
||||
export function arrayLike<T>(arrayLike: ArrayLike<(T | PromiseLike<T>)>): AsyncEnumerable<T> {
|
||||
return new WrappedArrayLikeAsync(arrayLike);
|
||||
}
|
||||
|
||||
export function of<T>(...elements: (T | PromiseLike<T>)[]): AsyncEnumerable<T> {
|
||||
switch (elements.length) {
|
||||
case 0:
|
||||
return empty();
|
||||
case 1:
|
||||
return single(elements[0]);
|
||||
default:
|
||||
return array(elements);
|
||||
}
|
||||
}
|
||||
|
||||
export function func<T>(f: () => Promise<T>): AsyncEnumerable<T> {
|
||||
return new FunctionAsyncEnumerable(f);
|
||||
}
|
||||
|
||||
export function generator<T>(generator: () => AsyncGenerator<T>): AsyncEnumerable<T> {
|
||||
return new GeneratorAsyncEnumerable(generator);
|
||||
}
|
||||
|
||||
export function range(max: number): AsyncEnumerable<number>
|
||||
export function range(min: number, max: number): AsyncEnumerable<number>
|
||||
export function range(min: number, max: number, step: number): AsyncEnumerable<number>
|
||||
export function range(a: number, b?: number, c?: number): AsyncEnumerable<number> {
|
||||
if (b === undefined) {
|
||||
b = a;
|
||||
a = 0;
|
||||
}
|
||||
|
||||
if (c === undefined) {
|
||||
c = 1;
|
||||
}
|
||||
|
||||
return new RangeAsyncEnumerable(a, b, c);
|
||||
}
|
||||
|
||||
export function repeat<T>(value: T, count?: number): AsyncEnumerable<T> {
|
||||
if (count == undefined) {
|
||||
return new RepeatForeverAsyncEnumerable(value);
|
||||
}
|
||||
|
||||
if (count < 0) {
|
||||
throw new Error("count < 0");
|
||||
}
|
||||
|
||||
if (count === 0) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
return new WrappedObjectAsync(value);
|
||||
}
|
||||
|
||||
return new RepeatAsyncEnumerable(value, count);
|
||||
}
|
||||
|
||||
export function isAsyncEnumerable<T = any>(obj: any): obj is AsyncEnumerable<T> {
|
||||
return obj instanceof AsyncEnumerableMarker;
|
||||
}
|
||||
Reference in New Issue
Block a user