UNPKG

895 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformLimit = void 0;
4const stream_1 = require("stream");
5/**
6 * 0 or falsy value means "no limit"
7 */
8function transformLimit(limit, opt = {}) {
9 let index = 0;
10 let ended = false;
11 return new stream_1.Transform({
12 objectMode: true,
13 ...opt,
14 transform(chunk, _encoding, cb) {
15 index++;
16 if (!ended) {
17 cb(null, chunk); // pass through the item
18 }
19 else {
20 cb(null); // pass-through empty
21 }
22 if (limit && index === limit) {
23 ended = true;
24 console.log(`transformLimit: limit of ${limit} reached`);
25 // this.emit('end') // this makes it "halt" on Node 14 lts
26 }
27 },
28 });
29}
30exports.transformLimit = transformLimit;