Skip to content

Commit 3d5db65

Browse files
nlindleyjonathanong
authored andcommitted
Allow custom stringifier
closes #8
1 parent e8bcf65 commit 3d5db65

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ will yield something like
4444
* Documents are separated by `'\n,\n'`.
4545
* The stream is terminated with `'\n]\n'`.
4646

47+
## Stringifier
48+
49+
By default, [json-stringify-safe](https://www.npmjs.com/package/json-stringify-safe) is used to convert objects into strings. This can be configured with `options.stringifier`.
50+
4751
## API
4852

4953
### Stringify([options])
@@ -62,6 +66,7 @@ stringify.space = 2
6266
stringify.opener = '['
6367
stringify.seperator = ','
6468
stringify.closer = ']'
69+
stringify.stringifier = JSON.stringify
6570
```
6671

6772
[gitter-image]: https://badges.gitter.im/stream-utils/streaming-json-stringify.png

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ function Stringify(options) {
2525
Transform.call(this, options || {})
2626
this._writableState.objectMode = true
2727

28-
// Array Deliminator defaults
28+
// Array Deliminator and Stringifier defaults
2929
var opener = options && options.opener ? options.opener : '[\n'
3030
var seperator = options && options.seperator ? options.seperator : '\n,\n'
3131
var closer = options && options.closer ? options.closer : '\n]\n'
32+
var stringifier = options && options.stringifier ? options.stringifier : stringify
3233

33-
// Array Deliminators
34+
// Array Deliminators and Stringifier
3435
this.opener = new Buffer(opener, 'utf8')
3536
this.seperator = new Buffer(seperator, 'utf8')
3637
this.closer = new Buffer(closer, 'utf8')
38+
this.stringifier = stringifier
3739
}
3840

3941
// Flags
@@ -51,7 +53,7 @@ Stringify.prototype._transform = function (doc, enc, cb) {
5153
this.started = true
5254
}
5355

54-
doc = stringify(doc, this.replacer, this.space)
56+
doc = this.stringifier(doc, this.replacer, this.space)
5557

5658
this.push(new Buffer(doc, 'utf8'))
5759
cb()

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"repository": "stream-utils/streaming-json-stringify",
77
"license": "MIT",
88
"devDependencies": {
9-
"mocha": "2",
9+
"cat-stream": "*",
1010
"istanbul": "0",
11-
"cat-stream": "*"
11+
"mocha": "2",
12+
"sinon": "^1.17.2"
1213
},
1314
"scripts": {
1415
"test": "mocha --reporter spec",

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var PassThrough = require('readable-stream/passthrough')
33
var assert = require('assert')
44
var cat = require('cat-stream')
5+
var sinon = require('sinon');
56

67
var Stringify = require('..')
78

@@ -198,4 +199,29 @@ describe('Streamify()', function () {
198199
stream.write(3)
199200
stream.end()
200201
})
202+
203+
it('should allow custom stringifiers', function(done) {
204+
var stream = new PassThrough({
205+
objectMode: true
206+
})
207+
208+
var stringifier = sinon.spy(JSON.stringify);
209+
210+
var stringify = Stringify({stringifier: stringifier})
211+
212+
stream
213+
.pipe(stringify)
214+
.pipe(cat(function (err, buf) {
215+
assert.ifError(err)
216+
assert.ok(stringifier.called)
217+
assert.equal(buf.toString('utf8'), "[\n1\n,\n2\n,\n3\n]\n")
218+
219+
done()
220+
}))
221+
222+
stream.write(1)
223+
stream.write(2)
224+
stream.write(3)
225+
stream.end()
226+
})
201227
})

0 commit comments

Comments
 (0)