/
index.js
70 lines (55 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var Transform = require('stream').Transform
, util = require('util')
, os = require('os')
;
if (!Transform) {
Transform = require('readable-stream/transform');
}
function Stringify(opts, replacer, space) {
if (!(this instanceof(Stringify)))
return new Stringify(opts, replacer, space);
Transform.call(this, {objectMode:true, decodeStrings:false});
if (!opts) opts = {};
this._state = {
open: opts.open,
sep: ((!opts.open || !opts.close)? os.EOL : (opts.sep? opts.sep : ',')),
close: ((opts.open && opts.close)? opts.close : null),
replacer: replacer,
space: space,
first: true
};
if (this._state.first && this._state.open) {
this.push(new Buffer(this._state.open));
}
}
util.inherits(Stringify, Transform);
Stringify.prototype._transform = function(chunk, encoding, done) {
var buf, str, error = null;
try {
str = !Buffer.isBuffer(chunk)? chunk : chunk.toString(encoding);
str = JSON.stringify(str, this._state.replacer, this._state.space);
if (!this._state.first && this._state.sep) {
buf = new Buffer(str.length + this._state.sep.length);
buf.write(this._state.sep, 0);
buf.write(str, this._state.sep.length);
} else {
buf = new Buffer(str);
}
this.push(buf);
this._state.first = false;
} catch (e) {
error = e;
}
done(error);
};
Stringify.prototype._flush = function(done) {
if (this._state.close) {
this.push(new Buffer(this._state.close));
} else if (true === this._state.first) {
this.push(new Buffer(''));
}
done();
};
module.exports = function(opts, replacer, space) {
return new Stringify(opts, replacer, space);
};