concat-stream
Advanced tools
Comparing version 0.0.6 to 0.0.7
28
index.js
var stream = require('stream') | ||
var util = require('util') | ||
var BufferJoiner = require('bufferjoiner') | ||
@@ -19,19 +18,20 @@ function ConcatStream(cb) { | ||
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) { | ||
var isArray = Array.isArray(arr) | ||
var isTypedArray = arr.toString().match(/Array/) | ||
return isArray || isTypedArray | ||
} | ||
ConcatStream.prototype.getBody = function () { | ||
if (this.body.length === 0) return | ||
if (typeof(this.body[0]) === "string") return this.body.join('') | ||
if (this.body[0].toString().match(/Array/)) { | ||
var first = false | ||
this.body.forEach(function(ary) { | ||
if (!first) return first = ary | ||
first.concat(ary) | ||
}) | ||
return first | ||
} | ||
if (this.isArray(this.body[0])) return this.arrayConcat(this.body) | ||
if (typeof(Buffer) !== "undefined" && Buffer.isBuffer(this.body[0])) { | ||
var buffs = new BufferJoiner() | ||
this.body.forEach(function(buf) { | ||
buffs.add(buf) | ||
}) | ||
return buffs.join() | ||
return Buffer.concat(this.body) | ||
} | ||
@@ -38,0 +38,0 @@ return this.body |
{ "name" : "concat-stream" | ||
, "description" : "writable stream that concatenates strings or data and calls a callback with the result" | ||
, "tags" : ["stream", "simple", "util", "utility"] | ||
, "version" : "0.0.6" | ||
, "version" : "0.0.7" | ||
, "author" : "Max Ogden <max@maxogden.com>" | ||
@@ -12,6 +12,3 @@ , "repository" : | ||
{ "url" : "http://github.com/maxogden/node-concat-stream/issues" } | ||
, "engines" : ["node >= 0.4.0"] | ||
, "dependencies": { | ||
"bufferjoiner": "0.1.3" | ||
} | ||
, "engines" : ["node >= 0.8.0"] | ||
} |
27
test.js
var spawn = require('child_process').spawn | ||
var concat = require('./') | ||
// real world example | ||
var cmd = spawn('ls') | ||
cmd.stdout.pipe( | ||
concat(function(err, out) { | ||
console.log(err, out.toString()) | ||
console.log('`ls`', err, out.toString()) | ||
}) | ||
) | ||
// array stream | ||
var arrays = concat(function(err, out) { | ||
console.log('arrays', err, out) | ||
}) | ||
arrays.write([1,2,3]) | ||
arrays.write([4,5,6]) | ||
arrays.end() | ||
// buffer stream | ||
var buffers = concat(function(err, out) { | ||
console.log('buffers', err, out) | ||
}) | ||
buffers.write(new Buffer('pizza ')) | ||
buffers.write(new Buffer('cats')) | ||
buffers.end() | ||
// string stream | ||
var strings = concat(function(err, out) { | ||
console.log('strings', err, out) | ||
}) | ||
strings.write("nacho ") | ||
strings.write("dogs") | ||
strings.end() |
3133
0
69
- Removedbufferjoiner@0.1.3
- Removedbufferjoiner@0.1.3(transitive)