From 02dbae6b3fed82f70c26732c309b76a54ceed913 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 28 Feb 2017 00:15:07 -0800 Subject: [PATCH] buffer: refactor Buffer.prototype.inspect() Replace toString().match().join() with toString().replace().trim(). This enables the elimination of a length check becuase replace() will return empty string if Buffer is empty whereas match() returns null. PR-URL: https://github.com/nodejs/node/pull/11600 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Yuta Hiroto Reviewed-By: Luigi Pinca --- lib/buffer.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 21e24c2980f5e6..27e2b5393e7de0 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -521,12 +521,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];