Skip to content

Commit

Permalink
Merge pull request #27 from parshap/infer-string-fix
Browse files Browse the repository at this point in the history
Correctly infer string encoding from empty string
  • Loading branch information
Max Ogden committed Jun 2, 2014
2 parents 59bb94d + 8c79b14 commit 8d6c3d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions index.js
Expand Up @@ -42,8 +42,7 @@ ConcatStream.prototype._write = function(chunk, enc, next) {
}

ConcatStream.prototype.inferEncoding = function (buff) {
var firstBuffer = buff || this.body[0]
if (!firstBuffer) return 'buffer'
var firstBuffer = buff === undefined ? this.body[0] : buff;
if (Buffer.isBuffer(firstBuffer)) return 'buffer'
if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array'
if (Array.isArray(firstBuffer)) return 'array'
Expand Down
3 changes: 2 additions & 1 deletion test/infer.js
Expand Up @@ -8,7 +8,8 @@ test('type inference works as expected', function(t) {
t.equal(stream.inferEncoding(undefined), 'buffer', 'buffer')
t.equal(stream.inferEncoding(new Uint8Array(1)), 'uint8array', 'uint8array')
t.equal(stream.inferEncoding('hello'), 'string', 'string')
t.equal(stream.inferEncoding(''), 'string', 'string')
t.equal(stream.inferEncoding({hello: "world"}), 'object', 'object')
t.equal(stream.inferEncoding(1), 'buffer', 'buffer')
t.end()
})
})
12 changes: 12 additions & 0 deletions test/string.js
Expand Up @@ -62,3 +62,15 @@ test('string from buffers with multibyte characters', function (t) {
}
strings.end()
})

test('string infer encoding with empty string chunk', function (t) {
t.plan(2)
var strings = concat(function(out) {
t.equal(typeof out, 'string')
t.equal(out, 'nacho dogs')
})
strings.write("")
strings.write("nacho ")
strings.write("dogs")
strings.end()
})

0 comments on commit 8d6c3d4

Please sign in to comment.