-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliner.js
37 lines (29 loc) · 839 Bytes
/
liner.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
var os = require('os');
var stream = require('stream');
var util = require('util');
var debug = require('debug')('liner');
var Transform = stream.Transform;
util.inherits(Liner, Transform);
function Liner()
{
if (!(this instanceof Liner))
return new Liner();
Transform.call(this, { objectMode: true });
this._lastLineData = null;
}
Liner.prototype._transform = function (chunk, encoding, done) {
debug('transform');
var data = chunk.toString()
if (this._lastLineData) data = this._lastLineData + data
var lines = data.split(os.EOL)
this._lastLineData = lines.splice(lines.length-1,1)[0]
lines.forEach(this.push.bind(this))
done()
}
Liner.prototype._flush = function (done) {
debug('_flush');
if (this._lastLineData) this.push(this._lastLineData)
this._lastLineData = null
done()
}
module.exports = Liner