Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ function base64ByteLength(str, bytes) {


function byteLength(string, encoding) {
if (string instanceof Buffer)
return string.length;

if (typeof string !== 'string')
string = '' + string;

Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
assert.equal(Buffer.byteLength({}, 'binary'), 15);
assert.equal(Buffer.byteLength(), 9);

// buffer
var incomplete = new Buffer([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
assert.equal(Buffer.byteLength(incomplete), 5);
var ascii = new Buffer('abc');
assert.equal(Buffer.byteLength(ascii), 3);

// special case: zero length string
assert.equal(Buffer.byteLength('', 'ascii'), 0);
assert.equal(Buffer.byteLength('', 'HeX'), 0);
Expand Down