diff --git a/lib/buffer.js b/lib/buffer.js index b6928c18f8ded3..d4f6ccff7e2fe2 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -531,12 +531,10 @@ Buffer.prototype.equals = function equals(b) { Buffer.prototype[internalUtil.customInspectSymbol] = function inspect() { var str = ''; var max = exports.INSPECT_MAX_BYTES; - if (this.length > 0) { - str = this.toString('hex', 0, max).match(/.{2}/g).join(' '); - if (this.length > max) - str += ' ... '; - } - return '<' + this.constructor.name + ' ' + str + '>'; + str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim(); + if (this.length > max) + str += ' ... '; + return `<${this.constructor.name} ${str}>`; }; Buffer.prototype.inspect = Buffer.prototype[internalUtil.customInspectSymbol]; diff --git a/test/parallel/test-buffer-prototype-inspect.js b/test/parallel/test-buffer-prototype-inspect.js new file mode 100644 index 00000000000000..5f65a9bb288f16 --- /dev/null +++ b/test/parallel/test-buffer-prototype-inspect.js @@ -0,0 +1,23 @@ +'use strict'; +require('../common'); + +// lib/buffer.js defines Buffer.prototype.inspect() to override how buffers are +// presented by util.inspect(). + +const assert = require('assert'); +const util = require('util'); + +{ + const buf = Buffer.from('fhqwhgads'); + assert.strictEqual(util.inspect(buf), ''); +} + +{ + const buf = Buffer.from(''); + assert.strictEqual(util.inspect(buf), ''); +} + +{ + const buf = Buffer.from('x'.repeat(51)); + assert.ok(/^$/.test(util.inspect(buf))); +}