json-brook 0.1.0-beta.0 → 0.1.0-beta.1
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/README.md +4 -4
- package/dist/generate.d.ts +6 -2
- package/dist/index.d.ts +2 -5
- package/dist/index.js +78 -73
- package/dist/index.umd.cjs +2 -2
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -23,11 +23,11 @@ const sample = JSON.stringify(
|
|
|
23
23
|
|
|
24
24
|
for (const char of sample) {
|
|
25
25
|
jsonBrook.parse(char);
|
|
26
|
-
console.log(generate
|
|
26
|
+
console.log(jsonBrook.generate());
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
jsonBrook.end();
|
|
30
|
-
console.log(generate
|
|
30
|
+
console.log(jsonBrook.generate());
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
## API
|
|
@@ -38,6 +38,7 @@ console.log(generate.simpleGenerator(jsonBrook));
|
|
|
38
38
|
* `end` 代表流结束,仅针对某些特殊场景,如纯数字形式的Json字符串
|
|
39
39
|
* `getRoot`: 获取当前解析出的ast根节点
|
|
40
40
|
* `getCurrent` 获取当前正在解析的ast节点
|
|
41
|
+
* `generate` 生成当前解析结果,其中字符串和数字尽可能解析合法内容,对于true/false/null,只要识别开头就返回
|
|
41
42
|
|
|
42
43
|
### tokenize导出
|
|
43
44
|
该模块主要是token解析相关方法
|
|
@@ -46,8 +47,7 @@ console.log(generate.simpleGenerator(jsonBrook));
|
|
|
46
47
|
该模块主要是ast解析相关方法
|
|
47
48
|
|
|
48
49
|
### generate导出
|
|
49
|
-
|
|
50
|
-
* `simpleGenerator` 生成逻辑为激进模式,可返回未解析完成的字符串/数字,对于true/false/null,就提前返回
|
|
50
|
+
该模块主要是默认生成方法
|
|
51
51
|
|
|
52
52
|
## 在线尝试
|
|
53
53
|
[Playground](https://asurance.github.io/json-brook/)
|
package/dist/generate.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
1
|
+
import { ArrayNode, LiteralNode, ObjectNode, RootNode } from '.';
|
|
2
|
+
export declare const literalGenerator: (node: LiteralNode) => any;
|
|
3
|
+
export declare const arrayGenerator: (node: ArrayNode) => unknown[];
|
|
4
|
+
export declare const objectGenerator: (node: ObjectNode) => Record<string, unknown>;
|
|
5
|
+
export declare const normalGenerator: (node: LiteralNode | ArrayNode | ObjectNode) => unknown;
|
|
6
|
+
export declare const rootGenerator: (node: RootNode) => unknown;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,14 +5,11 @@ export type * from './generate';
|
|
|
5
5
|
export type * from './parse';
|
|
6
6
|
export type * from './tokenize';
|
|
7
7
|
export { generate, parse, tokenize };
|
|
8
|
-
export type JsonBrook =
|
|
9
|
-
getRoot: () => parse.RootNode;
|
|
10
|
-
getCurrent: () => parse.Node;
|
|
11
|
-
parse: (char: string) => void;
|
|
12
|
-
};
|
|
8
|
+
export type JsonBrook = ReturnType<typeof createJsonBrook>;
|
|
13
9
|
export declare const createJsonBrook: () => {
|
|
14
10
|
getRoot: () => parse.RootNode;
|
|
15
11
|
getCurrent: () => parse.Node;
|
|
12
|
+
generate: () => unknown;
|
|
16
13
|
parse: (char: string) => void;
|
|
17
14
|
end: () => void;
|
|
18
15
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const E = (e) => {
|
|
2
2
|
if (e.current)
|
|
3
3
|
switch (e.current.type) {
|
|
4
4
|
case "keyword":
|
|
@@ -13,24 +13,28 @@ const V = (e) => {
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
return e.value;
|
|
16
|
-
},
|
|
17
|
-
(t, r) => (r.value && (t[r.key.value] =
|
|
16
|
+
}, h = (e) => e.children.map((t) => k(t)), m = (e) => e.children.reduce(
|
|
17
|
+
(t, r) => (r.value && (t[r.key.value] = k(r.value)), t),
|
|
18
18
|
{}
|
|
19
|
-
),
|
|
19
|
+
), k = (e) => {
|
|
20
20
|
switch (e.type) {
|
|
21
21
|
case "literal":
|
|
22
|
-
return
|
|
22
|
+
return E(e);
|
|
23
23
|
case "array":
|
|
24
|
-
return
|
|
24
|
+
return h(e);
|
|
25
25
|
case "object":
|
|
26
|
-
return
|
|
26
|
+
return m(e);
|
|
27
27
|
}
|
|
28
|
-
},
|
|
28
|
+
}, S = (e) => {
|
|
29
29
|
if (e.value)
|
|
30
|
-
return
|
|
31
|
-
},
|
|
30
|
+
return k(e.value);
|
|
31
|
+
}, G = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
32
32
|
__proto__: null,
|
|
33
|
-
|
|
33
|
+
arrayGenerator: h,
|
|
34
|
+
literalGenerator: E,
|
|
35
|
+
normalGenerator: k,
|
|
36
|
+
objectGenerator: m,
|
|
37
|
+
rootGenerator: S
|
|
34
38
|
}, Symbol.toStringTag, { value: "Module" })), p = {
|
|
35
39
|
Normal: "Normal",
|
|
36
40
|
Escape: "Escape"
|
|
@@ -42,14 +46,14 @@ const V = (e) => {
|
|
|
42
46
|
DigitFraction: "DigitFraction",
|
|
43
47
|
Exp: "Exp",
|
|
44
48
|
ExpDigitOrSign: "ExpDigitOrSign"
|
|
45
|
-
},
|
|
46
|
-
`, "\r"],
|
|
47
|
-
if (
|
|
49
|
+
}, C = [" ", " ", `
|
|
50
|
+
`, "\r"], _ = ["{", "}", "[", "]", ":", ","], b = ["true", "false", "null"], J = b.map((e) => e[0]), K = ['"', "\\", "/", "b", "f", "n", "r", "t"], Z = ["+", "-"], c = (e) => e >= "0" && e <= "9", N = (e) => e >= "1" && e <= "9", F = (e) => c(e) || e >= "a" && e <= "f" || e >= "A" && e <= "F", w = (e) => e === "e" || e === "E", i = (e) => {
|
|
51
|
+
if (C.includes(e))
|
|
48
52
|
return {
|
|
49
53
|
token: null,
|
|
50
54
|
current: null
|
|
51
55
|
};
|
|
52
|
-
if (
|
|
56
|
+
if (_.includes(e))
|
|
53
57
|
return {
|
|
54
58
|
token: {
|
|
55
59
|
type: "symbol",
|
|
@@ -57,13 +61,13 @@ const V = (e) => {
|
|
|
57
61
|
},
|
|
58
62
|
current: null
|
|
59
63
|
};
|
|
60
|
-
const t =
|
|
64
|
+
const t = J.indexOf(e);
|
|
61
65
|
if (t !== -1)
|
|
62
66
|
return {
|
|
63
67
|
token: null,
|
|
64
68
|
current: {
|
|
65
69
|
type: "keyword",
|
|
66
|
-
value:
|
|
70
|
+
value: b[t],
|
|
67
71
|
matchLength: 1
|
|
68
72
|
}
|
|
69
73
|
};
|
|
@@ -97,7 +101,7 @@ const V = (e) => {
|
|
|
97
101
|
validLength: 1
|
|
98
102
|
}
|
|
99
103
|
};
|
|
100
|
-
if (
|
|
104
|
+
if (N(e))
|
|
101
105
|
return {
|
|
102
106
|
token: null,
|
|
103
107
|
current: {
|
|
@@ -108,7 +112,7 @@ const V = (e) => {
|
|
|
108
112
|
}
|
|
109
113
|
};
|
|
110
114
|
throw new Error("解析失败");
|
|
111
|
-
},
|
|
115
|
+
}, x = (e, t) => {
|
|
112
116
|
if (e === t.value[t.matchLength])
|
|
113
117
|
return t.matchLength++, t.matchLength === t.value.length ? {
|
|
114
118
|
token: {
|
|
@@ -168,7 +172,7 @@ const V = (e) => {
|
|
|
168
172
|
});
|
|
169
173
|
throw new Error("解析失败");
|
|
170
174
|
}
|
|
171
|
-
},
|
|
175
|
+
}, L = (e, t) => {
|
|
172
176
|
switch (t.state) {
|
|
173
177
|
case a.Negative:
|
|
174
178
|
if (e === "0")
|
|
@@ -176,7 +180,7 @@ const V = (e) => {
|
|
|
176
180
|
token: null,
|
|
177
181
|
current: t
|
|
178
182
|
};
|
|
179
|
-
if (
|
|
183
|
+
if (N(e))
|
|
180
184
|
return t.state = a.Digit, t.value += e, t.validLength = t.value.length, {
|
|
181
185
|
token: null,
|
|
182
186
|
current: t
|
|
@@ -186,7 +190,7 @@ const V = (e) => {
|
|
|
186
190
|
return e === "." ? (t.state = a.Point, t.value += e, {
|
|
187
191
|
token: null,
|
|
188
192
|
current: t
|
|
189
|
-
}) :
|
|
193
|
+
}) : w(e) ? (t.state = a.Exp, t.value += e, {
|
|
190
194
|
token: null,
|
|
191
195
|
current: t
|
|
192
196
|
}) : {
|
|
@@ -204,7 +208,7 @@ const V = (e) => {
|
|
|
204
208
|
}) : e === "." ? (t.state = a.Point, t.value += e, {
|
|
205
209
|
token: null,
|
|
206
210
|
current: t
|
|
207
|
-
}) :
|
|
211
|
+
}) : w(e) ? (t.state = a.Exp, t.value += e, {
|
|
208
212
|
token: null,
|
|
209
213
|
current: t
|
|
210
214
|
}) : {
|
|
@@ -226,7 +230,7 @@ const V = (e) => {
|
|
|
226
230
|
return c(e) ? (t.value += e, t.validLength = t.value.length, {
|
|
227
231
|
token: null,
|
|
228
232
|
current: t
|
|
229
|
-
}) :
|
|
233
|
+
}) : w(e) ? (t.state = a.Exp, t.value += e, {
|
|
230
234
|
token: null,
|
|
231
235
|
current: t
|
|
232
236
|
}) : {
|
|
@@ -266,21 +270,21 @@ const V = (e) => {
|
|
|
266
270
|
};
|
|
267
271
|
throw new Error("解析失败");
|
|
268
272
|
}
|
|
269
|
-
},
|
|
273
|
+
}, T = (e) => {
|
|
270
274
|
if (e.validLength === e.value.length)
|
|
271
275
|
return {
|
|
272
276
|
type: "number",
|
|
273
277
|
value: JSON.parse(e.value)
|
|
274
278
|
};
|
|
275
279
|
throw new Error("解析失败");
|
|
276
|
-
},
|
|
280
|
+
}, z = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
277
281
|
__proto__: null,
|
|
278
282
|
NumberTokenState: a,
|
|
279
283
|
StringTokenState: p,
|
|
280
|
-
endNumberTokenCurrent:
|
|
281
|
-
parseNextToken:
|
|
282
|
-
parseNextTokenWithKeyword:
|
|
283
|
-
parseNextTokenWithNumber:
|
|
284
|
+
endNumberTokenCurrent: T,
|
|
285
|
+
parseNextToken: i,
|
|
286
|
+
parseNextTokenWithKeyword: x,
|
|
287
|
+
parseNextTokenWithNumber: L,
|
|
284
288
|
parseNextTokenWithString: y
|
|
285
289
|
}, Symbol.toStringTag, { value: "Module" })), l = {
|
|
286
290
|
Start: "Start",
|
|
@@ -296,7 +300,7 @@ const V = (e) => {
|
|
|
296
300
|
Property: "Property",
|
|
297
301
|
Comma: "Comma",
|
|
298
302
|
End: "End"
|
|
299
|
-
},
|
|
303
|
+
}, o = (e, t) => {
|
|
300
304
|
switch (e.type) {
|
|
301
305
|
case "root":
|
|
302
306
|
if (t)
|
|
@@ -311,14 +315,14 @@ const V = (e) => {
|
|
|
311
315
|
return t ? g(e.parent, t) : e.parent;
|
|
312
316
|
throw new Error("解析失败");
|
|
313
317
|
}
|
|
314
|
-
},
|
|
318
|
+
}, O = (e, t) => {
|
|
315
319
|
if (e.value) {
|
|
316
|
-
const r =
|
|
320
|
+
const r = i(t);
|
|
317
321
|
if (!r.token && !r.current)
|
|
318
322
|
return e;
|
|
319
323
|
throw new Error("解析失败");
|
|
320
324
|
} else {
|
|
321
|
-
const r =
|
|
325
|
+
const r = i(t);
|
|
322
326
|
if (r.current)
|
|
323
327
|
return e.value = {
|
|
324
328
|
type: "literal",
|
|
@@ -346,27 +350,27 @@ const V = (e) => {
|
|
|
346
350
|
}
|
|
347
351
|
throw new Error("解析失败");
|
|
348
352
|
}
|
|
349
|
-
},
|
|
353
|
+
}, V = (e, t) => {
|
|
350
354
|
if (e.current)
|
|
351
355
|
switch (e.current.type) {
|
|
352
356
|
case "keyword": {
|
|
353
|
-
const r =
|
|
354
|
-
return r.token ? (e.value = r.token.value, e.current = null,
|
|
357
|
+
const r = x(t, e.current);
|
|
358
|
+
return r.token ? (e.value = r.token.value, e.current = null, o(e.parent)) : (e.current = r.current, e);
|
|
355
359
|
}
|
|
356
360
|
case "string": {
|
|
357
361
|
const r = y(t, e.current);
|
|
358
|
-
return r.token ? (e.value = r.token.value, e.current = null,
|
|
362
|
+
return r.token ? (e.value = r.token.value, e.current = null, o(e.parent)) : (e.current = r.current, e);
|
|
359
363
|
}
|
|
360
364
|
case "number": {
|
|
361
|
-
const r =
|
|
362
|
-
return r.token ? (e.value = r.token.value, e.current = null,
|
|
365
|
+
const r = L(t, e.current);
|
|
366
|
+
return r.token ? (e.value = r.token.value, e.current = null, o(e.parent, r.char)) : (e.current = r.current, e);
|
|
363
367
|
}
|
|
364
368
|
}
|
|
365
369
|
throw new Error("解析失败");
|
|
366
370
|
}, f = (e, t) => {
|
|
367
371
|
switch (e.state) {
|
|
368
372
|
case l.Start: {
|
|
369
|
-
const r =
|
|
373
|
+
const r = i(t);
|
|
370
374
|
if (r.current) {
|
|
371
375
|
e.state = l.Value;
|
|
372
376
|
const n = {
|
|
@@ -381,7 +385,7 @@ const V = (e) => {
|
|
|
381
385
|
return e;
|
|
382
386
|
switch (r.token.value) {
|
|
383
387
|
case "]":
|
|
384
|
-
return e.state = l.End,
|
|
388
|
+
return e.state = l.End, o(e.parent);
|
|
385
389
|
case "[": {
|
|
386
390
|
e.state = l.Value;
|
|
387
391
|
const n = {
|
|
@@ -406,7 +410,7 @@ const V = (e) => {
|
|
|
406
410
|
throw new Error("解析失败");
|
|
407
411
|
}
|
|
408
412
|
case l.Value: {
|
|
409
|
-
const r =
|
|
413
|
+
const r = i(t);
|
|
410
414
|
if (r.current)
|
|
411
415
|
throw new Error("解析失败");
|
|
412
416
|
if (!r.token)
|
|
@@ -415,12 +419,12 @@ const V = (e) => {
|
|
|
415
419
|
case ",":
|
|
416
420
|
return e.state = l.Comma, e;
|
|
417
421
|
case "]":
|
|
418
|
-
return e.state = l.End,
|
|
422
|
+
return e.state = l.End, o(e.parent);
|
|
419
423
|
}
|
|
420
424
|
throw new Error("解析失败");
|
|
421
425
|
}
|
|
422
426
|
case l.Comma: {
|
|
423
|
-
const r =
|
|
427
|
+
const r = i(t);
|
|
424
428
|
if (r.current) {
|
|
425
429
|
e.state = l.Value;
|
|
426
430
|
const n = {
|
|
@@ -460,16 +464,16 @@ const V = (e) => {
|
|
|
460
464
|
case l.End:
|
|
461
465
|
throw new Error("解析失败");
|
|
462
466
|
}
|
|
463
|
-
},
|
|
467
|
+
}, j = (e, t) => {
|
|
464
468
|
if (e.current) {
|
|
465
469
|
const r = y(t, e.current);
|
|
466
470
|
return r.current ? (e.current = r.current, e) : (e.value = r.token.value, e.current = null, e.parent);
|
|
467
471
|
}
|
|
468
472
|
throw new Error("解析失败");
|
|
469
|
-
},
|
|
473
|
+
}, D = (e, t) => {
|
|
470
474
|
switch (e.state) {
|
|
471
475
|
case u.Key: {
|
|
472
|
-
const r =
|
|
476
|
+
const r = i(t);
|
|
473
477
|
if (r.current)
|
|
474
478
|
throw new Error("解析失败");
|
|
475
479
|
if (!r.token)
|
|
@@ -479,7 +483,7 @@ const V = (e) => {
|
|
|
479
483
|
throw new Error("解析失败");
|
|
480
484
|
}
|
|
481
485
|
case u.Colon: {
|
|
482
|
-
const r =
|
|
486
|
+
const r = i(t);
|
|
483
487
|
if (r.current) {
|
|
484
488
|
e.state = u.Value;
|
|
485
489
|
const n = {
|
|
@@ -522,7 +526,7 @@ const V = (e) => {
|
|
|
522
526
|
}, g = (e, t) => {
|
|
523
527
|
switch (e.state) {
|
|
524
528
|
case s.Start: {
|
|
525
|
-
const r =
|
|
529
|
+
const r = i(t);
|
|
526
530
|
if (r.current) {
|
|
527
531
|
if (r.current.type === "string") {
|
|
528
532
|
e.state = s.Property;
|
|
@@ -545,11 +549,11 @@ const V = (e) => {
|
|
|
545
549
|
if (!r.token)
|
|
546
550
|
return e;
|
|
547
551
|
if (r.token.value === "}")
|
|
548
|
-
return e.state = s.End,
|
|
552
|
+
return e.state = s.End, o(e.parent);
|
|
549
553
|
throw new Error("解析失败");
|
|
550
554
|
}
|
|
551
555
|
case s.Property: {
|
|
552
|
-
const r =
|
|
556
|
+
const r = i(t);
|
|
553
557
|
if (r.current)
|
|
554
558
|
throw new Error("解析失败");
|
|
555
559
|
if (!r.token)
|
|
@@ -558,12 +562,12 @@ const V = (e) => {
|
|
|
558
562
|
case ",":
|
|
559
563
|
return e.state = s.Comma, e;
|
|
560
564
|
case "}":
|
|
561
|
-
return e.state = s.End,
|
|
565
|
+
return e.state = s.End, o(e.parent);
|
|
562
566
|
}
|
|
563
567
|
throw new Error("解析失败");
|
|
564
568
|
}
|
|
565
569
|
case s.Comma: {
|
|
566
|
-
const r =
|
|
570
|
+
const r = i(t);
|
|
567
571
|
if (r.current) {
|
|
568
572
|
if (r.current.type === "string") {
|
|
569
573
|
e.state = s.Property;
|
|
@@ -590,14 +594,14 @@ const V = (e) => {
|
|
|
590
594
|
case s.End:
|
|
591
595
|
throw new Error("解析失败");
|
|
592
596
|
}
|
|
593
|
-
},
|
|
597
|
+
}, P = (e) => {
|
|
594
598
|
switch (e.type) {
|
|
595
599
|
case "root":
|
|
596
600
|
return e;
|
|
597
601
|
case "literal":
|
|
598
602
|
if (e.current && e.current.type === "number") {
|
|
599
|
-
const t =
|
|
600
|
-
return e.value = t.value, e.current = null,
|
|
603
|
+
const t = T(e.current);
|
|
604
|
+
return e.value = t.value, e.current = null, o(e.parent);
|
|
601
605
|
}
|
|
602
606
|
throw new Error("解析失败");
|
|
603
607
|
case "array":
|
|
@@ -606,19 +610,19 @@ const V = (e) => {
|
|
|
606
610
|
case "identifier":
|
|
607
611
|
throw new Error("解析失败");
|
|
608
612
|
}
|
|
609
|
-
},
|
|
613
|
+
}, A = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
610
614
|
__proto__: null,
|
|
611
615
|
ArrayNodeState: l,
|
|
612
616
|
ObjectNodeState: s,
|
|
613
617
|
PropertyNodeState: u,
|
|
614
|
-
endAst:
|
|
618
|
+
endAst: P,
|
|
615
619
|
parseArrayNode: f,
|
|
616
|
-
parseIdentifierNode:
|
|
617
|
-
parseLiteralNode:
|
|
620
|
+
parseIdentifierNode: j,
|
|
621
|
+
parseLiteralNode: V,
|
|
618
622
|
parseObjectNode: g,
|
|
619
|
-
parsePropertyNode:
|
|
620
|
-
parseRootNode:
|
|
621
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
623
|
+
parsePropertyNode: D,
|
|
624
|
+
parseRootNode: O
|
|
625
|
+
}, Symbol.toStringTag, { value: "Module" })), M = () => {
|
|
622
626
|
const e = {
|
|
623
627
|
type: "root",
|
|
624
628
|
value: null
|
|
@@ -627,22 +631,23 @@ const V = (e) => {
|
|
|
627
631
|
return {
|
|
628
632
|
getRoot: () => e,
|
|
629
633
|
getCurrent: () => t,
|
|
634
|
+
generate: () => S(e),
|
|
630
635
|
parse: (r) => {
|
|
631
636
|
switch (t.type) {
|
|
632
637
|
case "root":
|
|
633
|
-
t =
|
|
638
|
+
t = O(t, r);
|
|
634
639
|
break;
|
|
635
640
|
case "literal":
|
|
636
|
-
t =
|
|
641
|
+
t = V(t, r);
|
|
637
642
|
break;
|
|
638
643
|
case "array":
|
|
639
644
|
t = f(t, r);
|
|
640
645
|
break;
|
|
641
646
|
case "identifier":
|
|
642
|
-
t =
|
|
647
|
+
t = j(t, r);
|
|
643
648
|
break;
|
|
644
649
|
case "property":
|
|
645
|
-
t =
|
|
650
|
+
t = D(t, r);
|
|
646
651
|
break;
|
|
647
652
|
case "object":
|
|
648
653
|
t = g(t, r);
|
|
@@ -650,13 +655,13 @@ const V = (e) => {
|
|
|
650
655
|
}
|
|
651
656
|
},
|
|
652
657
|
end: () => {
|
|
653
|
-
|
|
658
|
+
P(t);
|
|
654
659
|
}
|
|
655
660
|
};
|
|
656
661
|
};
|
|
657
662
|
export {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
663
|
+
M as createJsonBrook,
|
|
664
|
+
G as generate,
|
|
665
|
+
A as parse,
|
|
666
|
+
z as tokenize
|
|
662
667
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(i,
|
|
2
|
-
`,"\r"],
|
|
1
|
+
(function(i,v){typeof exports=="object"&&typeof module<"u"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(i=typeof globalThis<"u"?globalThis:i||self,v(i.JsonBrook={}))})(this,(function(i){"use strict";const v=e=>{if(e.current)switch(e.current.type){case"keyword":return JSON.parse(e.current.value);case"string":return JSON.parse(`${e.current.escapeLength>0?e.current.value.slice(0,-e.current.escapeLength):e.current.value}"`);case"number":return JSON.parse(e.current.value.slice(0,e.current.validLength))}return e.value},m=e=>e.children.map(t=>w(t)),S=e=>e.children.reduce((t,r)=>(r.value&&(t[r.key.value]=w(r.value)),t),{}),w=e=>{switch(e.type){case"literal":return v(e);case"array":return m(e);case"object":return S(e)}},b=e=>{if(e.value)return w(e.value)},J=Object.freeze(Object.defineProperty({__proto__:null,arrayGenerator:m,literalGenerator:v,normalGenerator:w,objectGenerator:S,rootGenerator:b},Symbol.toStringTag,{value:"Module"})),c={Normal:"Normal",Escape:"Escape"},a={Negative:"Negative",Zero:"Zero",Digit:"Digit",Point:"Point",DigitFraction:"DigitFraction",Exp:"Exp",ExpDigitOrSign:"ExpDigitOrSign"},_=[" "," ",`
|
|
2
|
+
`,"\r"],K=["{","}","[","]",":",","],N=["true","false","null"],Z=N.map(e=>e[0]),z=['"',"\\","/","b","f","n","r","t"],F=["+","-"],k=e=>e>="0"&&e<="9",x=e=>e>="1"&&e<="9",G=e=>k(e)||e>="a"&&e<="f"||e>="A"&&e<="F",y=e=>e==="e"||e==="E",u=e=>{if(_.includes(e))return{token:null,current:null};if(K.includes(e))return{token:{type:"symbol",value:e},current:null};const t=Z.indexOf(e);if(t!==-1)return{token:null,current:{type:"keyword",value:N[t],matchLength:1}};if(e==='"')return{token:null,current:{type:"string",state:c.Normal,value:e,escapeLength:0}};if(e==="-")return{token:null,current:{type:"number",state:a.Negative,value:e,validLength:0}};if(e==="0")return{token:null,current:{type:"number",state:a.Zero,value:e,validLength:1}};if(x(e))return{token:null,current:{type:"number",state:a.Digit,value:e,validLength:1}};throw new Error("解析失败")},L=(e,t)=>{if(e===t.value[t.matchLength])return t.matchLength++,t.matchLength===t.value.length?{token:{type:"keyword",value:JSON.parse(t.value)},current:null}:{token:null,current:t};throw new Error("解析失败")},g=(e,t)=>{switch(t.state){case c.Normal:switch(e){case'"':return t.value+=e,{token:{type:"string",value:JSON.parse(t.value)},current:null};case"\\":return t.state=c.Escape,t.value+=e,t.escapeLength=1,{token:null,current:t};default:return t.value+=e,{token:null,current:t}}case c.Escape:if(t.escapeLength===1){if(z.includes(e))return t.state=c.Normal,t.value+=e,t.escapeLength=0,{token:null,current:t};if(e==="u")return t.value+=e,t.escapeLength++,{token:null,current:t};throw new Error("解析失败")}if(G(e))return t.escapeLength===6?(t.state=c.Normal,t.value+=e,t.escapeLength=0,{token:null,current:t}):(t.value+=e,t.escapeLength++,{token:null,current:t});throw new Error("解析失败")}},T=(e,t)=>{switch(t.state){case a.Negative:if(e==="0")return t.state=a.Zero,t.value+=e,t.validLength=t.value.length,{token:null,current:t};if(x(e))return t.state=a.Digit,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.Zero:return e==="."?(t.state=a.Point,t.value+=e,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Digit:return k(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):e==="."?(t.state=a.Point,t.value+=e,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Point:if(k(e))return t.state=a.DigitFraction,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.DigitFraction:return k(e)?(t.value+=e,t.validLength=t.value.length,{token:null,current:t}):y(e)?(t.state=a.Exp,t.value+=e,{token:null,current:t}):{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};case a.Exp:if(F.includes(e))return t.state=a.ExpDigitOrSign,t.value+=e,{token:null,current:t};if(k(e))return t.state=a.ExpDigitOrSign,t.value+=e,t.validLength=t.value.length,{token:null,current:t};throw new Error("解析失败");case a.ExpDigitOrSign:if(k(e))return t.value+=e,t.validLength=t.value.length,{token:null,current:t};if(t.validLength===t.value.length)return{token:{type:"number",value:JSON.parse(t.value)},current:null,char:e};throw new Error("解析失败")}},O=e=>{if(e.validLength===e.value.length)return{type:"number",value:JSON.parse(e.value)};throw new Error("解析失败")},A=Object.freeze(Object.defineProperty({__proto__:null,NumberTokenState:a,StringTokenState:c,endNumberTokenCurrent:O,parseNextToken:u,parseNextTokenWithKeyword:L,parseNextTokenWithNumber:T,parseNextTokenWithString:g},Symbol.toStringTag,{value:"Module"})),l={Start:"Start",Value:"Value",Comma:"Comma",End:"End"},o={Key:"Key",Colon:"Colon",Value:"Value"},s={Start:"Start",Property:"Property",Comma:"Comma",End:"End"},p=(e,t)=>{switch(e.type){case"root":if(t)throw new Error("解析失败");return e;case"array":if(e.state===l.Value)return t?h(e,t):e;throw new Error("解析失败");case"property":if(e.state===o.Value)return t?E(e.parent,t):e.parent;throw new Error("解析失败")}},j=(e,t)=>{if(e.value){const r=u(t);if(!r.token&&!r.current)return e;throw new Error("解析失败")}else{const r=u(t);if(r.current)return e.value={type:"literal",value:null,current:r.current,parent:e},e.value;if(!r.token)return e;switch(r.token.value){case"[":return e.value={type:"array",state:l.Start,children:[],parent:e},e.value;case"{":return e.value={type:"object",state:s.Start,children:[],parent:e},e.value}throw new Error("解析失败")}},P=(e,t)=>{if(e.current)switch(e.current.type){case"keyword":{const r=L(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent)):(e.current=r.current,e)}case"string":{const r=g(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent)):(e.current=r.current,e)}case"number":{const r=T(t,e.current);return r.token?(e.value=r.token.value,e.current=null,p(e.parent,r.char)):(e.current=r.current,e)}}throw new Error("解析失败")},h=(e,t)=>{switch(e.state){case l.Start:{const r=u(t);if(r.current){e.state=l.Value;const n={type:"literal",value:null,current:r.current,parent:e};return e.children.push(n),n}if(!r.token)return e;switch(r.token.value){case"]":return e.state=l.End,p(e.parent);case"[":{e.state=l.Value;const n={type:"array",state:l.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=l.Value;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case l.Value:{const r=u(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;switch(r.token.value){case",":return e.state=l.Comma,e;case"]":return e.state=l.End,p(e.parent)}throw new Error("解析失败")}case l.Comma:{const r=u(t);if(r.current){e.state=l.Value;const n={type:"literal",value:null,current:r.current,parent:e};return e.children.push(n),n}if(!r.token)return e;switch(r.token.value){case"[":{e.state=l.Value;const n={type:"array",state:l.Start,children:[],parent:e};return e.children.push(n),n}case"{":{e.state=l.Value;const n={type:"object",state:s.Start,children:[],parent:e};return e.children.push(n),n}}throw new Error("解析失败")}case l.End:throw new Error("解析失败")}},V=(e,t)=>{if(e.current){const r=g(t,e.current);return r.current?(e.current=r.current,e):(e.value=r.token.value,e.current=null,e.parent)}throw new Error("解析失败")},D=(e,t)=>{switch(e.state){case o.Key:{const r=u(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;if(r.token.value===":")return e.state=o.Colon,e;throw new Error("解析失败")}case o.Colon:{const r=u(t);if(r.current){e.state=o.Value;const n={type:"literal",value:null,current:r.current,parent:e};return e.value=n,n}if(!r.token)return e;switch(r.token.value){case"[":{e.state=o.Value;const n={type:"array",state:l.Start,children:[],parent:e};return e.value=n,n}case"{":{e.state=o.Value;const n={type:"object",state:s.Start,children:[],parent:e};return e.value=n,n}}throw new Error("解析失败")}case o.Value:throw new Error("解析失败")}},E=(e,t)=>{switch(e.state){case s.Start:{const r=u(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:o.Key,key:null,value:null,parent:e},f={type:"identifier",value:"",current:r.current,parent:n};return n.key=f,e.children.push(n),f}throw new Error("解析失败")}if(!r.token)return e;if(r.token.value==="}")return e.state=s.End,p(e.parent);throw new Error("解析失败")}case s.Property:{const r=u(t);if(r.current)throw new Error("解析失败");if(!r.token)return e;switch(r.token.value){case",":return e.state=s.Comma,e;case"}":return e.state=s.End,p(e.parent)}throw new Error("解析失败")}case s.Comma:{const r=u(t);if(r.current){if(r.current.type==="string"){e.state=s.Property;const n={type:"property",state:o.Key,key:null,value:null,parent:e},f={type:"identifier",value:"",current:r.current,parent:n};return n.key=f,e.children.push(n),f}throw new Error("解析失败")}if(!r.token)return e;throw new Error("解析失败")}case s.End:throw new Error("解析失败")}},C=e=>{switch(e.type){case"root":return e;case"literal":if(e.current&&e.current.type==="number"){const t=O(e.current);return e.value=t.value,e.current=null,p(e.parent)}throw new Error("解析失败");case"array":case"object":case"property":case"identifier":throw new Error("解析失败")}},M=Object.freeze(Object.defineProperty({__proto__:null,ArrayNodeState:l,ObjectNodeState:s,PropertyNodeState:o,endAst:C,parseArrayNode:h,parseIdentifierNode:V,parseLiteralNode:P,parseObjectNode:E,parsePropertyNode:D,parseRootNode:j},Symbol.toStringTag,{value:"Module"})),B=()=>{const e={type:"root",value:null};let t=e;return{getRoot:()=>e,getCurrent:()=>t,generate:()=>b(e),parse:r=>{switch(t.type){case"root":t=j(t,r);break;case"literal":t=P(t,r);break;case"array":t=h(t,r);break;case"identifier":t=V(t,r);break;case"property":t=D(t,r);break;case"object":t=E(t,r);break}},end:()=>{C(t)}}};i.createJsonBrook=B,i.generate=J,i.parse=M,i.tokenize=A,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-brook",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
4
|
"description": "parse json data streamly",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"@biomejs/biome": "2.4.4",
|
|
40
40
|
"@tailwindcss/vite": "^4.1.13",
|
|
41
41
|
"@types/node": "^25.3.0",
|
|
42
|
+
"@vitest/coverage-v8": "4.1.0",
|
|
42
43
|
"lint-staged": "~16.2.7",
|
|
43
44
|
"lucide-solid": "^0.577.0",
|
|
44
45
|
"shiki": "^3.22.0",
|
|
@@ -50,13 +51,16 @@
|
|
|
50
51
|
"typescript": "~5.9.3",
|
|
51
52
|
"vite": "^7.3.1",
|
|
52
53
|
"vite-plugin-dts": "^4.5.4",
|
|
53
|
-
"vite-plugin-solid": "^2.11.8"
|
|
54
|
+
"vite-plugin-solid": "^2.11.8",
|
|
55
|
+
"vitest": "^4.1.0"
|
|
54
56
|
},
|
|
55
57
|
"scripts": {
|
|
56
58
|
"dev": "vite --config vite.doc.config.ts",
|
|
57
59
|
"build": "tsc && vite build",
|
|
58
60
|
"build:doc": "tsc && vite build --config vite.doc.config.ts",
|
|
59
61
|
"preview": "vite preview",
|
|
62
|
+
"test": "vitest",
|
|
63
|
+
"coverage": "vitest run --coverage",
|
|
60
64
|
"lint": "biome check --fix"
|
|
61
65
|
}
|
|
62
66
|
}
|