json-brook 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,201 +29,134 @@ module.exports = __toCommonJS(internal_exports);
29
29
  function createParser() {
30
30
  const root = {
31
31
  type: "Root",
32
- value: null
32
+ current: null
33
33
  };
34
34
  let currentRef = root;
35
- const getRoot = () => root.value;
35
+ const tryGetNextValue = (token) => {
36
+ if (token.type === "Keyword") {
37
+ return {
38
+ type: "Literal",
39
+ value: token.value
40
+ };
41
+ }
42
+ if (token.type === "String") {
43
+ return {
44
+ type: "Literal",
45
+ value: token.value
46
+ };
47
+ }
48
+ if (token.type === "Number") {
49
+ return {
50
+ type: "Literal",
51
+ value: token.value
52
+ };
53
+ }
54
+ if (token.type === "Symbol" && token.value === "[") {
55
+ return {
56
+ type: "Array",
57
+ children: [],
58
+ state: 0 /* Start */,
59
+ parent: currentRef,
60
+ current: null
61
+ };
62
+ }
63
+ if (token.type === "Symbol" && token.value === "{") {
64
+ return {
65
+ type: "Object",
66
+ children: [],
67
+ state: 0 /* Start */,
68
+ parent: currentRef,
69
+ current: null
70
+ };
71
+ }
72
+ return null;
73
+ };
74
+ const onArrayOrObjectFinish = () => {
75
+ currentRef = currentRef;
76
+ switch (currentRef.parent.type) {
77
+ case "Root":
78
+ return;
79
+ case "Array":
80
+ currentRef.parent.children.push(currentRef);
81
+ currentRef.parent.current = null;
82
+ currentRef = currentRef.parent;
83
+ return;
84
+ case "Property":
85
+ currentRef.parent.current = currentRef;
86
+ currentRef = currentRef.parent;
87
+ currentRef.parent.children.push(currentRef);
88
+ currentRef.parent.current = null;
89
+ currentRef = currentRef.parent;
90
+ return;
91
+ }
92
+ };
93
+ const onPropertyFinish = () => {
94
+ currentRef = currentRef;
95
+ currentRef.parent.children.push(currentRef);
96
+ currentRef.parent.current = null;
97
+ currentRef = currentRef.parent;
98
+ };
99
+ const getRoot = () => root.current;
36
100
  const write = (token) => {
37
101
  switch (currentRef.type) {
38
102
  case "Root":
39
- if (currentRef.value === null) {
40
- if (token.type === "Keyword") {
41
- currentRef.value = {
42
- type: "Literal",
43
- value: token.value
44
- };
45
- return;
46
- }
47
- if (token.type === "String") {
48
- currentRef.value = {
49
- type: "Literal",
50
- value: token.value
51
- };
52
- return;
53
- }
54
- if (token.type === "Number") {
55
- currentRef.value = {
56
- type: "Literal",
57
- value: token.value
58
- };
59
- return;
60
- }
61
- if (token.type === "Symbol" && token.value === "[") {
62
- currentRef.value = {
63
- type: "Array",
64
- children: [],
65
- state: 0 /* Start */,
66
- parent: currentRef,
67
- current: null
68
- };
69
- currentRef = currentRef.value;
70
- return;
71
- }
72
- if (token.type === "Symbol" && token.value === "{") {
73
- currentRef.value = {
74
- type: "Object",
75
- children: [],
76
- state: 0 /* Start */,
77
- parent: currentRef,
78
- current: null
79
- };
80
- currentRef = currentRef.value;
103
+ if (currentRef.current === null) {
104
+ const next = tryGetNextValue(token);
105
+ if (next) {
106
+ currentRef.current = next;
107
+ if (next.type !== "Literal") {
108
+ currentRef = next;
109
+ }
81
110
  return;
82
111
  }
83
112
  }
84
113
  throw new Error("解析错误");
85
114
  case "Array":
86
115
  switch (currentRef.state) {
87
- case 0 /* Start */:
116
+ case 0 /* Start */: {
88
117
  if (token.type === "Symbol" && token.value === "]") {
89
- switch (currentRef.parent.type) {
90
- case "Root":
91
- return;
92
- case "Array":
93
- currentRef.parent.children.push(currentRef);
94
- currentRef.parent.current = null;
95
- currentRef = currentRef.parent;
96
- return;
97
- case "Property":
98
- currentRef.parent.value = currentRef;
99
- currentRef = currentRef.parent;
100
- currentRef.parent.children.push(currentRef);
101
- currentRef.parent.current = null;
102
- currentRef = currentRef.parent;
103
- return;
104
- }
105
- }
106
- if (token.type === "Keyword") {
107
- currentRef.state = 1 /* Value */;
108
- currentRef.children.push({
109
- type: "Literal",
110
- value: token.value
111
- });
112
- return;
113
- }
114
- if (token.type === "String") {
115
- currentRef.state = 1 /* Value */;
116
- currentRef.children.push({
117
- type: "Literal",
118
- value: token.value
119
- });
120
- return;
121
- }
122
- if (token.type === "Number") {
123
- currentRef.state = 1 /* Value */;
124
- currentRef.children.push({
125
- type: "Literal",
126
- value: token.value
127
- });
128
- return;
129
- }
130
- if (token.type === "Symbol" && token.value === "[") {
131
- currentRef.state = 1 /* Value */;
132
- currentRef.current = {
133
- type: "Array",
134
- children: [],
135
- state: 0 /* Start */,
136
- parent: currentRef,
137
- current: null
138
- };
139
- currentRef = currentRef.current;
118
+ onArrayOrObjectFinish();
140
119
  return;
141
120
  }
142
- if (token.type === "Symbol" && token.value === "{") {
121
+ const next = tryGetNextValue(token);
122
+ if (next) {
143
123
  currentRef.state = 1 /* Value */;
144
- currentRef.current = {
145
- type: "Object",
146
- children: [],
147
- state: 0 /* Start */,
148
- parent: currentRef,
149
- current: null
150
- };
124
+ if (next.type === "Literal") {
125
+ currentRef.children.push(next);
126
+ currentRef.current = null;
127
+ } else {
128
+ currentRef.current = next;
129
+ currentRef = next;
130
+ }
151
131
  return;
152
132
  }
153
133
  throw new Error("解析错误");
134
+ }
154
135
  case 1 /* Value */:
155
136
  if (token.type === "Symbol" && token.value === "]") {
156
- switch (currentRef.parent.type) {
157
- case "Root":
158
- return;
159
- case "Array":
160
- currentRef.parent.children.push(currentRef);
161
- currentRef.parent.current = null;
162
- currentRef = currentRef.parent;
163
- return;
164
- case "Property":
165
- currentRef.parent.value = currentRef;
166
- currentRef = currentRef.parent;
167
- currentRef.parent.children.push(currentRef);
168
- currentRef.parent.current = null;
169
- currentRef = currentRef.parent;
170
- return;
171
- }
137
+ onArrayOrObjectFinish();
138
+ return;
172
139
  }
173
140
  if (token.type === "Symbol" && token.value === ",") {
174
141
  currentRef.state = 2 /* Comma */;
175
142
  return;
176
143
  }
177
144
  throw new Error("解析错误");
178
- case 2 /* Comma */:
179
- if (token.type === "Keyword") {
180
- currentRef.state = 1 /* Value */;
181
- currentRef.children.push({
182
- type: "Literal",
183
- value: token.value
184
- });
185
- return;
186
- }
187
- if (token.type === "String") {
188
- currentRef.state = 1 /* Value */;
189
- currentRef.children.push({
190
- type: "Literal",
191
- value: token.value
192
- });
193
- return;
194
- }
195
- if (token.type === "Number") {
196
- currentRef.state = 1 /* Value */;
197
- currentRef.children.push({
198
- type: "Literal",
199
- value: token.value
200
- });
201
- return;
202
- }
203
- if (token.type === "Symbol" && token.value === "[") {
145
+ case 2 /* Comma */: {
146
+ const next = tryGetNextValue(token);
147
+ if (next) {
204
148
  currentRef.state = 1 /* Value */;
205
- currentRef.current = {
206
- type: "Array",
207
- children: [],
208
- state: 0 /* Start */,
209
- parent: currentRef,
210
- current: null
211
- };
212
- currentRef = currentRef.current;
213
- return;
214
- }
215
- if (token.type === "Symbol" && token.value === "{") {
216
- currentRef.state = 1 /* Value */;
217
- currentRef.current = {
218
- type: "Object",
219
- children: [],
220
- state: 0 /* Start */,
221
- parent: currentRef,
222
- current: null
223
- };
149
+ if (next.type === "Literal") {
150
+ currentRef.children.push(next);
151
+ currentRef.current = null;
152
+ } else {
153
+ currentRef.current = next;
154
+ currentRef = next;
155
+ }
224
156
  return;
225
157
  }
226
158
  throw new Error("解析错误");
159
+ }
227
160
  }
228
161
  case "Property":
229
162
  switch (currentRef.state) {
@@ -233,65 +166,20 @@ function createParser() {
233
166
  return;
234
167
  }
235
168
  throw new Error("解析错误");
236
- case 1 /* Colon */:
237
- if (token.type === "Keyword") {
169
+ case 1 /* Colon */: {
170
+ const next = tryGetNextValue(token);
171
+ if (next) {
238
172
  currentRef.state = 2 /* Value */;
239
- currentRef.value = {
240
- type: "Literal",
241
- value: token.value
242
- };
243
- currentRef.parent.children.push(currentRef);
244
- currentRef.parent.current = null;
245
- currentRef = currentRef.parent;
246
- return;
247
- }
248
- if (token.type === "String") {
249
- currentRef.state = 2 /* Value */;
250
- currentRef.value = {
251
- type: "Literal",
252
- value: token.value
253
- };
254
- currentRef.parent.children.push(currentRef);
255
- currentRef.parent.current = null;
256
- currentRef = currentRef.parent;
257
- return;
258
- }
259
- if (token.type === "Number") {
260
- currentRef.state = 2 /* Value */;
261
- currentRef.value = {
262
- type: "Literal",
263
- value: token.value
264
- };
265
- currentRef.parent.children.push(currentRef);
266
- currentRef.parent.current = null;
267
- currentRef = currentRef.parent;
268
- return;
269
- }
270
- if (token.type === "Symbol" && token.value === "[") {
271
- currentRef.state = 2 /* Value */;
272
- currentRef.value = {
273
- type: "Array",
274
- children: [],
275
- state: 0 /* Start */,
276
- parent: currentRef,
277
- current: null
278
- };
279
- currentRef = currentRef.value;
280
- return;
281
- }
282
- if (token.type === "Symbol" && token.value === "{") {
283
- currentRef.state = 2 /* Value */;
284
- currentRef.value = {
285
- type: "Object",
286
- children: [],
287
- state: 0 /* Start */,
288
- parent: currentRef,
289
- current: null
290
- };
291
- currentRef = currentRef.value;
173
+ currentRef.current = next;
174
+ if (next.type === "Literal") {
175
+ onPropertyFinish();
176
+ } else {
177
+ currentRef = next;
178
+ }
292
179
  return;
293
180
  }
294
181
  throw new Error("解析错误");
182
+ }
295
183
  }
296
184
  case "Object":
297
185
  switch (currentRef.state) {
@@ -304,7 +192,7 @@ function createParser() {
304
192
  type: "Identifier",
305
193
  value: token.value
306
194
  },
307
- value: null,
195
+ current: null,
308
196
  state: 0 /* Key */,
309
197
  parent: currentRef
310
198
  };
@@ -312,42 +200,14 @@ function createParser() {
312
200
  return;
313
201
  }
314
202
  if (token.type === "Symbol" && token.value === "}") {
315
- switch (currentRef.parent.type) {
316
- case "Root":
317
- return;
318
- case "Array":
319
- currentRef.parent.children.push(currentRef);
320
- currentRef.parent.current = null;
321
- currentRef = currentRef.parent;
322
- return;
323
- case "Property":
324
- currentRef.parent.value = currentRef;
325
- currentRef = currentRef.parent;
326
- currentRef.parent.children.push(currentRef);
327
- currentRef.parent.current = null;
328
- currentRef = currentRef.parent;
329
- return;
330
- }
203
+ onArrayOrObjectFinish();
204
+ return;
331
205
  }
332
206
  throw new Error("解析错误");
333
207
  case 1 /* Property */:
334
208
  if (token.type === "Symbol" && token.value === "}") {
335
- switch (currentRef.parent.type) {
336
- case "Root":
337
- return;
338
- case "Array":
339
- currentRef.parent.children.push(currentRef);
340
- currentRef.parent.current = null;
341
- currentRef = currentRef.parent;
342
- return;
343
- case "Property":
344
- currentRef.parent.value = currentRef;
345
- currentRef = currentRef.parent;
346
- currentRef.parent.children.push(currentRef);
347
- currentRef.parent.current = null;
348
- currentRef = currentRef.parent;
349
- return;
350
- }
209
+ onArrayOrObjectFinish();
210
+ return;
351
211
  }
352
212
  if (token.type === "Symbol" && token.value === ",") {
353
213
  currentRef.state = 2 /* Comma */;
@@ -363,7 +223,7 @@ function createParser() {
363
223
  type: "Identifier",
364
224
  value: token.value
365
225
  },
366
- value: null,
226
+ current: null,
367
227
  state: 0 /* Key */,
368
228
  parent: currentRef
369
229
  };
@@ -406,7 +266,7 @@ function createTokenize() {
406
266
  if (current) {
407
267
  switch (current.type) {
408
268
  case "Keyword":
409
- if (char === keywords[current.matchedIndex + 1]) {
269
+ if (char === current.value[current.matchedIndex + 1]) {
410
270
  current.matchedIndex++;
411
271
  if (current.matchedIndex === current.value.length - 1) {
412
272
  const token = {
@@ -54,7 +54,7 @@ declare function createTokenize(): {
54
54
 
55
55
  type RootNode = {
56
56
  type: 'Root';
57
- value: ObjectNode | ArrayNode | LiteralNode | null;
57
+ current: ObjectNode | ArrayNode | LiteralNode | null;
58
58
  };
59
59
  type LiteralNode = {
60
60
  type: 'Literal';
@@ -84,7 +84,7 @@ declare enum PropertyNodeState {
84
84
  type PropertyNode = {
85
85
  type: 'Property';
86
86
  key: IdentifierNode;
87
- value: LiteralNode | ArrayNode | ObjectNode | null;
87
+ current: LiteralNode | ArrayNode | ObjectNode | null;
88
88
  state: PropertyNodeState;
89
89
  parent: ObjectNode;
90
90
  };
@@ -1 +1 @@
1
- export { m as createParser, l as createTokenize } from './internal-73736079.js';
1
+ export { m as createParser, l as createTokenize } from './internal-2ae957fd.js';
@@ -1 +1 @@
1
- export { i as ArrayNode, A as ArrayNodeState, I as IdentifierNode, c as KeywordCurrent, b as KeywordToken, K as KeywordValue, L as LiteralNode, h as NumberCurrent, N as NumberState, g as NumberToken, k as ObjectNode, O as ObjectNodeState, j as PropertyNode, P as PropertyNodeState, R as RootNode, f as StringCurrent, d as StringState, e as StringToken, a as SymbolToken, S as SymbolValue, T as Token } from './internal-73736079.js';
1
+ export { i as ArrayNode, A as ArrayNodeState, I as IdentifierNode, c as KeywordCurrent, b as KeywordToken, K as KeywordValue, L as LiteralNode, h as NumberCurrent, N as NumberState, g as NumberToken, k as ObjectNode, O as ObjectNodeState, j as PropertyNode, P as PropertyNodeState, R as RootNode, f as StringCurrent, d as StringState, e as StringToken, a as SymbolToken, S as SymbolValue, T as Token } from './internal-2ae957fd.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-brook",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "parse json data streamly",
5
5
  "keywords": [
6
6
  "json",
@@ -32,11 +32,14 @@
32
32
  "@biomejs/biome": "1.8.3",
33
33
  "@modern-js/module-tools": "2.60.3",
34
34
  "@modern-js/plugin-rspress": "1.33.1",
35
+ "@types/jest": "^29.5.14",
35
36
  "@types/node": "~16.11.7",
37
+ "jest": "^29.7.0",
36
38
  "lint-staged": "~13.1.0",
37
39
  "rimraf": "^6.0.1",
38
40
  "simple-git-hooks": "^2.11.1",
39
41
  "sort-package-json": "^2.10.1",
42
+ "ts-jest": "^29.2.5",
40
43
  "typescript": "~5.0.4"
41
44
  },
42
45
  "publishConfig": {
@@ -58,6 +61,7 @@
58
61
  "release": "modern release",
59
62
  "reset": "npx rimraf node_modules ./**/node_modules",
60
63
  "sort": "sort-package-json",
64
+ "test": "jest",
61
65
  "upgrade": "modern upgrade"
62
66
  }
63
67
  }