Skip to content

Commit d5a1710

Browse files
committed
pass on replacer and space parameters to JSON.stringify
1 parent 4d0b504 commit d5a1710

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ this to/from storage.
3434

3535
Create a parsing stream.
3636

37-
### `jsonArrayStreams.stringify()`
37+
### `jsonArrayStreams.stringify([replacer, [space]])`
3838

3939
Create a stringifying stream.
40+
41+
replacer, space are optional parameters that are passed through to JSON.stringify, to support pretty-printing the output.

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function createParseStream() {
1818
});
1919
}
2020

21-
function createStringifyStream() {
21+
function createStringifyStream(replacer, space) {
2222
var first = true;
2323
return through.obj(
2424
function (value, enc, cb) {
@@ -29,7 +29,7 @@ function createStringifyStream() {
2929
else {
3030
this.push(",");
3131
}
32-
this.push(JSON.stringify(value));
32+
this.push(JSON.stringify(value, replacer, space));
3333
cb();
3434
},
3535
function (cb) {

test/json-array-streams.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ var through = require('through2');
55
var jsonArrayStreams = require('../index');
66
var Readable = require('stream').Readable;
77

8-
function createStringifyStream(dataCB) {
8+
function createStringifyStream(dataCB, replacer, space) {
99
var input = new Readable({objectMode: true});
1010
input._read = function () {};
1111
input
12-
.pipe(jsonArrayStreams.stringify())
12+
.pipe(jsonArrayStreams.stringify(replacer, space))
1313
.pipe(through.obj(
1414
function (chunk, enc, cb) {
1515
dataCB(chunk);
@@ -92,3 +92,18 @@ test("stringify stream", function (t) {
9292
});
9393
});
9494

95+
test("with space", function (t) {
96+
var result = "";
97+
var input = createStringifyStream(function (chunk) {
98+
result = result + chunk;
99+
}, null, 2);
100+
101+
input.push({key: "value"});
102+
input.push(null);
103+
104+
input.on('end', function () {
105+
t.equal(result, '[{\n "key": "value"\n}]');
106+
t.end();
107+
});
108+
});
109+

0 commit comments

Comments
 (0)