diff --git a/index.js b/index.js index 5172a71..1fcee2c 100644 --- a/index.js +++ b/index.js @@ -687,7 +687,7 @@ function configure (options) { if (arguments.length > 1) { let spacer = '' if (typeof space === 'number') { - spacer = ' '.repeat(Math.min(space, 10)) + spacer = ' '.repeat(Math.max(0, Math.min(space, 10))) } else if (typeof space === 'string') { spacer = space.slice(0, 10) } diff --git a/test.js b/test.js index 322b832..6a63d0b 100644 --- a/test.js +++ b/test.js @@ -773,6 +773,15 @@ test('maximum spacer length', function (assert) { assert.end() }) +test('negative spacer length', function (assert) { + const input = { a: 0 } + // A negative number means no indentation, same as JSON.stringify (not a throw). + assert.equal(stringify(input, null, -1), JSON.stringify(input, null, -1)) + assert.equal(stringify(input, null, -1e5), JSON.stringify(input, null, -1e5)) + assert.equal(stringify(input, null, -Infinity), JSON.stringify(input, null, -Infinity)) + assert.end() +}) + test('indent properly; regression test for issue #16', function (assert) { const o = { collections: {},