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

19 lines (17 loc) 627 B
import type { Ord } from "rambda"; import type { Awaitable } from "./Awaitable"; /** chunk items by compareFn, group items with same Ord */ export function chunkBys<T>(compareFn: (x: T) => Awaitable<Ord>) { let chunks: T[] = []; let lastOrder: Ord; return new TransformStream<T, T[]>({ transform: async (chunk, ctrl) => { const order = await compareFn(chunk); if (lastOrder && lastOrder !== order) ctrl.enqueue(chunks.splice(0, Infinity)); // clear chunks; chunks.push(chunk); lastOrder = order; }, flush: async (ctrl) => void (chunks.length && ctrl.enqueue(chunks)), }); }