Skip to content

Commit 02dbae6

Browse files
Trottaddaleax
authored andcommitted
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: #11600 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 1445e28 commit 02dbae6

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/buffer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,10 @@ Buffer.prototype.equals = function equals(b) {
521521
Buffer.prototype[internalUtil.customInspectSymbol] = function inspect() {
522522
var str = '';
523523
var max = exports.INSPECT_MAX_BYTES;
524-
if (this.length > 0) {
525-
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
526-
if (this.length > max)
527-
str += ' ... ';
528-
}
529-
return '<' + this.constructor.name + ' ' + str + '>';
524+
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
525+
if (this.length > max)
526+
str += ' ... ';
527+
return `<${this.constructor.name} ${str}>`;
530528
};
531529
Buffer.prototype.inspect = Buffer.prototype[internalUtil.customInspectSymbol];
532530

0 commit comments

Comments
 (0)