Version 1
[yaffs-website] / node_modules / linerstream / index.js
diff --git a/node_modules/linerstream/index.js b/node_modules/linerstream/index.js
new file mode 100644 (file)
index 0000000..407be32
--- /dev/null
@@ -0,0 +1,32 @@
+var Transform = require('stream').Transform
+var util = require('util')
+var os = require('os')
+
+function Liner(opts) {
+  opts = opts || {}
+  opts.objectMode = true
+  Transform.call(this, opts)
+}
+
+util.inherits(Liner, Transform)
+Liner.prototype._transform = function transform(chunk, encoding, done) {
+  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 flush(done) {
+  if (this._lastLineData) {
+    this.push(this._lastLineData)
+  }
+  this._lastLineData = null
+  done()
+}
+
+module.exports = Liner