Version 1
[yaffs-website] / node_modules / linerstream / index.js
1 var Transform = require('stream').Transform
2 var util = require('util')
3 var os = require('os')
4
5 function Liner(opts) {
6   opts = opts || {}
7   opts.objectMode = true
8   Transform.call(this, opts)
9 }
10
11 util.inherits(Liner, Transform)
12 Liner.prototype._transform = function transform(chunk, encoding, done) {
13   var data = chunk.toString()
14   if (this._lastLineData) {
15     data = this._lastLineData + data
16   }
17   var lines = data.split(os.EOL)
18   this._lastLineData = lines.splice(lines.length - 1, 1)[0]
19
20   lines.forEach(this.push.bind(this))
21   done()
22 }
23
24 Liner.prototype._flush = function flush(done) {
25   if (this._lastLineData) {
26     this.push(this._lastLineData)
27   }
28   this._lastLineData = null
29   done()
30 }
31
32 module.exports = Liner