Skip to content

Commit

Permalink
buffer: refactor Buffer.prototype.inspect()
Browse files Browse the repository at this point in the history
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: #11600
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
Trott authored and addaleax committed Mar 5, 2017
1 parent 1445e28 commit 02dbae6
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit 02dbae6

Please sign in to comment.