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

22 lines (18 loc) 648 B
import type { Awaitable } from "./Awaitable"; export function finds<T>( predicate: (value: T, index: number) => Awaitable<any> ): TransformStream<T, T> { let index = 0; let found = false; return new TransformStream<T, T>({ async transform(chunk, controller) { if (found) return; // Already found, ignore further chunks const shouldEmit = await predicate(chunk, index++); if (shouldEmit) { found = true; controller.enqueue(chunk); controller.terminate(); // Stop the stream after finding the first match } } }); }