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.
package/dist/lib/index.js CHANGED
@@ -28,201 +28,134 @@ module.exports = __toCommonJS(src_exports);
28
28
  function createParser() {
29
29
  const root = {
30
30
  type: "Root",
31
- value: null
31
+ current: null
32
32
  };
33
33
  let currentRef = root;
34
- const getRoot = () => root.value;
34
+ const tryGetNextValue = (token) => {
35
+ if (token.type === "Keyword") {
36
+ return {
37
+ type: "Literal",
38
+ value: token.value
39
+ };
40
+ }
41
+ if (token.type === "String") {
42
+ return {
43
+ type: "Literal",
44
+ value: token.value
45
+ };
46
+ }
47
+ if (token.type === "Number") {
48
+ return {
49
+ type: "Literal",
50
+ value: token.value
51
+ };
52
+ }
53
+ if (token.type === "Symbol" && token.value === "[") {
54
+ return {
55
+ type: "Array",
56
+ children: [],
57
+ state: 0 /* Start */,
58
+ parent: currentRef,
59
+ current: null
60
+ };
61
+ }
62
+ if (token.type === "Symbol" && token.value === "{") {
63
+ return {
64
+ type: "Object",
65
+ children: [],
66
+ state: 0 /* Start */,
67
+ parent: currentRef,
68
+ current: null
69
+ };
70
+ }
71
+ return null;
72
+ };
73
+ const onArrayOrObjectFinish = () => {
74
+ currentRef = currentRef;
75
+ switch (currentRef.parent.type) {
76
+ case "Root":
77
+ return;
78
+ case "Array":
79
+ currentRef.parent.children.push(currentRef);
80
+ currentRef.parent.current = null;
81
+ currentRef = currentRef.parent;
82
+ return;
83
+ case "Property":
84
+ currentRef.parent.current = currentRef;
85
+ currentRef = currentRef.parent;
86
+ currentRef.parent.children.push(currentRef);
87
+ currentRef.parent.current = null;
88
+ currentRef = currentRef.parent;
89
+ return;
90
+ }
91
+ };
92
+ const onPropertyFinish = () => {
93
+ currentRef = currentRef;
94
+ currentRef.parent.children.push(currentRef);
95
+ currentRef.parent.current = null;
96
+ currentRef = currentRef.parent;
97
+ };
98
+ const getRoot = () => root.current;
35
99
  const write = (token) => {
36
100
  switch (currentRef.type) {
37
101
  case "Root":
38
- if (currentRef.value === null) {
39
- if (token.type === "Keyword") {
40
- currentRef.value = {
41
- type: "Literal",
42
- value: token.value
43
- };
44
- return;
45
- }
46
- if (token.type === "String") {
47
- currentRef.value = {
48
- type: "Literal",
49
- value: token.value
50
- };
51
- return;
52
- }
53
- if (token.type === "Number") {
54
- currentRef.value = {
55
- type: "Literal",
56
- value: token.value
57
- };
58
- return;
59
- }
60
- if (token.type === "Symbol" && token.value === "[") {
61
- currentRef.value = {
62
- type: "Array",
63
- children: [],
64
- state: 0 /* Start */,
65
- parent: currentRef,
66
- current: null
67
- };
68
- currentRef = currentRef.value;
69
- return;
70
- }
71
- if (token.type === "Symbol" && token.value === "{") {
72
- currentRef.value = {
73
- type: "Object",
74
- children: [],
75
- state: 0 /* Start */,
76
- parent: currentRef,
77
- current: null
78
- };
79
- currentRef = currentRef.value;
102
+ if (currentRef.current === null) {
103
+ const next = tryGetNextValue(token);
104
+ if (next) {
105
+ currentRef.current = next;
106
+ if (next.type !== "Literal") {
107
+ currentRef = next;
108
+ }
80
109
  return;
81
110
  }
82
111
  }
83
112
  throw new Error("解析错误");
84
113
  case "Array":
85
114
  switch (currentRef.state) {
86
- case 0 /* Start */:
115
+ case 0 /* Start */: {
87
116
  if (token.type === "Symbol" && token.value === "]") {
88
- switch (currentRef.parent.type) {
89
- case "Root":
90
- return;
91
- case "Array":
92
- currentRef.parent.children.push(currentRef);
93
- currentRef.parent.current = null;
94
- currentRef = currentRef.parent;
95
- return;
96
- case "Property":
97
- currentRef.parent.value = currentRef;
98
- currentRef = currentRef.parent;
99
- currentRef.parent.children.push(currentRef);
100
- currentRef.parent.current = null;
101
- currentRef = currentRef.parent;
102
- return;
103
- }
104
- }
105
- if (token.type === "Keyword") {
106
- currentRef.state = 1 /* Value */;
107
- currentRef.children.push({
108
- type: "Literal",
109
- value: token.value
110
- });
111
- return;
112
- }
113
- if (token.type === "String") {
114
- currentRef.state = 1 /* Value */;
115
- currentRef.children.push({
116
- type: "Literal",
117
- value: token.value
118
- });
119
- return;
120
- }
121
- if (token.type === "Number") {
122
- currentRef.state = 1 /* Value */;
123
- currentRef.children.push({
124
- type: "Literal",
125
- value: token.value
126
- });
127
- return;
128
- }
129
- if (token.type === "Symbol" && token.value === "[") {
130
- currentRef.state = 1 /* Value */;
131
- currentRef.current = {
132
- type: "Array",
133
- children: [],
134
- state: 0 /* Start */,
135
- parent: currentRef,
136
- current: null
137
- };
138
- currentRef = currentRef.current;
117
+ onArrayOrObjectFinish();
139
118
  return;
140
119
  }
141
- if (token.type === "Symbol" && token.value === "{") {
120
+ const next = tryGetNextValue(token);
121
+ if (next) {
142
122
  currentRef.state = 1 /* Value */;
143
- currentRef.current = {
144
- type: "Object",
145
- children: [],
146
- state: 0 /* Start */,
147
- parent: currentRef,
148
- current: null
149
- };
123
+ if (next.type === "Literal") {
124
+ currentRef.children.push(next);
125
+ currentRef.current = null;
126
+ } else {
127
+ currentRef.current = next;
128
+ currentRef = next;
129
+ }
150
130
  return;
151
131
  }
152
132
  throw new Error("解析错误");
133
+ }
153
134
  case 1 /* Value */:
154
135
  if (token.type === "Symbol" && token.value === "]") {
155
- switch (currentRef.parent.type) {
156
- case "Root":
157
- return;
158
- case "Array":
159
- currentRef.parent.children.push(currentRef);
160
- currentRef.parent.current = null;
161
- currentRef = currentRef.parent;
162
- return;
163
- case "Property":
164
- currentRef.parent.value = currentRef;
165
- currentRef = currentRef.parent;
166
- currentRef.parent.children.push(currentRef);
167
- currentRef.parent.current = null;
168
- currentRef = currentRef.parent;
169
- return;
170
- }
136
+ onArrayOrObjectFinish();
137
+ return;
171
138
  }
172
139
  if (token.type === "Symbol" && token.value === ",") {
173
140
  currentRef.state = 2 /* Comma */;
174
141
  return;
175
142
  }
176
143
  throw new Error("解析错误");
177
- case 2 /* Comma */:
178
- if (token.type === "Keyword") {
179
- currentRef.state = 1 /* Value */;
180
- currentRef.children.push({
181
- type: "Literal",
182
- value: token.value
183
- });
184
- return;
185
- }
186
- if (token.type === "String") {
187
- currentRef.state = 1 /* Value */;
188
- currentRef.children.push({
189
- type: "Literal",
190
- value: token.value
191
- });
192
- return;
193
- }
194
- if (token.type === "Number") {
195
- currentRef.state = 1 /* Value */;
196
- currentRef.children.push({
197
- type: "Literal",
198
- value: token.value
199
- });
200
- return;
201
- }
202
- if (token.type === "Symbol" && token.value === "[") {
144
+ case 2 /* Comma */: {
145
+ const next = tryGetNextValue(token);
146
+ if (next) {
203
147
  currentRef.state = 1 /* Value */;
204
- currentRef.current = {
205
- type: "Array",
206
- children: [],
207
- state: 0 /* Start */,
208
- parent: currentRef,
209
- current: null
210
- };
211
- currentRef = currentRef.current;
212
- return;
213
- }
214
- if (token.type === "Symbol" && token.value === "{") {
215
- currentRef.state = 1 /* Value */;
216
- currentRef.current = {
217
- type: "Object",
218
- children: [],
219
- state: 0 /* Start */,
220
- parent: currentRef,
221
- current: null
222
- };
148
+ if (next.type === "Literal") {
149
+ currentRef.children.push(next);
150
+ currentRef.current = null;
151
+ } else {
152
+ currentRef.current = next;
153
+ currentRef = next;
154
+ }
223
155
  return;
224
156
  }
225
157
  throw new Error("解析错误");
158
+ }
226
159
  }
227
160
  case "Property":
228
161
  switch (currentRef.state) {
@@ -232,65 +165,20 @@ function createParser() {
232
165
  return;
233
166
  }
234
167
  throw new Error("解析错误");
235
- case 1 /* Colon */:
236
- if (token.type === "Keyword") {
168
+ case 1 /* Colon */: {
169
+ const next = tryGetNextValue(token);
170
+ if (next) {
237
171
  currentRef.state = 2 /* Value */;
238
- currentRef.value = {
239
- type: "Literal",
240
- value: token.value
241
- };
242
- currentRef.parent.children.push(currentRef);
243
- currentRef.parent.current = null;
244
- currentRef = currentRef.parent;
245
- return;
246
- }
247
- if (token.type === "String") {
248
- currentRef.state = 2 /* Value */;
249
- currentRef.value = {
250
- type: "Literal",
251
- value: token.value
252
- };
253
- currentRef.parent.children.push(currentRef);
254
- currentRef.parent.current = null;
255
- currentRef = currentRef.parent;
256
- return;
257
- }
258
- if (token.type === "Number") {
259
- currentRef.state = 2 /* Value */;
260
- currentRef.value = {
261
- type: "Literal",
262
- value: token.value
263
- };
264
- currentRef.parent.children.push(currentRef);
265
- currentRef.parent.current = null;
266
- currentRef = currentRef.parent;
267
- return;
268
- }
269
- if (token.type === "Symbol" && token.value === "[") {
270
- currentRef.state = 2 /* Value */;
271
- currentRef.value = {
272
- type: "Array",
273
- children: [],
274
- state: 0 /* Start */,
275
- parent: currentRef,
276
- current: null
277
- };
278
- currentRef = currentRef.value;
279
- return;
280
- }
281
- if (token.type === "Symbol" && token.value === "{") {
282
- currentRef.state = 2 /* Value */;
283
- currentRef.value = {
284
- type: "Object",
285
- children: [],
286
- state: 0 /* Start */,
287
- parent: currentRef,
288
- current: null
289
- };
290
- currentRef = currentRef.value;
172
+ currentRef.current = next;
173
+ if (next.type === "Literal") {
174
+ onPropertyFinish();
175
+ } else {
176
+ currentRef = next;
177
+ }
291
178
  return;
292
179
  }
293
180
  throw new Error("解析错误");
181
+ }
294
182
  }
295
183
  case "Object":
296
184
  switch (currentRef.state) {
@@ -303,7 +191,7 @@ function createParser() {
303
191
  type: "Identifier",
304
192
  value: token.value
305
193
  },
306
- value: null,
194
+ current: null,
307
195
  state: 0 /* Key */,
308
196
  parent: currentRef
309
197
  };
@@ -311,42 +199,14 @@ function createParser() {
311
199
  return;
312
200
  }
313
201
  if (token.type === "Symbol" && token.value === "}") {
314
- switch (currentRef.parent.type) {
315
- case "Root":
316
- return;
317
- case "Array":
318
- currentRef.parent.children.push(currentRef);
319
- currentRef.parent.current = null;
320
- currentRef = currentRef.parent;
321
- return;
322
- case "Property":
323
- currentRef.parent.value = currentRef;
324
- currentRef = currentRef.parent;
325
- currentRef.parent.children.push(currentRef);
326
- currentRef.parent.current = null;
327
- currentRef = currentRef.parent;
328
- return;
329
- }
202
+ onArrayOrObjectFinish();
203
+ return;
330
204
  }
331
205
  throw new Error("解析错误");
332
206
  case 1 /* Property */:
333
207
  if (token.type === "Symbol" && token.value === "}") {
334
- switch (currentRef.parent.type) {
335
- case "Root":
336
- return;
337
- case "Array":
338
- currentRef.parent.children.push(currentRef);
339
- currentRef.parent.current = null;
340
- currentRef = currentRef.parent;
341
- return;
342
- case "Property":
343
- currentRef.parent.value = currentRef;
344
- currentRef = currentRef.parent;
345
- currentRef.parent.children.push(currentRef);
346
- currentRef.parent.current = null;
347
- currentRef = currentRef.parent;
348
- return;
349
- }
208
+ onArrayOrObjectFinish();
209
+ return;
350
210
  }
351
211
  if (token.type === "Symbol" && token.value === ",") {
352
212
  currentRef.state = 2 /* Comma */;
@@ -362,7 +222,7 @@ function createParser() {
362
222
  type: "Identifier",
363
223
  value: token.value
364
224
  },
365
- value: null,
225
+ current: null,
366
226
  state: 0 /* Key */,
367
227
  parent: currentRef
368
228
  };
@@ -405,7 +265,7 @@ function createTokenize() {
405
265
  if (current) {
406
266
  switch (current.type) {
407
267
  case "Keyword":
408
- if (char === keywords[current.matchedIndex + 1]) {
268
+ if (char === current.value[current.matchedIndex + 1]) {
409
269
  current.matchedIndex++;
410
270
  if (current.matchedIndex === current.value.length - 1) {
411
271
  const token = {
@@ -735,8 +595,8 @@ function getValueFromNode(node) {
735
595
  case "Object":
736
596
  return (node.current ? [...node.children, node.current] : node.children).reduce(
737
597
  (obj, property) => {
738
- if (property.value) {
739
- obj[property.key.value] = getValueFromNode(property.value);
598
+ if (property.current) {
599
+ obj[property.key.value] = getValueFromNode(property.current);
740
600
  }
741
601
  return obj;
742
602
  },