Skip to content

Commit b617fbc

Browse files
authored
Merge pull request #47 from mafintosh/to-string-numbers
to-string numbers written to the stream
2 parents 4ca83d6 + 3e285ba commit b617fbc

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ function isArrayish (arr) {
7373
return /Array\]$/.test(Object.prototype.toString.call(arr))
7474
}
7575

76+
function isBufferish (p) {
77+
return typeof p === 'string' || isArrayish(p) || (p && typeof p.subarray === 'function')
78+
}
79+
7680
function stringConcat (parts) {
7781
var strings = []
7882
var needsToString = false
@@ -82,8 +86,10 @@ function stringConcat (parts) {
8286
strings.push(p)
8387
} else if (Buffer.isBuffer(p)) {
8488
strings.push(p)
85-
} else {
89+
} else if (isBufferish(p)) {
8690
strings.push(new Buffer(p))
91+
} else {
92+
strings.push(new Buffer(String(p)))
8793
}
8894
}
8995
if (Buffer.isBuffer(parts[0])) {
@@ -101,10 +107,11 @@ function bufferConcat (parts) {
101107
var p = parts[i]
102108
if (Buffer.isBuffer(p)) {
103109
bufs.push(p)
104-
} else if (typeof p === 'string' || isArrayish(p)
105-
|| (p && typeof p.subarray === 'function')) {
110+
} else if (isBufferish(p)) {
106111
bufs.push(new Buffer(p))
107-
} else bufs.push(new Buffer(String(p)))
112+
} else {
113+
bufs.push(new Buffer(String(p)))
114+
}
108115
}
109116
return Buffer.concat(bufs)
110117
}

test/string.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('string from buffers with multibyte characters', function (t) {
5858
var snowman = new Buffer('☃')
5959
for (var i = 0; i < 8; i++) {
6060
strings.write(snowman.slice(0, 1))
61-
strings.write(snowman.slice(1))
61+
strings.write(snowman.slice(1))
6262
}
6363
strings.end()
6464
})
@@ -74,3 +74,14 @@ test('string infer encoding with empty string chunk', function (t) {
7474
strings.write("dogs")
7575
strings.end()
7676
})
77+
78+
test('to string numbers', function (t) {
79+
var write = concat(function (str) {
80+
t.equal(str, 'a1000')
81+
t.end()
82+
})
83+
84+
write.write('a')
85+
write.write(1000)
86+
write.end()
87+
})

0 commit comments

Comments
 (0)