json-brook 0.0.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.
@@ -0,0 +1,788 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ createJsonBrook: () => createJsonBrook
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/create-parser.ts
28
+ function createParser() {
29
+ const root = {
30
+ type: "Root",
31
+ value: null
32
+ };
33
+ let currentRef = root;
34
+ const getRoot = () => root.value;
35
+ const write = (token) => {
36
+ switch (currentRef.type) {
37
+ 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;
80
+ return;
81
+ }
82
+ }
83
+ throw new Error("解析错误");
84
+ case "Array":
85
+ switch (currentRef.state) {
86
+ case 0 /* Start */:
87
+ 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;
139
+ return;
140
+ }
141
+ if (token.type === "Symbol" && token.value === "{") {
142
+ currentRef.state = 1 /* Value */;
143
+ currentRef.current = {
144
+ type: "Object",
145
+ children: [],
146
+ state: 0 /* Start */,
147
+ parent: currentRef,
148
+ current: null
149
+ };
150
+ return;
151
+ }
152
+ throw new Error("解析错误");
153
+ case 1 /* Value */:
154
+ 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
+ }
171
+ }
172
+ if (token.type === "Symbol" && token.value === ",") {
173
+ currentRef.state = 2 /* Comma */;
174
+ return;
175
+ }
176
+ 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 === "[") {
203
+ 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
+ };
223
+ return;
224
+ }
225
+ throw new Error("解析错误");
226
+ }
227
+ case "Property":
228
+ switch (currentRef.state) {
229
+ case 0 /* Key */:
230
+ if (token.type === "Symbol" && token.value === ":") {
231
+ currentRef.state = 1 /* Colon */;
232
+ return;
233
+ }
234
+ throw new Error("解析错误");
235
+ case 1 /* Colon */:
236
+ if (token.type === "Keyword") {
237
+ 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;
291
+ return;
292
+ }
293
+ throw new Error("解析错误");
294
+ }
295
+ case "Object":
296
+ switch (currentRef.state) {
297
+ case 0 /* Start */:
298
+ if (token.type === "String") {
299
+ currentRef.state = 1 /* Property */;
300
+ currentRef.current = {
301
+ type: "Property",
302
+ key: {
303
+ type: "Identifier",
304
+ value: token.value
305
+ },
306
+ value: null,
307
+ state: 0 /* Key */,
308
+ parent: currentRef
309
+ };
310
+ currentRef = currentRef.current;
311
+ return;
312
+ }
313
+ 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
+ }
330
+ }
331
+ throw new Error("解析错误");
332
+ case 1 /* Property */:
333
+ 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
+ }
350
+ }
351
+ if (token.type === "Symbol" && token.value === ",") {
352
+ currentRef.state = 2 /* Comma */;
353
+ return;
354
+ }
355
+ throw new Error("解析错误");
356
+ case 2 /* Comma */:
357
+ if (token.type === "String") {
358
+ currentRef.state = 1 /* Property */;
359
+ currentRef.current = {
360
+ type: "Property",
361
+ key: {
362
+ type: "Identifier",
363
+ value: token.value
364
+ },
365
+ value: null,
366
+ state: 0 /* Key */,
367
+ parent: currentRef
368
+ };
369
+ currentRef = currentRef.current;
370
+ return;
371
+ }
372
+ throw new Error("解析错误");
373
+ }
374
+ }
375
+ };
376
+ return {
377
+ getRoot,
378
+ write
379
+ };
380
+ }
381
+
382
+ // src/create-tokenize.ts
383
+ var whiteSpaces = [" ", " ", "\n", "\r"];
384
+ var symbols = ["{", "}", "[", "]", ":", ","];
385
+ var keywords = ["true", "false", "null"];
386
+ var keywordsStart = keywords.map((keyword) => keyword.charAt(0));
387
+ var escapes = ['"', "\\", "/", "b", "f", "n", "r", "t"];
388
+ var hexEscape = "u";
389
+ var flags = ["+", "-"];
390
+ function isDigit(char) {
391
+ return char >= "0" && char <= "9";
392
+ }
393
+ function isDigitNotZero(char) {
394
+ return char >= "1" && char <= "9";
395
+ }
396
+ function isHex(char) {
397
+ return isDigit(char) || char >= "a" && char <= "f" || char >= "A" && char <= "F";
398
+ }
399
+ function isExp(char) {
400
+ return char === "e" || char === "E";
401
+ }
402
+ function createTokenize() {
403
+ let current = null;
404
+ const write = (char) => {
405
+ if (current) {
406
+ switch (current.type) {
407
+ case "Keyword":
408
+ if (char === keywords[current.matchedIndex + 1]) {
409
+ current.matchedIndex++;
410
+ if (current.matchedIndex === current.value.length - 1) {
411
+ const token = {
412
+ type: "Keyword",
413
+ value: JSON.parse(current.value)
414
+ };
415
+ current = null;
416
+ return token;
417
+ }
418
+ return null;
419
+ }
420
+ throw new Error("解析失败");
421
+ case "String":
422
+ switch (current.state) {
423
+ case 0 /* Normal */:
424
+ switch (char) {
425
+ case '"': {
426
+ current.value += char;
427
+ const token = {
428
+ type: "String",
429
+ value: JSON.parse(current.value)
430
+ };
431
+ current = null;
432
+ return token;
433
+ }
434
+ case "\\":
435
+ current.state = 1 /* Escape */;
436
+ current.value += char;
437
+ current.escapeIndex = 0;
438
+ return null;
439
+ default:
440
+ current.value += char;
441
+ return null;
442
+ }
443
+ case 1 /* Escape */: {
444
+ if (current.escapeIndex === 0) {
445
+ if (escapes.includes(char)) {
446
+ current.state = 0 /* Normal */;
447
+ current.value += char;
448
+ return null;
449
+ }
450
+ if (char === hexEscape) {
451
+ current.value += char;
452
+ current.escapeIndex++;
453
+ return null;
454
+ }
455
+ throw new Error("解析失败");
456
+ } else {
457
+ if (isHex(char)) {
458
+ if (current.escapeIndex === 4) {
459
+ current.state = 0 /* Normal */;
460
+ current.value += char;
461
+ return null;
462
+ } else {
463
+ current.value += char;
464
+ current.escapeIndex++;
465
+ return null;
466
+ }
467
+ } else {
468
+ throw new Error("解析失败");
469
+ }
470
+ }
471
+ }
472
+ }
473
+ case "Number":
474
+ switch (current.state) {
475
+ case 1 /* Negative */:
476
+ if (char === "0") {
477
+ current.state = 2 /* Zero */;
478
+ current.value += char;
479
+ current.passed = true;
480
+ return null;
481
+ }
482
+ if (isDigitNotZero(char)) {
483
+ current.state = 3 /* Digit */;
484
+ current.value += char;
485
+ current.passed = true;
486
+ return null;
487
+ }
488
+ throw new Error("解析失败");
489
+ case 2 /* Zero */:
490
+ if (char === ".") {
491
+ current.state = 4 /* Point */;
492
+ current.value += char;
493
+ current.passed = false;
494
+ return null;
495
+ }
496
+ if (isExp(char)) {
497
+ current.state = 6 /* Exp */;
498
+ current.value += char;
499
+ current.passed = false;
500
+ return null;
501
+ }
502
+ if (current.passed) {
503
+ const token = {
504
+ type: "Number",
505
+ value: JSON.parse(current.value)
506
+ };
507
+ current = null;
508
+ const next = write(char);
509
+ if (next) {
510
+ return Array.isArray(next) ? [token, ...next] : [token, next];
511
+ } else {
512
+ return token;
513
+ }
514
+ }
515
+ throw new Error("解析失败");
516
+ case 3 /* Digit */:
517
+ if (isDigit(char)) {
518
+ current.value += char;
519
+ current.passed = true;
520
+ return null;
521
+ }
522
+ if (char === ".") {
523
+ current.state = 4 /* Point */;
524
+ current.value += char;
525
+ current.passed = false;
526
+ return null;
527
+ }
528
+ if (isExp(char)) {
529
+ current.state = 6 /* Exp */;
530
+ current.value += char;
531
+ current.passed = false;
532
+ return null;
533
+ }
534
+ if (current.passed) {
535
+ const token = {
536
+ type: "Number",
537
+ value: JSON.parse(current.value)
538
+ };
539
+ current = null;
540
+ const next = write(char);
541
+ if (next) {
542
+ return Array.isArray(next) ? [token, ...next] : [token, next];
543
+ } else {
544
+ return token;
545
+ }
546
+ }
547
+ throw new Error("解析失败");
548
+ case 4 /* Point */:
549
+ if (isDigit(char)) {
550
+ current.state = 5 /* DigitFraction */;
551
+ current.value += char;
552
+ current.passed = true;
553
+ return null;
554
+ }
555
+ if (current.passed) {
556
+ const token = {
557
+ type: "Number",
558
+ value: JSON.parse(current.value)
559
+ };
560
+ current = null;
561
+ const next = write(char);
562
+ if (next) {
563
+ return Array.isArray(next) ? [token, ...next] : [token, next];
564
+ } else {
565
+ return token;
566
+ }
567
+ }
568
+ throw new Error("解析失败");
569
+ case 5 /* DigitFraction */:
570
+ if (isDigit(char)) {
571
+ current.value += char;
572
+ current.passed = true;
573
+ return null;
574
+ }
575
+ if (isExp(char)) {
576
+ current.state = 6 /* Exp */;
577
+ current.value += char;
578
+ current.passed = false;
579
+ return null;
580
+ }
581
+ if (current.passed) {
582
+ const token = {
583
+ type: "Number",
584
+ value: JSON.parse(current.value)
585
+ };
586
+ current = null;
587
+ const next = write(char);
588
+ if (next) {
589
+ return Array.isArray(next) ? [token, ...next] : [token, next];
590
+ } else {
591
+ return token;
592
+ }
593
+ }
594
+ throw new Error("解析失败");
595
+ case 6 /* Exp */:
596
+ if (flags.includes(char)) {
597
+ current.state = 7 /* ExpDigitOrSign */;
598
+ current.value += char;
599
+ current.passed = false;
600
+ return null;
601
+ }
602
+ if (isDigit(char)) {
603
+ current.state = 7 /* ExpDigitOrSign */;
604
+ current.value += char;
605
+ current.passed = true;
606
+ return null;
607
+ }
608
+ if (current.passed) {
609
+ const token = {
610
+ type: "Number",
611
+ value: JSON.parse(current.value)
612
+ };
613
+ current = null;
614
+ const next = write(char);
615
+ if (next) {
616
+ return Array.isArray(next) ? [token, ...next] : [token, next];
617
+ } else {
618
+ return token;
619
+ }
620
+ }
621
+ throw new Error("解析失败");
622
+ case 7 /* ExpDigitOrSign */:
623
+ if (isDigit(char)) {
624
+ current.value += char;
625
+ current.passed = true;
626
+ return null;
627
+ }
628
+ if (current.passed) {
629
+ const token = {
630
+ type: "Number",
631
+ value: JSON.parse(current.value)
632
+ };
633
+ current = null;
634
+ const next = write(char);
635
+ if (next) {
636
+ return Array.isArray(next) ? [token, ...next] : [token, next];
637
+ } else {
638
+ return token;
639
+ }
640
+ }
641
+ throw new Error("解析失败");
642
+ }
643
+ }
644
+ } else {
645
+ if (whiteSpaces.includes(char)) {
646
+ return null;
647
+ }
648
+ if (symbols.includes(char)) {
649
+ return {
650
+ type: "Symbol",
651
+ value: char
652
+ };
653
+ }
654
+ const keywordIndex = keywordsStart.indexOf(char);
655
+ if (keywordIndex >= 0) {
656
+ current = {
657
+ type: "Keyword",
658
+ value: keywords[keywordIndex],
659
+ matchedIndex: 0
660
+ };
661
+ return null;
662
+ }
663
+ if (char === '"') {
664
+ current = {
665
+ type: "String",
666
+ state: 0 /* Normal */,
667
+ value: '"',
668
+ escapeIndex: 0
669
+ };
670
+ return null;
671
+ }
672
+ if (char === "-") {
673
+ current = {
674
+ type: "Number",
675
+ state: 1 /* Negative */,
676
+ value: char,
677
+ passed: false
678
+ };
679
+ return null;
680
+ }
681
+ if (char === "0") {
682
+ current = {
683
+ type: "Number",
684
+ state: 2 /* Zero */,
685
+ value: char,
686
+ passed: true
687
+ };
688
+ return null;
689
+ }
690
+ if (isDigitNotZero(char)) {
691
+ current = {
692
+ type: "Number",
693
+ state: 3 /* Digit */,
694
+ value: char,
695
+ passed: true
696
+ };
697
+ return null;
698
+ }
699
+ throw new Error("解析失败");
700
+ }
701
+ };
702
+ const end = () => {
703
+ if (current) {
704
+ switch (current.type) {
705
+ case "Keyword":
706
+ case "String":
707
+ throw new Error("解析失败");
708
+ case "Number":
709
+ if (current.passed) {
710
+ const token = {
711
+ type: "Number",
712
+ value: JSON.parse(current.value)
713
+ };
714
+ current = null;
715
+ return token;
716
+ }
717
+ return null;
718
+ }
719
+ }
720
+ return null;
721
+ };
722
+ return {
723
+ write,
724
+ end
725
+ };
726
+ }
727
+
728
+ // src/create-json-brook.ts
729
+ function getValueFromNode(node) {
730
+ switch (node.type) {
731
+ case "Literal":
732
+ return node.value;
733
+ case "Array":
734
+ return (node.current ? [...node.children, node.current] : node.children).map((child) => getValueFromNode(child));
735
+ case "Object":
736
+ return (node.current ? [...node.children, node.current] : node.children).reduce(
737
+ (obj, property) => {
738
+ if (property.value) {
739
+ obj[property.key.value] = getValueFromNode(property.value);
740
+ }
741
+ return obj;
742
+ },
743
+ // biome-ignore lint/suspicious/noExplicitAny: 可以返回的类型不确定
744
+ {}
745
+ );
746
+ }
747
+ }
748
+ function createJsonBrook() {
749
+ const tokenize = createTokenize();
750
+ const parser = createParser();
751
+ const write = (str) => {
752
+ for (const char of str) {
753
+ const token = tokenize.write(char);
754
+ if (token) {
755
+ if (Array.isArray(token)) {
756
+ for (const t of token) {
757
+ parser.write(t);
758
+ }
759
+ } else {
760
+ parser.write(token);
761
+ }
762
+ }
763
+ }
764
+ };
765
+ const end = () => {
766
+ const token = tokenize.end();
767
+ if (token) {
768
+ parser.write(token);
769
+ }
770
+ };
771
+ const getCurrent = () => {
772
+ const root = parser.getRoot();
773
+ if (root) {
774
+ return getValueFromNode(root);
775
+ } else {
776
+ return void 0;
777
+ }
778
+ };
779
+ return {
780
+ getCurrent,
781
+ write,
782
+ end
783
+ };
784
+ }
785
+ // Annotate the CommonJS export names for ESM import in node:
786
+ 0 && (module.exports = {
787
+ createJsonBrook
788
+ });