File tree Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,8 @@ this to/from storage.
34
34
35
35
Create a parsing stream.
36
36
37
- ### ` jsonArrayStreams.stringify() `
37
+ ### ` jsonArrayStreams.stringify([replacer, [space]] ) `
38
38
39
39
Create a stringifying stream.
40
+
41
+ replacer, space are optional parameters that are passed through to JSON.stringify, to support pretty-printing the output.
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ function createParseStream() {
18
18
} ) ;
19
19
}
20
20
21
- function createStringifyStream ( ) {
21
+ function createStringifyStream ( replacer , space ) {
22
22
var first = true ;
23
23
return through . obj (
24
24
function ( value , enc , cb ) {
@@ -29,7 +29,7 @@ function createStringifyStream() {
29
29
else {
30
30
this . push ( "," ) ;
31
31
}
32
- this . push ( JSON . stringify ( value ) ) ;
32
+ this . push ( JSON . stringify ( value , replacer , space ) ) ;
33
33
cb ( ) ;
34
34
} ,
35
35
function ( cb ) {
Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ var through = require('through2');
5
5
var jsonArrayStreams = require ( '../index' ) ;
6
6
var Readable = require ( 'stream' ) . Readable ;
7
7
8
- function createStringifyStream ( dataCB ) {
8
+ function createStringifyStream ( dataCB , replacer , space ) {
9
9
var input = new Readable ( { objectMode : true } ) ;
10
10
input . _read = function ( ) { } ;
11
11
input
12
- . pipe ( jsonArrayStreams . stringify ( ) )
12
+ . pipe ( jsonArrayStreams . stringify ( replacer , space ) )
13
13
. pipe ( through . obj (
14
14
function ( chunk , enc , cb ) {
15
15
dataCB ( chunk ) ;
@@ -92,3 +92,18 @@ test("stringify stream", function (t) {
92
92
} ) ;
93
93
} ) ;
94
94
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
+
You can’t perform that action at this time.
0 commit comments