var stream = require('stream')
var bops = require('bops')
var util = require('util')
function ConcatStream(cb) {
stream.Stream.call(this)
this.writable = true
if (cb) this.cb = cb
this.body = []
this.on('error', function(err) {
})
}
util.inherits(ConcatStream, stream.Stream)
ConcatStream.prototype.write = function(chunk) {
this.emit('data', chunk)
this.body.push(chunk)
}
ConcatStream.prototype.destroy = function() {}
ConcatStream.prototype.arrayConcat = function(arrs) {
if (arrs.length === 0) return []
if (arrs.length === 1) return arrs[0]
return arrs.reduce(function (a, b) { return a.concat(b) })
}
ConcatStream.prototype.isArray = function(arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
}
ConcatStream.prototype.getBody = function () {
if (this.body.length === 0) return bops.from(0)
if (typeof(this.body[0]) === "string") return this.body.join('')
if (this.isArray(this.body[0])) return this.arrayConcat(this.body)
if (bops.is(this.body[0])) return bops.join(this.body)
return this.body
}
ConcatStream.prototype.end = function(chunk) {
if (chunk) this.write(chunk)
this.emit('end')
if (this.cb) this.cb(this.getBody())
}
module.exports = function(cb) {
return new ConcatStream(cb)
}
module.exports.ConcatStream = ConcatStream