forked from uhop/stream-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringer.js
169 lines (155 loc) · 5.37 KB
/
Stringer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const {Transform} = require('stream');
const noCommaAfter = {startObject: 1, startArray: 1, endKey: 1, keyValue: 1},
noSpaceAfter = {endObject: 1, endArray: 1, '': 1},
noSpaceBefore = {startObject: 1, startArray: 1},
depthIncrement = {startObject: 1, startArray: 1},
depthDecrement = {endObject: 1, endArray: 1},
values = {startKey: 'keyValue', startString: 'stringValue', startNumber: 'numberValue'},
stopNames = {startKey: 'endKey', startString: 'endString', startNumber: 'endNumber'},
symbols = {
startObject: '{',
endObject: '}',
startArray: '[',
endArray: ']',
startKey: '"',
endKey: '":',
startString: '"',
endString: '"',
startNumber: '',
endNumber: '',
nullValue: 'null',
trueValue: 'true',
falseValue: 'false'
};
const symbolsEndKeyForKeepFormatter = '"';
const skipValue = endName =>
function(chunk, encoding, callback) {
if (chunk.name === endName) {
this._transform = this._prev_transform;
}
callback(null);
};
const replaceSymbols = {'\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t', '"': '\\"', '\\': '\\\\'};
const sanitizeString = value => value.replace(/[\b\f\n\r\t\"\\]/g, match => replaceSymbols[match]);
const doNothing = () => {};
class Stringer extends Transform {
static make(options) {
return new Stringer(options);
}
constructor(options) {
super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: false}));
this._values = {};
this.keepFormatter = false;
if (options) {
'useValues' in options && (this._values.keyValue = this._values.stringValue = this._values.numberValue = options.useValues);
'useKeyValues' in options && (this._values.keyValue = options.useKeyValues);
'useStringValues' in options && (this._values.stringValue = options.useStringValues);
'useNumberValues' in options && (this._values.numberValue = options.useNumberValues);
'keepFormatter' in options && (this.keepFormatter = options.keepFormatter);
this._makeArray = options.makeArray;
}
this._prev = '';
this._depth = 0;
if (this._makeArray) {
this._transform = this._arrayTransform;
this._flush = this._arrayFlush;
}
}
_arrayTransform(chunk, encoding, callback) {
// it runs once
delete this._transform;
this._transform({name: 'startArray'}, encoding, doNothing);
this._transform(chunk, encoding, callback);
}
_arrayFlush(callback) {
if (this._transform === this._arrayTransform) {
delete this._transform;
this._transform({name: 'startArray'}, null, doNothing);
}
this._transform({name: 'endArray'}, null, callback);
}
_transform(chunk, _, callback) {
if (this._values[chunk.name]) {
if (this._depth && noCommaAfter[this._prev] !== 1 && !this.keepFormatter) this.push(',');
switch (chunk.name) {
case 'keyValue':
if (!(this.keepFormatter && chunk.name === 'endKey')) {
this.push('"' + sanitizeString(chunk.value) + symbols.endKey);
} else {
this.push('"' + sanitizeString(chunk.value) + symbolsEndKeyForKeepFormatter);
}
break;
case 'stringValue':
this.push('"' + sanitizeString(chunk.value)+ '"');
break;
case 'numberValue':
this.push(chunk.value);
break;
case 'formatter':
this.keepFormatter && this.push(chunk.value);
break;
}
} else {
// filter out values
switch (chunk.name) {
case 'formatter':
this.keepFormatter && this.push(chunk.value);
break;
case 'endObject':
case 'endArray':
case 'endKey':
case 'endString':
case 'endNumber':
if (!(this.keepFormatter && chunk.name === 'endKey')) {
this.push(symbols[chunk.name]);
} else {
this.push(symbolsEndKeyForKeepFormatter);
}
break;
case 'stringChunk':
this.push(sanitizeString(chunk.value));
break;
case 'numberChunk':
this.push(chunk.value);
break;
case 'keyValue':
case 'stringValue':
case 'numberValue':
// skip completely
break;
case 'startKey':
case 'startString':
case 'startNumber':
if (this._values[values[chunk.name]]) {
this._prev_transform = this._transform;
this._transform = skipValue(stopNames[chunk.name]);
return callback(null);
}
// intentional fall down
default:
// case 'startObject': case 'startArray': case 'startKey': case 'startString':
// case 'startNumber': case 'nullValue': case 'trueValue': case 'falseValue':
if (this._depth) {
if (noCommaAfter[this._prev] !== 1 && !this.keepFormatter) this.push(',');
} else {
if (noSpaceAfter[this._prev] !== 1 && noSpaceBefore[chunk.name] !== 1) this.push(' ');
}
this.push(symbols[chunk.name]);
break;
}
if (depthIncrement[chunk.name]) {
++this._depth;
} else if (depthDecrement[chunk.name]) {
--this._depth;
}
}
if (chunk.name !== 'formatter') {
this._prev = chunk.name;
}
callback(null);
}
}
Stringer.stringer = Stringer.make;
Stringer.make.Constructor = Stringer;
module.exports = Stringer;