Skip to content

Commit 5d74c9e

Browse files
TrottMylesBorins
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 96924ed commit 5d74c9e

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/buffer.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,10 @@ Buffer.prototype.equals = function equals(b) {
513513
Buffer.prototype[internalUtil.customInspectSymbol] = function inspect() {
514514
var str = '';
515515
var max = exports.INSPECT_MAX_BYTES;
516-
if (this.length > 0) {
517-
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
518-
if (this.length > max)
519-
str += ' ... ';
520-
}
521-
return '<' + this.constructor.name + ' ' + str + '>';
516+
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
517+
if (this.length > max)
518+
str += ' ... ';
519+
return `<${this.constructor.name} ${str}>`;
522520
};
523521
Buffer.prototype.inspect = Buffer.prototype[internalUtil.customInspectSymbol];
524522

0 commit comments

Comments
 (0)