Skip to content

Commit 5dcb5c4

Browse files
committed
Fix stringifyInfo() to correctly accept the space parameter from options
1 parent 32b95eb commit 5dcb5c4

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## next
2+
3+
- Fixed `stringifyInfo()` to correctly accept the `space` parameter from options, i.e. `stringifyInfo(data, { space: 2 })`
4+
15
## 0.6.1 (2024-08-06)
26

37
- Enhanced the performance of `stringifyChunked()` by 1.5-3x

src/stringify-cases.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const tests = [
110110
];
111111
export const spaceTests = tests
112112
.filter(t => typeof t === 'object')
113-
.concat('foo', 123, null, false);
113+
.concat('foo', 123, false);
114114
export const replacerTests = [
115115
[1, () => 2],
116116
[{ a: undefined }, (k, v) => {

src/stringify-chunked.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ function testTitleWithValue(title) {
1717

1818
function createStringifyTestFn(input, expected, ...args) {
1919
return () => {
20-
const chunks = [...stringifyChunked(input, ...args)];
21-
const actual = chunks.join('');
20+
const actual = [...stringifyChunked(input, ...args)].join('');
2221

2322
if (actual !== expected) {
2423
const escapedActual = JSON.stringify(actual);
@@ -166,6 +165,19 @@ describe('stringifyChunked()', () => {
166165
});
167166
});
168167

168+
it('options', () => {
169+
const actual = [...stringifyChunked({ foo: 123, bar: 456 }, {
170+
highWaterMark: 1,
171+
replacer: ['foo'],
172+
space: 4
173+
}, 2)]; // should ignore third argument when options passed
174+
175+
assert.deepStrictEqual(actual, [
176+
'{\n "foo": 123',
177+
'\n}'
178+
]);
179+
});
180+
169181
describe('circular structure', () => {
170182
it('{ a: $ } should emit error when object refers to ancestor object', () => {
171183
const circularRef = {};

src/stringify-info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function stringifyInfo(value, optionsOrReplacer, space) {
149149
replacer = null;
150150
}
151151

152-
space = spaceLength(space);
152+
space = spaceLength(optionsOrReplacer.space);
153153

154154
const keysLength = new Map();
155155
const visited = new Map();

src/stringify-info.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ describe('stringifyInfo()', () => {
5353
}
5454
});
5555

56+
it('options', () => {
57+
const actual = stringifyInfo({ foo: 123, bar: 456 }, {
58+
replacer: ['foo'],
59+
space: 4
60+
}, 2); // should ignore third argument when options passed
61+
62+
assert.deepStrictEqual(actual, {
63+
bytes: 18,
64+
circular: []
65+
});
66+
});
67+
5668
describe('circular', () => {
5769
it('should stop on first circular reference by default', () => {
5870
const circularRef = {};

0 commit comments

Comments
 (0)