-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliner.js
More file actions
30 lines (25 loc) · 745 Bytes
/
liner.js
File metadata and controls
30 lines (25 loc) · 745 Bytes
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
/***
* Nifty stream line processor.
* Grabbed and cleaned up from the following strongloop post:
* http://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/
*/
var stream = require('stream');
var liner = new stream.Transform({objectMode: true});
liner._transform = function(chunk, encoding, done) {
var data = chunk.toString();
if (this._lastLineData) {
data = this._lastLineData + data;
}
var lines = data.split('\n');
this._lastLineData = lines.splice(lines.length - 1, 1)[0];
lines.forEach(this.push.bind(this));
done();
};
liner._flush = function(done) {
if (this._lastLineData) {
this.push(this._lastLineData);
}
this._lastLineData = null;
done();
};
module.exports = liner;