Skip to content

Commit

Permalink
Add custom seperator and open/close options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Ballast authored and jonathanong committed Jul 15, 2015
1 parent c76814f commit 574695a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ function Stringify(options) {
}
Transform.call(this, options || {})
this._writableState.objectMode = true

// Array Deliminator defaults
var open = options && options.open ? options.open : '[\n'
var seperator = options && options.seperator ? options.seperator : '\n,\n'
var close = options && options.close ? options.close : '\n]\n'

// Array Deliminators
this.open = new Buffer(open, 'utf8')
this.seperator = new Buffer(seperator, 'utf8')
this.close = new Buffer(close, 'utf8')
}

// Flags
Stringify.prototype.started = false

// Array delimiters
Stringify.prototype.open = new Buffer('[\n', 'utf8')
Stringify.prototype.seperator = new Buffer('\n,\n', 'utf8')
Stringify.prototype.close = new Buffer('\n]\n', 'utf8')

// JSON.stringify options
Stringify.prototype.replacer = null
Stringify.prototype.space = 0
Expand Down
42 changes: 42 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,46 @@ describe('Streamify()', function () {
stream.end(obj)
})

it('should allow custom openings and closings', function (done) {
var stream = new PassThrough({
objectMode: true
})

var stringify = Stringify({open: "{\"test\": [\n", close: "\n]}\n"})

var obj = [{a: 1}]

stream
.pipe(stringify)
.pipe(cat(function (err, buf) {
assert.ifError(err)
assert.equal(buf.toString('utf8'), "{\"test\": [\n" + JSON.stringify(obj, null) + "\n]}\n")

done()
}))

stream.end(obj)
})

it('should allow custom seperators', function (done) {
var stream = new PassThrough({
objectMode: true
})

var stringify = Stringify({seperator: ' , '})

stream
.pipe(stringify)
.pipe(cat(function (err, buf) {
assert.ifError(err)
assert.equal(buf.toString('utf8'), "[\n1 , 2 , 3\n]\n")

done()
}))

stream.write(1)
stream.write(2)
stream.write(3)
stream.end()
})
})

0 comments on commit 574695a

Please sign in to comment.