UNPKG

781 BJavaScriptView Raw
1// Copyright 2016 Luca-SAS, licensed under the Apache License 2.0
2
3'use strict';
4
5var stream = require('stream');
6var util = require('util');
7
8var 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};
14util.inherits(Lines, stream.Transform);
15
16Lines.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
24Lines.prototype._flush = function (done) {
25 if (this._buf) this.push([this._buf]);
26 done();
27 this.emit('endNewline', this._newline);
28};