From 67076e99cfcd7e606207f4c9f5c2b312ad99a749 Mon Sep 17 00:00:00 2001 From: Yves M <4225430+yvele@users.noreply.github.com> Date: Tue, 18 Nov 2025 20:46:41 +0100 Subject: [PATCH] fix: correct overflow info for arrays with maximumBreadth --- index.js | 16 ++++++++-------- test.js | 11 +++++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 0907c91..ebbc451 100644 --- a/index.js +++ b/index.js @@ -246,8 +246,8 @@ function configure (options) { } const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation) res += tmp !== undefined ? tmp : 'null' - if (value.length - 1 > maximumBreadth) { - const removedKeys = value.length - maximumBreadth - 1 + if (value.length > maximumBreadth) { + const removedKeys = value.length - maximumBreadth res += `${join}"... ${getItemCount(removedKeys)} not stringified"` } if (spacer !== '') { @@ -354,8 +354,8 @@ function configure (options) { } const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation) res += tmp !== undefined ? tmp : 'null' - if (value.length - 1 > maximumBreadth) { - const removedKeys = value.length - maximumBreadth - 1 + if (value.length > maximumBreadth) { + const removedKeys = value.length - maximumBreadth res += `${join}"... ${getItemCount(removedKeys)} not stringified"` } if (spacer !== '') { @@ -444,8 +444,8 @@ function configure (options) { } const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation) res += tmp !== undefined ? tmp : 'null' - if (value.length - 1 > maximumBreadth) { - const removedKeys = value.length - maximumBreadth - 1 + if (value.length > maximumBreadth) { + const removedKeys = value.length - maximumBreadth res += `${join}"... ${getItemCount(removedKeys)} not stringified"` } res += `\n${originalIndentation}` @@ -553,8 +553,8 @@ function configure (options) { } const tmp = stringifySimple(String(i), value[i], stack) res += tmp !== undefined ? tmp : 'null' - if (value.length - 1 > maximumBreadth) { - const removedKeys = value.length - maximumBreadth - 1 + if (value.length > maximumBreadth) { + const removedKeys = value.length - maximumBreadth res += `,"... ${getItemCount(removedKeys)} not stringified"` } stack.pop() diff --git a/test.js b/test.js index 23edd23..0cf7eb0 100644 --- a/test.js +++ b/test.js @@ -911,10 +911,10 @@ test('maximumBreadth config', function (assert) { }) const result = serialize(obj, (key, val) => val) - assert.equal(result, '{"a":["a","b","c","... 1 item not stringified"]}') + assert.equal(result, '{"a":["a","b","c","... 2 items not stringified"]}') const res2 = serialize(obj, ['a', 'b']) - assert.equal(res2, '{"a":["a","b","c","... 1 item not stringified"]}') + assert.equal(res2, '{"a":["a","b","c","... 2 items not stringified"]}') const res3 = serialize(obj, null, 2) assert.equal(res3, `{ @@ -922,7 +922,7 @@ test('maximumBreadth config', function (assert) { "a", "b", "c", - "... 1 item not stringified" + "... 2 items not stringified" ] }`) @@ -936,6 +936,9 @@ test('maximumBreadth config', function (assert) { } }`) + const res5 = serialize(['a', 'b', 'c', 'd']) + assert.equal(res5, '["a","b","c","... 1 item not stringified"]') + assert.end() }) test('limit number of keys with array replacer', function (assert) { @@ -976,7 +979,7 @@ test('limit number of keys in array', (assert) => { arr.push(i) } const res = serialize(arr) - const expected = '[0,1,2,"... 96 items not stringified"]' + const expected = '[0,1,2,"... 97 items not stringified"]' assert.equal(res, expected) assert.end() })