8000 tmp · ORESoftware/json-parser@e8b159f · GitHub
[go: up one dir, main page]

Skip to content

Commit e8b159f

Browse files
author
Alexander Mills
committed
tmp
1 parent a7d8d4f commit e8b159f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/main.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,27 @@ export interface JSONParserOpts {
1616
trackBytesWritten?: boolean,
1717
trackBytesRead?: boolean,
1818
includeByteCount?: boolean,
19-
emitNonJSON?: boolean
19+
emitNonJSON?: boolean,
20+
includeRawString?: boolean,
21+
stringifyNonJSON?: boolean
2022
}
2123

24+
export const RawStringSymbol = Symbol('raw.json.str');
2225
export const RawJSONBytesSymbol = Symbol('raw.json.bytes');
2326
export const JSONBytesSymbol = Symbol('json.bytes');
2427

25-
export class JSONParser extends stream.Transform {
28+
export class JSONParser<T = any> extends stream.Transform {
2629

2730
emitNonJSON = false;
2831
lastLineData = '';
2932
debug = false;
3033
delimiter = '\n';
3134
jpBytesWritten = 0;
35+
stringifyNonJSON = false;
3236
jpBytesRead = 0;
3337
isTrackBytesRead = false;
3438
isTrackBytesWritten = false;
39+
isIncludeRawString = false;
3540
isIncludeByteCount = false;
3641

3742
constructor(opts ?: JSONParserOpts) {
@@ -41,6 +46,10 @@ export class JSONParser extends stream.Transform {
4146
this.emitNonJSON = true;
4247
}
4348

49+
if (opts && opts.includeRawString) {
50+
this.isIncludeRawString = true;
51+
}
52+
4453
if (opts && opts.includeByteCount) {
4554
this.isIncludeByteCount = true;
4655
}
@@ -49,6 +58,10 @@ export class JSONParser extends stream.Transform {
4958
this.isTrackBytesWritten = true;
5059
}
5160

61+
if(opts && opts.stringifyNonJSON){
62+
this.stringifyNonJSON = true;
63+
}
64+
5265
if (opts && opts.trackBytesRead) {
5366
this.isTrackBytesRead = true;
5467
}
@@ -89,13 +102,21 @@ export class JSONParser extends stream.Transform {
89102
this.emit('string', o);
90103
}
91104

105+
if(this.stringifyNonJSON){
106+
this.emit('data', JSON.stringify(o));
107+
}
108+
92109
return;
93110
}
94111

95112
if (this.isIncludeByteCount && json && typeof json === 'object') {
96113
json[RawJSONBytesSymbol] = Buffer.byteLength(o);
97114
}
98115

116+
if (this.isIncludeRawString && json && typeof json === 'object') {
117+
json[RawStringSymbol] = o;
118+
}
119+
99120
this.push(json);
100121

101122
if (this.isTrackBytesWritten) {
@@ -140,7 +161,7 @@ export class JSONParser extends stream.Transform {
140161
this.handleJSON(this.lastLineData);
141162
this.lastLineData = '';
142163
}
143-
164+
144165
cb();
145166
}
146167

0 commit comments

Comments
 (0)
0