UNPKG

sflow

Version:

sflow is a powerful and highly-extensible library designed for processing and manipulating streams of data effortlessly. Inspired by the functional programming paradigm, it provides a rich set of utilities for transforming streams, including chunking, fil

20 lines (15 loc) 494 B
import type { Awaitable } from "./Awaitable"; // from([1, 2, 3]).pipeThrough(filters()); export function flatMaps<T, R>( fn: (x: T, i: number) => Awaitable<R[]>, ): TransformStream<T, R> { let i = 0; return new TransformStream<T, R>({ transform: async (chunk, ctrl) => { const ret = fn(chunk, i++); // await only if ret is promise, to ensure performance const val = ret instanceof Promise ? await ret : ret; val.map((e) => ctrl.enqueue(e)); }, }); }