From 5d74c9e749076ceb20eb57dc98e281727e3bb726 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 7c29af3353d474..8c4f8620e55c9d 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -513,12 +513,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];