1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | var stream = require('stream');
|
6 | var util = require('util');
|
7 |
|
8 | var Lines = module.exports = function Lines(opt) {
|
9 | if (!(this instanceof Lines)) return new Lines(opt);
|
10 | stream.Transform.call(this, {objectMode: true});
|
11 | this._buf = '';
|
12 | this._newline = false;
|
13 | };
|
14 | util.inherits(Lines, stream.Transform);
|
15 |
|
16 | Lines.prototype._transform = function (chunk, encoding, done) {
|
17 | var data = this._buf + chunk.toString('utf8');
|
18 | var lines = data.split(/\r\n|\r|\n/);
|
19 | this._newline = data[data.length - 1] === '\n';
|
20 | this._buf = lines.pop();
|
21 | done(null, lines);
|
22 | };
|
23 |
|
24 | Lines.prototype._flush = function (done) {
|
25 | if (this._buf) this.push([this._buf]);
|
26 | done();
|
27 | this.emit('endNewline', this._newline);
|
28 | };
|