Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ this to/from storage.

Create a parsing stream.

### `jsonArrayStreams.stringify()`
### `jsonArrayStreams.stringify([replacer, [space]])`

Create a stringifying stream.

replacer, space are optional parameters that are passed through to JSON.stringify, to support pretty-printing the output.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function createParseStream() {
});
}

function createStringifyStream() {
function createStringifyStream(replacer, space) {
var first = true;
return through.obj(
function (value, enc, cb) {
Expand All @@ -29,7 +29,7 @@ function createStringifyStream() {
else {
this.push(",");
}
this.push(JSON.stringify(value));
this.push(JSON.stringify(value, replacer, space));
cb();
},
function (cb) {
Expand Down
19 changes: 17 additions & 2 deletions test/json-array-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ var through = require('through2');
var jsonArrayStreams = require('../index');
var Readable = require('stream').Readable;

function createStringifyStream(dataCB) {
function createStringifyStream(dataCB, replacer, space) {
var input = new Readable({objectMode: true});
input._read = function () {};
input
.pipe(jsonArrayStreams.stringify())
.pipe(jsonArrayStreams.stringify(replacer, space))
.pipe(through.obj(
function (chunk, enc, cb) {
dataCB(chunk);
Expand Down Expand Up @@ -92,3 +92,18 @@ test("stringify stream", function (t) {
});
});

test("with space", function (t) {
var result = "";
var input = createStringifyStream(function (chunk) {
result = result + chunk;
}, null, 2);

input.push({key: "value"});
input.push(null);

input.on('end', function () {
t.equal(result, '[{\n "key": "value"\n}]');
t.end();
});
});