UNPKG

@naturalcycles/nodejs-lib

Version:
35 lines 909 B
"use strict"; 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