diff --git a/lib/utils.js b/lib/utils.js index 4b56c182..1b219cdd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -206,7 +206,7 @@ var isRegExp = function isRegExp(obj) { }; var isBuffer = function isBuffer(obj) { - if (obj === null || typeof obj === 'undefined') { + if (!obj || typeof obj !== 'object') { return false; } diff --git a/package.json b/package.json index ced1effc..bd7877dc 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,10 @@ "editorconfig-tools": "^0.1.1", "eslint": "^5.9.0", "evalmd": "^0.0.17", + "for-each": "^0.3.3", "iconv-lite": "^0.4.24", "mkdirp": "^0.5.1", + "object-inspect": "^1.6.0", "qs-iconv": "^1.0.4", "safe-publish-latest": "^1.1.2", "safer-buffer": "^2.1.2", diff --git a/test/utils.js b/test/utils.js index b3c5dedb..259c5ad9 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,6 +1,9 @@ 'use strict'; var test = require('tape'); +var inspect = require('object-inspect'); +var SaferBuffer = require('safer-buffer').Buffer; +var forEach = require('for-each'); var utils = require('../lib/utils'); test('merge()', function (t) { @@ -115,3 +118,19 @@ test('combine()', function (t) { t.end(); }); + +test('isBuffer()', function (t) { + forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) { + t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer'); + }); + + var fakeBuffer = { constructor: Buffer }; + t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer'); + + var saferBuffer = SaferBuffer.from('abc'); + t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer'); + + var buffer = Buffer.from('abc'); + t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer'); + t.end(); +});