From 042d4d5da9f9d8d6d14691c2127fb0abcc62f224 Mon Sep 17 00:00:00 2001 From: Alfonso Boza <alfonso@cloud.com> Date: Wed, 8 Jan 2014 12:24:19 -0500 Subject: [PATCH] Buffer.isBuffer() returns explicit true/false, fixed for non-Uint8Array buffer --- index.js | 3 ++- test/is-buffer.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/is-buffer.js diff --git a/index.js b/index.js index c77d54a..7124b1a 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,7 @@ function Buffer (subject, encoding, noZero) { // Fallback: Return this instance of Buffer buf = this buf.length = length + buf._isBuffer = true } var i @@ -121,7 +122,7 @@ Buffer.isEncoding = function (encoding) { } Buffer.isBuffer = function (b) { - return b && b._isBuffer + return (b != null && b._isBuffer) || false } Buffer.byteLength = function (str, encoding) { diff --git a/test/is-buffer.js b/test/is-buffer.js new file mode 100644 index 0000000..eec918f --- /dev/null +++ b/test/is-buffer.js @@ -0,0 +1,10 @@ +var B = require('../index.js').Buffer +var test = require('tape') + +test('Buffer.isBuffer', function (t) { + t.plan(3) + t.equal(B.isBuffer(new B('hey', 'utf8')), true) + t.equal(B.isBuffer(new B([1, 2, 3], 'utf8')), true) + t.equal(B.isBuffer('hey'), false) + t.end() +}) -- 2.34.1