@naturalcycles/nodejs-lib
Version:
Standard library for Node.js
27 lines • 782 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
/**
* 0 or falsy value means "no limit"
*/
function transformLimit(limit, opt = {}) {
let index = 0;
let ended = false;
return new stream_1.Transform({
objectMode: true,
...opt,
transform(chunk, _encoding, cb) {
index++;
if (!ended) {
cb(null, chunk); // pass through the item
}
if (limit && index === limit) {
ended = true;
console.log(`transformLimit: limit of ${limit} reached`);
this.emit('end');
}
},
});
}
exports.transformLimit = transformLimit;
//# sourceMappingURL=transformLimit.js.map