buffer: show hidden item count

This adds the number of hidden items in case INSPECT_MAX_BYTES is
exceeded.

PR-URL: https://github.com/nodejs/node/pull/22289
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-08-13 00:47:11 +02:00
parent 8a41470c85
commit 755520c4c3
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 5 additions and 4 deletions

View File

@ -683,8 +683,9 @@ Buffer.prototype[customInspectSymbol] = function inspect() {
var str = '';
var max = exports.INSPECT_MAX_BYTES;
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
if (this.length > max)
str += ' ... ';
const remaining = this.length - max;
if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
return `<${this.constructor.name} ${str}>`;
};
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];

View File

@ -33,7 +33,7 @@ b.fill('1234');
let s = buffer.SlowBuffer(4);
s.fill('1234');
let expected = '<Buffer 31 32 ... >';
let expected = '<Buffer 31 32 ... 2 more bytes>';
assert.strictEqual(util.inspect(b), expected);
assert.strictEqual(util.inspect(s), expected);

View File

@ -19,5 +19,5 @@ const util = require('util');
{
const buf = Buffer.from('x'.repeat(51));
assert.ok(/^<Buffer (?:78 ){50}\.\.\. >$/.test(util.inspect(buf)));
assert.ok(/^<Buffer (?:78 ){50}\.\.\. 1 more byte>$/.test(util.inspect(buf)));
}