From 755520c4c3f9e1f85268d65ca647dec076e9a17a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 13 Aug 2018 00:47:11 +0200 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Luigi Pinca --- lib/buffer.js | 5 +++-- test/parallel/test-buffer-inspect.js | 2 +- test/parallel/test-buffer-prototype-inspect.js | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 398357c6845568..466e08d293a1bf 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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]; diff --git a/test/parallel/test-buffer-inspect.js b/test/parallel/test-buffer-inspect.js index aa703db67dfac4..9f91de700c7878 100644 --- a/test/parallel/test-buffer-inspect.js +++ b/test/parallel/test-buffer-inspect.js @@ -33,7 +33,7 @@ b.fill('1234'); let s = buffer.SlowBuffer(4); s.fill('1234'); -let expected = ''; +let expected = ''; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); diff --git a/test/parallel/test-buffer-prototype-inspect.js b/test/parallel/test-buffer-prototype-inspect.js index 9e6c66dc3da002..f13dda49f0dd45 100644 --- a/test/parallel/test-buffer-prototype-inspect.js +++ b/test/parallel/test-buffer-prototype-inspect.js @@ -19,5 +19,5 @@ const util = require('util'); { const buf = Buffer.from('x'.repeat(51)); - assert.ok(/^$/.test(util.inspect(buf))); + assert.ok(/^$/.test(util.inspect(buf))); }