-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (39 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const {StringDecoder} = require('string_decoder');
const through = require('through2');
function transform(chunk, enc, cb) {
this._last += this._decoder.write(chunk);
if (this._last.length > this.maxLength) {
return cb(new Error('maximum buffer reached'));
}
const list = this._last.split('\n');
this._last = list.pop();
for (const line of list) {
this.count += 1;
if (this.lines.includes(this.count) === false) {
this.push(line + '\n');
}
}
cb();
}
function flush(cb) {
// Forward any gibberish left in there
this._last += this._decoder.end();
if (this._last) {
this.push(this._last);
}
cb();
}
function rmlines(lines, options) {
options = options || {};
const stream = through(options, transform, flush);
// This stream is in objectMode only in the readable part
stream._readableState.objectMode = true;
stream._last = '';
stream._decoder = new StringDecoder('utf8');
stream.lines = (Number.isInteger(lines)) ? [lines] : lines;
stream.count = 0;
stream.maxLength = options.maxLength;
return stream;
}
module.exports = rmlines;