UNPKG

@naturalcycles/nodejs-lib

Version:
35 lines (34 loc) 949 B
import { Transform } from 'node:stream'; /** * Similar to RxJS bufferCount(), * allows to "chunk" the input stream into chunks of `opt.chunkSize` size. * Last chunk will contain the remaining items, possibly less than chunkSize. * * `chunkSize` indicates how many items to include in each chunk. * Last chunk will contain the remaining items, possibly less than chunkSize. */ export function transformChunk(chunkSize, opt) { let buf = []; return new Transform({ objectMode: true, highWaterMark: 1, ...opt, transform(chunk, _, cb) { buf.push(chunk); if (buf.length >= chunkSize) { cb(null, buf); buf = []; } else { cb(); } }, final(cb) { if (buf.length) { this.push(buf); buf = []; } cb(); }, }); }