@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
41 lines (34 loc) • 777 B
text/typescript
import { Transform } from 'stream'
import { TransformOptions, TransformTyped } from '../stream.model'
export interface TransformBufferOptions extends TransformOptions {
batchSize: number
}
/**
* Similar to RxJS bufferCount()
*
* @default batchSize is 10
*/
export function transformBuffer<IN = any>(opt: TransformBufferOptions): TransformTyped<IN, IN[]> {
const { batchSize } = opt
let buf: IN[] = []
return new Transform({
objectMode: true,
...opt,
transform(chunk, _, cb) {
buf.push(chunk)
if (buf.length >= batchSize) {
cb(null, buf)
buf = []
} else {
cb()
}
},
final(this: Transform, cb) {
if (buf.length) {
this.push(buf)
buf = []
}
cb()
},
})
}