Skip to content

Commit fac3c14

Browse files
Allan Ebdrupjonathanong
authored andcommitted
allow passing space and replacer in options
1 parent c5feca9 commit fac3c14

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ module.exports = Stringify
1616
function Stringify(options) {
1717
if (!(this instanceof Stringify))
1818
return new Stringify(options || {})
19-
19+
if (options && options.replacer) {
20+
this.replacer = options.replacer;
21+
}
22+
if (options && options.space !== undefined) {
23+
this.space = options.space;
24+
}
2025
Transform.call(this, options || {})
2126
this._writableState.objectMode = true
2227
}

test/test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,55 @@ describe('Streamify()', function () {
105105

106106
stream.end(obj)
107107
})
108+
it('should allow a space argument to JSON.stringify()', function (done) {
109+
var stream = new PassThrough({
110+
objectMode: true
111+
})
112+
113+
var stringify = Stringify({space:2})
114+
115+
var obj = {
116+
a: 1
117+
}
118+
119+
stream
120+
.pipe(stringify)
121+
.pipe(cat(function (err, buf) {
122+
assert.ifError(err)
123+
assert.equal(buf.toString('utf8'), '[\n' + JSON.stringify(obj, null, 2) + '\n]\n')
124+
125+
done()
126+
}))
127+
128+
stream.end(obj)
129+
})
130+
131+
it('should allow a space argument to JSON.stringify()', function (done) {
132+
var stream = new PassThrough({
133+
objectMode: true
134+
})
135+
136+
var replacer = function(key, value){
137+
if(key === 'a') return undefined
138+
return value
139+
}
140+
141+
var stringify = Stringify({replacer:replacer})
142+
143+
var obj = {
144+
a: 1
145+
}
146+
147+
stream
148+
.pipe(stringify)
149+
.pipe(cat(function (err, buf) {
150+
assert.ifError(err)
151+
assert.equal(buf.toString('utf8'), '[\n' + JSON.stringify({}, null, 2) + '\n]\n')
152+
153+
done()
154+
}))
155+
156+
stream.end(obj)
157+
})
158+
108159
})

0 commit comments

Comments
 (0)