UNPKG

@thi.ng/transducers-async

Version:

Async versions of various highly composable transducers, reducers and iterators

36 lines (35 loc) 846 B
import { isReduced } from "@thi.ng/transducers/reduced"; import { __iter, iterator } from "./iterator.js"; function rechunk(...args) { const iter = __iter(rechunk, args, iterator); if (iter) return iter; return ([init, complete, reduce]) => { let buf = ""; const re = args[0] || /\r?\n/; return [ init, async (acc) => { if (buf) acc = await reduce(acc, buf); return complete(acc); }, async (acc, chunk) => { buf += chunk; const parts = buf.split(re); if (parts.length > 1) { buf = parts.pop(); for (const part of parts) { acc = await reduce(acc, part); if (isReduced(acc)) { buf = ""; break; } } } return acc; } ]; }; } export { rechunk };