1
1
#! /usr/bin/env node
2
2
3
3
var Parser = require ( 'jsonparse' )
4
- , through = require ( 'through2' ) . obj
4
+ , Transform = require ( 'readable-stream' ) . Transform
5
+ , inherits = require ( 'inherits' )
5
6
6
7
/*
7
8
@@ -12,16 +13,22 @@ var Parser = require('jsonparse')
12
13
13
14
*/
14
15
15
- exports . parse = function ( path , map ) {
16
+ exports . parse = Parse ;
16
17
17
- var parser = new Parser ( )
18
- var stream = through ( function ( chunk , encoding , next ) {
19
- if ( 'string' === typeof encoding )
20
- chunk = new Buffer ( chunk , encoding )
21
- parser . write ( chunk )
22
- next ( )
18
+ inherits ( Parse , Transform )
19
+
20
+ function Parse ( path , map ) {
21
+ if ( ! ( this instanceof Parse ) )
22
+ return new Parse ( path , map )
23
+
24
+ Transform . call ( this , {
25
+ objectMode : true
23
26
} )
24
27
28
+ var stream = this ;
29
+ this . root = null ;
30
+ var parser = this . parser = new Parser ( )
31
+
25
32
if ( 'string' === typeof path )
26
33
path = path . split ( '.' ) . map ( function ( e ) {
27
34
if ( e === '*' )
@@ -105,7 +112,13 @@ exports.parse = function (path, map) {
105
112
}
106
113
107
114
108
- return stream
115
+ }
116
+
117
+ Parse . prototype . _transform = function ( chunk , encoding , next ) {
118
+ if ( 'string' === typeof encoding )
119
+ chunk = new Buffer ( chunk , encoding )
120
+ this . parser . write ( chunk )
121
+ next ( )
109
122
}
110
123
111
124
function check ( x , y ) {
@@ -120,77 +133,100 @@ function check (x, y) {
120
133
return false
121
134
}
122
135
123
- exports . stringify = function ( op , sep , cl , indent ) {
124
- indent = indent || 0
136
+ exports . stringify = Stringify ;
137
+
138
+ inherits ( Stringify , Transform )
139
+
140
+ function Stringify ( op , sep , cl , indent ) {
141
+ if ( ! ( this instanceof Stringify ) )
142
+ return new Stringify ( op , sep , cl , indent )
143
+
144
+ Transform . call ( this , {
145
+ objectMode : true
146
+ } )
147
+
148
+ this . indent = indent || 0
125
149
if ( op === false ) {
126
- op = ''
127
- sep = '\n'
128
- cl = ''
150
+ this . op = ''
151
+ this . sep = '\n'
152
+ this . cl = ''
129
153
} else if ( op == null ) {
130
154
131
- op = '[\n'
132
- sep = '\n,\n'
133
- cl = '\n]\n'
155
+ this . op = '[\n'
156
+ this . sep = '\n,\n'
157
+ this . cl = '\n]\n'
134
158
135
159
}
136
160
137
161
//else, what ever you like
138
162
139
- var stream
140
- , first = true
141
- , anyData = false
142
- stream = through ( function ( data , _ , next ) {
143
- anyData = true
144
- var json = JSON . stringify ( data , null , indent )
145
- if ( first ) { first = false ; stream . push ( op + json ) }
146
- else stream . push ( sep + json )
147
- next ( )
148
- } ,
149
- function ( done ) {
150
- if ( ! anyData )
151
- stream . push ( op )
152
- stream . push ( cl )
153
- done ( )
154
- } )
163
+ this . first = true
164
+ this . anyData = false
165
+ }
155
166
156
- return stream
167
+ Stringify . prototype . _transform = function ( data , _ , next ) {
168
+ this . anyData = true
169
+ var json = JSON . stringify ( data , null , this . indent )
170
+ if ( this . first ) {
171
+ this . first = false ;
172
+ this . push ( this . op + json )
173
+ }
174
+ else this . push ( this . sep + json )
175
+ next ( )
176
+ }
177
+
178
+ Stringify . prototype . _flush = function ( done ) {
179
+ if ( ! this . anyData )
180
+ this . push ( this . op )
181
+ this . push ( this . cl )
182
+ done ( )
157
183
}
158
184
159
- exports . stringifyObject = function ( op , sep , cl , indent ) {
160
- indent = indent || 0
185
+ exports . stringifyObject = StringifyObject ;
186
+
187
+ inherits ( StringifyObject , Transform )
188
+
189
+ function StringifyObject ( op , sep , cl , indent ) {
190
+ if ( ! ( this instanceof StringifyObject ) )
191
+ return new StringifyObject ( op , sep , cl , indent )
192
+
193
+ Transform . call ( this , {
194
+ objectMode : true
195
+ } )
196
+
197
+ this . indent = indent || 0
161
198
if ( op === false ) {
162
- op = ''
163
- sep = '\n'
164
- cl = ''
199
+ this . op = ''
200
+ this . sep = '\n'
201
+ this . cl = ''
165
202
} else if ( op == null ) {
166
203
167
- op = '{\n'
168
- sep = '\n,\n'
169
- cl = '\n}\n'
204
+ this . op = '{\n'
205
+ this . sep = '\n,\n'
206
+ this . cl = '\n}\n'
170
207
171
208
}
172
209
173
210
//else, what ever you like
174
211
175
- var first = true
176
- , anyData = false
177
- stream = through ( function ( data , _ , next ) {
178
- anyData = true
179
- var json = JSON . stringify ( data [ 0 ] ) + ':' + JSON . stringify ( data [ 1 ] , null , indent )
180
- if ( first ) { first = false ; this . push ( op + json ) }
181
- else this . push ( sep + json )
182
- next ( )
183
- } ,
184
- function ( done ) {
185
- if ( ! anyData ) this . push ( op )
186
- this . push ( cl )
187
-
188
- done ( )
189
- } )
212
+ this . first = true
213
+ this . anyData = false
214
+ }
215
+ StringifyObject . prototype . _transform = function ( data , _ , next ) {
216
+ this . anyData = true
217
+ var json = JSON . stringify ( data [ 0 ] ) + ':' + JSON . stringify ( data [ 1 ] , null , this . indent )
218
+ if ( this . first ) { this . first = false ; this . push ( this . op + json ) }
219
+ else this . push ( this . sep + json )
220
+ next ( )
221
+ }
222
+ StringifyObject . prototype . _flush = function ( done ) {
223
+ if ( ! this . anyData ) this . push ( this . op )
224
+ this . push ( this . cl )
190
225
191
- return stream
226
+ done ( )
192
227
}
193
228
229
+
194
230
if ( ! module . parent && process . title !== 'browser' ) {
195
231
process . stdin
196
232
. pipe ( exports . parse ( process . argv [ 2 ] ) )
0 commit comments