From f467aca55d064f5081c490c7355ff23c94c0462d Mon Sep 17 00:00:00 2001 From: Chamath Adithya Date: Sat, 30 May 2026 00:53:29 +0530 Subject: [PATCH] fix: enforce maximumDepth for nested objects with array replacers --- index.js | 3 +++ test.js | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5172a71..5e7da81 100644 --- a/index.js +++ b/index.js @@ -419,6 +419,9 @@ function configure (options) { stack.pop() return `[${res}]` } + if (maximumDepth < stack.length + 1) { + return '"[Object]"' + } stack.push(value) let whitespace = '' if (spacer !== '') { diff --git a/test.js b/test.js index 322b832..67ca667 100644 --- a/test.js +++ b/test.js @@ -910,11 +910,17 @@ test('maximumDepth config', function (assert) { assert.equal(result, '{"a":{"a":"[Array]","b":"[Object]"}}') const res2 = serialize(obj, ['a', 'b']) - assert.equal(res2, '{"a":{"a":"[Array]","b":{}}}') + assert.equal(res2, '{"a":{"a":"[Array]","b":"[Object]"}}') const json = JSON.stringify(obj, ['a', 'b']) assert.equal(json, '{"a":{"a":[1,2,3],"b":{}}}') + const serializeDepth1 = stringify.configure({ + maximumDepth: 1 + }) + const resDepth1 = serializeDepth1(obj, ['a', 'b']) + assert.equal(resDepth1, '{"a":"[Object]"}') + const res3 = serialize(obj, null, 2) assert.equal(res3, `{ "a": {