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": {