Permalink
Browse files

use 0.8 Buffer.concat and better custom array concatenation

  • Loading branch information...
1 parent a6a59ef commit 170331a4b984e7316967e5ab6f945c9e66c98877 @maxogden committed Aug 3, 2012
Showing with 42 additions and 20 deletions.
  1. +14 −14 index.js
  2. +2 −5 package.json
  3. +26 −1 test.js
View
@@ -1,6 +1,5 @@
var stream = require('stream')
var util = require('util')
-var BufferJoiner = require('bufferjoiner')
function ConcatStream(cb) {
stream.Stream.call(this)
@@ -16,23 +15,24 @@ ConcatStream.prototype.write = function(chunk) {
this.body.push(chunk)
}
+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)
}
return this.body
}
View
@@ -1,16 +1,13 @@
{ "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>"
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/maxogden/node-concat-stream.git"
}
, "bugs" :
{ "url" : "http://github.com/maxogden/node-concat-stream/issues" }
-, "engines" : ["node >= 0.4.0"]
-, "dependencies": {
- "bufferjoiner": "0.1.3"
- }
+, "engines" : ["node >= 0.8.0"]
@Raynos

Raynos Aug 5, 2012

what about the people that like 0.6 :(

@cbrammer

cbrammer Aug 5, 2012

Big, big +1 for this, not ready to migrate every thing up to 8 yet

@maxogden

maxogden Aug 6, 2012

Owner

ok someone who wants 0.6 support should pull request in a feature detection enabled version with a fallback implemented

}
View
27 test.js
@@ -1,9 +1,34 @@
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()

0 comments on commit 170331a

Please sign in to comment.