Skip to content

Commit

Permalink
[Refactor] utils: isBuffer: small tweak; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 14, 2019
1 parent 41c42b8 commit d3a52ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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();
});

0 comments on commit d3a52ee

Please sign in to comment.