@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
36 lines (35 loc) • 825 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformBuffer = void 0;
const stream_1 = require("stream");
/**
* Similar to RxJS bufferCount()
*
* @default batchSize is 10
*/
function transformBuffer(opt) {
const { batchSize } = opt;
let buf = [];
return new stream_1.Transform({
objectMode: true,
...opt,
transform(chunk, _, cb) {
buf.push(chunk);
if (buf.length >= batchSize) {
cb(null, buf);
buf = [];
}
else {
cb();
}
},
final(cb) {
if (buf.length) {
this.push(buf);
buf = [];
}
cb();
},
});
}
exports.transformBuffer = transformBuffer;