Skip to content
This repository was archived by the owner on Dec 4, 2018. It is now read-only.

Commit dad7b88

Browse files
committed
readable streams
1 parent 5bb776e commit dad7b88

File tree

2 files changed

+96
-59
lines changed

2 files changed

+96
-59
lines changed

index.js

Lines changed: 94 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#! /usr/bin/env node
22

33
var Parser = require('jsonparse')
4-
, through = require('through2').obj
4+
, Transform = require('readable-stream').Transform
5+
, inherits = require('inherits')
56

67
/*
78
@@ -12,16 +13,22 @@ var Parser = require('jsonparse')
1213
1314
*/
1415

15-
exports.parse = function (path, map) {
16+
exports.parse = Parse;
1617

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
2326
})
2427

28+
var stream = this;
29+
this.root = null;
30+
var parser = this.parser = new Parser()
31+
2532
if('string' === typeof path)
2633
path = path.split('.').map(function (e) {
2734
if (e === '*')
@@ -105,7 +112,13 @@ exports.parse = function (path, map) {
105112
}
106113

107114

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()
109122
}
110123

111124
function check (x, y) {
@@ -120,77 +133,100 @@ function check (x, y) {
120133
return false
121134
}
122135

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
125149
if (op === false){
126-
op = ''
127-
sep = '\n'
128-
cl = ''
150+
this.op = ''
151+
this.sep = '\n'
152+
this.cl = ''
129153
} else if (op == null) {
130154

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'
134158

135159
}
136160

137161
//else, what ever you like
138162

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+
}
155166

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()
157183
}
158184

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
161198
if (op === false){
162-
op = ''
163-
sep = '\n'
164-
cl = ''
199+
this.op = ''
200+
this.sep = '\n'
201+
this.cl = ''
165202
} else if (op == null) {
166203

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'
170207

171208
}
172209

173210
//else, what ever you like
174211

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)
190225

191-
return stream
226+
done()
192227
}
193228

229+
194230
if(!module.parent && process.title !== 'browser') {
195231
process.stdin
196232
.pipe(exports.parse(process.argv[2]))

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"parsing"
1717
],
1818
"dependencies": {
19+
"inherits": "^2.0.1",
1920
"jsonparse": "~1.0.0",
20-
"through2": "^0.6.3"
21+
"readable-stream": "^1.0.33"
2122
},
2223
"devDependencies": {
2324
"it-is": "~1",

0 commit comments

Comments
 (0)