export type Awaitable = T | Promise; export type ForAwaitable = Iterable | AsyncIterable; export type { ForAwaitable as ForOfAwaitable } export const isIterable = (x: unknown): x is Iterable => x != null && typeof x === 'object' && Symbol.iterator in x export const isAsyncIterable = (x: unknown): x is AsyncIterable => x != null && typeof x === 'object' && Symbol.asyncIterator in x export const isForAwaitable = (x: unknown): x is ForAwaitable => x != null && typeof x === 'object' && (Symbol.asyncIterator in x || Symbol.iterator in x) /** * Alternates items from the first and second iterable in the output iterable, until either input runs out of items. */ export function* interleave(xs: Iterable, ys: Iterable): IterableIterator { const itx = xs[Symbol.iterator](); const ity = ys[Symbol.iterator](); while (true) { const rx = itx.next(); if (rx.done) break; else yield rx.value; const ry = ity.next(); if (ry.done) break; else yield ry.value; } } /** * It's like interleave, but will flatten items of the second (async) iterable. */ export async function* aInterleaveFlattenSecond(xs: Iterable, ys: Iterable>): AsyncIterableIterator { const itx = xs[Symbol.iterator](); const ity = ys[Symbol.iterator](); while (true) { const rx = itx.next(); if (rx.done) break; else yield rx.value; const ry = ity.next(); if (ry.done) break; else yield* ry.value; } } export function* map(iterable: Iterable, f: (a: A) => B): IterableIterator { for (const x of iterable) yield f(x); } export async function* aMap(iterable: ForAwaitable, f: (a: A) => Awaitable): AsyncIterableIterator { for await (const x of iterable) yield f(x); } export function join(iterable: Iterable): string { return [...iterable].join(''); } export async function aJoin(iterable: ForAwaitable): Promise { const chunks = []; for await (const x of iterable) chunks.push(x); return chunks.join(''); } export async function collect(iterable: ForAwaitable): Promise { const chunks = []; for await (const x of iterable) chunks.push(x); return chunks } export async function* promiseToAsyncIter(promise: Promise): AsyncIterableIterator { yield await promise; } export function promiseToStream(promise: Promise): ReadableStream { return new ReadableStream({ async start(ctrl) { try { ctrl.enqueue(await promise); ctrl.close() } catch (err) { ctrl.error(err) } } }) }