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