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 (18 loc) 570 B
import type { Awaitable } from "./Awaitable"; export const filters: { <T>(): TransformStream<T, NonNullable<T>>; <T>(fn: (x: T, i: number) => Awaitable<any>): TransformStream<T, T>; } = (fn?: (...args: any[]) => any) => { let i = 0; return new TransformStream({ transform: async (chunk, ctrl) => { if (fn) { const shouldEnqueue = await fn(chunk, i++); if (shouldEnqueue) ctrl.enqueue(chunk); } else { const isNull = undefined === chunk || null === chunk; if (!isNull) ctrl.enqueue(chunk); } }, }); };