import type { Reducer , Transducer } from "./api.js" ; import { iterator , __iter } from "./iterator.js" ; import { isReduced } from "./reduced.js" ; remarks example example param re export function rechunk ( re ? ) ; export function rechunk ( xs ) ; export function rechunk ( re , xs ) ; export 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 , ( acc ) => { if ( buf ) acc = reduce ( acc , buf ) ; return complete ( acc ) ; } , ( acc , chunk ) => { buf += chunk ; const res = buf . split ( re ) ; if ( res . length > 1 ) { buf = res . pop ( ) ! ; for ( let l of res ) { acc = reduce ( acc , l ) ; if ( isReduced ( acc ) ) { buf = "" ; break ; } } } return acc ; } , ] ; } ; }