json-brook 0.0.4 → 0.1.0-alpha.0

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 DELETED
@@ -1,652 +0,0 @@
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
- createParser: () => createParser,
25
- createTokenize: () => createTokenize
26
- });
27
- module.exports = __toCommonJS(src_exports);
28
-
29
- // src/create-parser.ts
30
- function createParser() {
31
- const root = {
32
- type: "Root",
33
- current: null
34
- };
35
- let currentRef = root;
36
- const tryGetNextValue = (token) => {
37
- if (token.type === "Keyword") {
38
- return {
39
- type: "Literal",
40
- value: token.value
41
- };
42
- }
43
- if (token.type === "String") {
44
- return {
45
- type: "Literal",
46
- value: token.value
47
- };
48
- }
49
- if (token.type === "Number") {
50
- return {
51
- type: "Literal",
52
- value: token.value
53
- };
54
- }
55
- if (token.type === "Symbol" && token.value === "[") {
56
- return {
57
- type: "Array",
58
- children: [],
59
- state: 0 /* Start */,
60
- parent: currentRef,
61
- current: null
62
- };
63
- }
64
- if (token.type === "Symbol" && token.value === "{") {
65
- return {
66
- type: "Object",
67
- children: [],
68
- state: 0 /* Start */,
69
- parent: currentRef,
70
- current: null
71
- };
72
- }
73
- return null;
74
- };
75
- const onArrayOrObjectFinish = () => {
76
- currentRef = currentRef;
77
- switch (currentRef.parent.type) {
78
- case "Root":
79
- return;
80
- case "Array":
81
- currentRef.parent.children.push(currentRef);
82
- currentRef.parent.current = null;
83
- currentRef = currentRef.parent;
84
- return;
85
- case "Property":
86
- currentRef.parent.current = currentRef;
87
- currentRef = currentRef.parent;
88
- currentRef.parent.children.push(currentRef);
89
- currentRef.parent.current = null;
90
- currentRef = currentRef.parent;
91
- return;
92
- }
93
- };
94
- const onPropertyFinish = () => {
95
- currentRef = currentRef;
96
- currentRef.parent.children.push(currentRef);
97
- currentRef.parent.current = null;
98
- currentRef = currentRef.parent;
99
- };
100
- const getRoot = () => root.current;
101
- const write = (token) => {
102
- switch (currentRef.type) {
103
- case "Root":
104
- if (currentRef.current === null) {
105
- const next = tryGetNextValue(token);
106
- if (next) {
107
- currentRef.current = next;
108
- if (next.type !== "Literal") {
109
- currentRef = next;
110
- }
111
- return;
112
- }
113
- }
114
- throw new Error("解析错误");
115
- case "Array":
116
- switch (currentRef.state) {
117
- case 0 /* Start */: {
118
- if (token.type === "Symbol" && token.value === "]") {
119
- onArrayOrObjectFinish();
120
- return;
121
- }
122
- const next = tryGetNextValue(token);
123
- if (next) {
124
- currentRef.state = 1 /* Value */;
125
- if (next.type === "Literal") {
126
- currentRef.children.push(next);
127
- currentRef.current = null;
128
- } else {
129
- currentRef.current = next;
130
- currentRef = next;
131
- }
132
- return;
133
- }
134
- throw new Error("解析错误");
135
- }
136
- case 1 /* Value */:
137
- if (token.type === "Symbol" && token.value === "]") {
138
- onArrayOrObjectFinish();
139
- return;
140
- }
141
- if (token.type === "Symbol" && token.value === ",") {
142
- currentRef.state = 2 /* Comma */;
143
- return;
144
- }
145
- throw new Error("解析错误");
146
- case 2 /* Comma */: {
147
- const next = tryGetNextValue(token);
148
- if (next) {
149
- currentRef.state = 1 /* Value */;
150
- if (next.type === "Literal") {
151
- currentRef.children.push(next);
152
- currentRef.current = null;
153
- } else {
154
- currentRef.current = next;
155
- currentRef = next;
156
- }
157
- return;
158
- }
159
- throw new Error("解析错误");
160
- }
161
- }
162
- case "Property":
163
- switch (currentRef.state) {
164
- case 0 /* Key */:
165
- if (token.type === "Symbol" && token.value === ":") {
166
- currentRef.state = 1 /* Colon */;
167
- return;
168
- }
169
- throw new Error("解析错误");
170
- case 1 /* Colon */: {
171
- const next = tryGetNextValue(token);
172
- if (next) {
173
- currentRef.state = 2 /* Value */;
174
- currentRef.current = next;
175
- if (next.type === "Literal") {
176
- onPropertyFinish();
177
- } else {
178
- currentRef = next;
179
- }
180
- return;
181
- }
182
- throw new Error("解析错误");
183
- }
184
- }
185
- case "Object":
186
- switch (currentRef.state) {
187
- case 0 /* Start */:
188
- if (token.type === "String") {
189
- currentRef.state = 1 /* Property */;
190
- currentRef.current = {
191
- type: "Property",
192
- key: {
193
- type: "Identifier",
194
- value: token.value
195
- },
196
- current: null,
197
- state: 0 /* Key */,
198
- parent: currentRef
199
- };
200
- currentRef = currentRef.current;
201
- return;
202
- }
203
- if (token.type === "Symbol" && token.value === "}") {
204
- onArrayOrObjectFinish();
205
- return;
206
- }
207
- throw new Error("解析错误");
208
- case 1 /* Property */:
209
- if (token.type === "Symbol" && token.value === "}") {
210
- onArrayOrObjectFinish();
211
- return;
212
- }
213
- if (token.type === "Symbol" && token.value === ",") {
214
- currentRef.state = 2 /* Comma */;
215
- return;
216
- }
217
- throw new Error("解析错误");
218
- case 2 /* Comma */:
219
- if (token.type === "String") {
220
- currentRef.state = 1 /* Property */;
221
- currentRef.current = {
222
- type: "Property",
223
- key: {
224
- type: "Identifier",
225
- value: token.value
226
- },
227
- current: null,
228
- state: 0 /* Key */,
229
- parent: currentRef
230
- };
231
- currentRef = currentRef.current;
232
- return;
233
- }
234
- throw new Error("解析错误");
235
- }
236
- }
237
- };
238
- return {
239
- getRoot,
240
- write
241
- };
242
- }
243
-
244
- // src/create-tokenize.ts
245
- var whiteSpaces = [" ", " ", "\n", "\r"];
246
- var symbols = ["{", "}", "[", "]", ":", ","];
247
- var keywords = ["true", "false", "null"];
248
- var keywordsStart = keywords.map((keyword) => keyword.charAt(0));
249
- var escapes = ['"', "\\", "/", "b", "f", "n", "r", "t"];
250
- var hexEscape = "u";
251
- var flags = ["+", "-"];
252
- function isDigit(char) {
253
- return char >= "0" && char <= "9";
254
- }
255
- function isDigitNotZero(char) {
256
- return char >= "1" && char <= "9";
257
- }
258
- function isHex(char) {
259
- return isDigit(char) || char >= "a" && char <= "f" || char >= "A" && char <= "F";
260
- }
261
- function isExp(char) {
262
- return char === "e" || char === "E";
263
- }
264
- function createTokenize() {
265
- let current = null;
266
- const write = (char) => {
267
- if (current) {
268
- switch (current.type) {
269
- case "Keyword":
270
- if (char === current.value[current.matchedIndex + 1]) {
271
- current.matchedIndex++;
272
- if (current.matchedIndex === current.value.length - 1) {
273
- const token = {
274
- type: "Keyword",
275
- value: JSON.parse(current.value)
276
- };
277
- current = null;
278
- return token;
279
- }
280
- return null;
281
- }
282
- throw new Error("解析失败");
283
- case "String":
284
- switch (current.state) {
285
- case 0 /* Normal */:
286
- switch (char) {
287
- case '"': {
288
- current.value += char;
289
- const token = {
290
- type: "String",
291
- value: JSON.parse(current.value)
292
- };
293
- current = null;
294
- return token;
295
- }
296
- case "\\":
297
- current.state = 1 /* Escape */;
298
- current.value += char;
299
- current.escapeIndex = 0;
300
- return null;
301
- default:
302
- current.value += char;
303
- return null;
304
- }
305
- case 1 /* Escape */: {
306
- if (current.escapeIndex === 0) {
307
- if (escapes.includes(char)) {
308
- current.state = 0 /* Normal */;
309
- current.value += char;
310
- return null;
311
- }
312
- if (char === hexEscape) {
313
- current.value += char;
314
- current.escapeIndex++;
315
- return null;
316
- }
317
- throw new Error("解析失败");
318
- } else {
319
- if (isHex(char)) {
320
- if (current.escapeIndex === 4) {
321
- current.state = 0 /* Normal */;
322
- current.value += char;
323
- return null;
324
- } else {
325
- current.value += char;
326
- current.escapeIndex++;
327
- return null;
328
- }
329
- } else {
330
- throw new Error("解析失败");
331
- }
332
- }
333
- }
334
- }
335
- case "Number":
336
- switch (current.state) {
337
- case 1 /* Negative */:
338
- if (char === "0") {
339
- current.state = 2 /* Zero */;
340
- current.value += char;
341
- current.passed = true;
342
- return null;
343
- }
344
- if (isDigitNotZero(char)) {
345
- current.state = 3 /* Digit */;
346
- current.value += char;
347
- current.passed = true;
348
- return null;
349
- }
350
- throw new Error("解析失败");
351
- case 2 /* Zero */:
352
- if (char === ".") {
353
- current.state = 4 /* Point */;
354
- current.value += char;
355
- current.passed = false;
356
- return null;
357
- }
358
- if (isExp(char)) {
359
- current.state = 6 /* Exp */;
360
- current.value += char;
361
- current.passed = false;
362
- return null;
363
- }
364
- if (current.passed) {
365
- const token = {
366
- type: "Number",
367
- value: JSON.parse(current.value)
368
- };
369
- current = null;
370
- const next = write(char);
371
- if (next) {
372
- return Array.isArray(next) ? [token, ...next] : [token, next];
373
- } else {
374
- return token;
375
- }
376
- }
377
- throw new Error("解析失败");
378
- case 3 /* Digit */:
379
- if (isDigit(char)) {
380
- current.value += char;
381
- current.passed = true;
382
- return null;
383
- }
384
- if (char === ".") {
385
- current.state = 4 /* Point */;
386
- current.value += char;
387
- current.passed = false;
388
- return null;
389
- }
390
- if (isExp(char)) {
391
- current.state = 6 /* Exp */;
392
- current.value += char;
393
- current.passed = false;
394
- return null;
395
- }
396
- if (current.passed) {
397
- const token = {
398
- type: "Number",
399
- value: JSON.parse(current.value)
400
- };
401
- current = null;
402
- const next = write(char);
403
- if (next) {
404
- return Array.isArray(next) ? [token, ...next] : [token, next];
405
- } else {
406
- return token;
407
- }
408
- }
409
- throw new Error("解析失败");
410
- case 4 /* Point */:
411
- if (isDigit(char)) {
412
- current.state = 5 /* DigitFraction */;
413
- current.value += char;
414
- current.passed = true;
415
- return null;
416
- }
417
- if (current.passed) {
418
- const token = {
419
- type: "Number",
420
- value: JSON.parse(current.value)
421
- };
422
- current = null;
423
- const next = write(char);
424
- if (next) {
425
- return Array.isArray(next) ? [token, ...next] : [token, next];
426
- } else {
427
- return token;
428
- }
429
- }
430
- throw new Error("解析失败");
431
- case 5 /* DigitFraction */:
432
- if (isDigit(char)) {
433
- current.value += char;
434
- current.passed = true;
435
- return null;
436
- }
437
- if (isExp(char)) {
438
- current.state = 6 /* Exp */;
439
- current.value += char;
440
- current.passed = false;
441
- return null;
442
- }
443
- if (current.passed) {
444
- const token = {
445
- type: "Number",
446
- value: JSON.parse(current.value)
447
- };
448
- current = null;
449
- const next = write(char);
450
- if (next) {
451
- return Array.isArray(next) ? [token, ...next] : [token, next];
452
- } else {
453
- return token;
454
- }
455
- }
456
- throw new Error("解析失败");
457
- case 6 /* Exp */:
458
- if (flags.includes(char)) {
459
- current.state = 7 /* ExpDigitOrSign */;
460
- current.value += char;
461
- current.passed = false;
462
- return null;
463
- }
464
- if (isDigit(char)) {
465
- current.state = 7 /* ExpDigitOrSign */;
466
- current.value += char;
467
- current.passed = true;
468
- return null;
469
- }
470
- if (current.passed) {
471
- const token = {
472
- type: "Number",
473
- value: JSON.parse(current.value)
474
- };
475
- current = null;
476
- const next = write(char);
477
- if (next) {
478
- return Array.isArray(next) ? [token, ...next] : [token, next];
479
- } else {
480
- return token;
481
- }
482
- }
483
- throw new Error("解析失败");
484
- case 7 /* ExpDigitOrSign */:
485
- if (isDigit(char)) {
486
- current.value += char;
487
- current.passed = true;
488
- return null;
489
- }
490
- if (current.passed) {
491
- const token = {
492
- type: "Number",
493
- value: JSON.parse(current.value)
494
- };
495
- current = null;
496
- const next = write(char);
497
- if (next) {
498
- return Array.isArray(next) ? [token, ...next] : [token, next];
499
- } else {
500
- return token;
501
- }
502
- }
503
- throw new Error("解析失败");
504
- }
505
- }
506
- } else {
507
- if (whiteSpaces.includes(char)) {
508
- return null;
509
- }
510
- if (symbols.includes(char)) {
511
- return {
512
- type: "Symbol",
513
- value: char
514
- };
515
- }
516
- const keywordIndex = keywordsStart.indexOf(char);
517
- if (keywordIndex >= 0) {
518
- current = {
519
- type: "Keyword",
520
- value: keywords[keywordIndex],
521
- matchedIndex: 0
522
- };
523
- return null;
524
- }
525
- if (char === '"') {
526
- current = {
527
- type: "String",
528
- state: 0 /* Normal */,
529
- value: '"',
530
- escapeIndex: 0
531
- };
532
- return null;
533
- }
534
- if (char === "-") {
535
- current = {
536
- type: "Number",
537
- state: 1 /* Negative */,
538
- value: char,
539
- passed: false
540
- };
541
- return null;
542
- }
543
- if (char === "0") {
544
- current = {
545
- type: "Number",
546
- state: 2 /* Zero */,
547
- value: char,
548
- passed: true
549
- };
550
- return null;
551
- }
552
- if (isDigitNotZero(char)) {
553
- current = {
554
- type: "Number",
555
- state: 3 /* Digit */,
556
- value: char,
557
- passed: true
558
- };
559
- return null;
560
- }
561
- throw new Error("解析失败");
562
- }
563
- };
564
- const end = () => {
565
- if (current) {
566
- switch (current.type) {
567
- case "Keyword":
568
- case "String":
569
- throw new Error("解析失败");
570
- case "Number":
571
- if (current.passed) {
572
- const token = {
573
- type: "Number",
574
- value: JSON.parse(current.value)
575
- };
576
- current = null;
577
- return token;
578
- }
579
- return null;
580
- }
581
- }
582
- return null;
583
- };
584
- return {
585
- write,
586
- end
587
- };
588
- }
589
-
590
- // src/create-json-brook.ts
591
- function getValueFromNode(node) {
592
- switch (node.type) {
593
- case "Literal":
594
- return node.value;
595
- case "Array":
596
- return (node.current ? [...node.children, node.current] : node.children).map((child) => getValueFromNode(child));
597
- case "Object":
598
- return (node.current ? [...node.children, node.current] : node.children).reduce(
599
- (obj, property) => {
600
- if (property.current) {
601
- obj[property.key.value] = getValueFromNode(property.current);
602
- }
603
- return obj;
604
- },
605
- // biome-ignore lint/suspicious/noExplicitAny: 可以返回的类型不确定
606
- {}
607
- );
608
- }
609
- }
610
- function createJsonBrook() {
611
- const tokenize = createTokenize();
612
- const parser = createParser();
613
- const write = (str) => {
614
- for (const char of str) {
615
- const token = tokenize.write(char);
616
- if (token) {
617
- if (Array.isArray(token)) {
618
- for (const t of token) {
619
- parser.write(t);
620
- }
621
- } else {
622
- parser.write(token);
623
- }
624
- }
625
- }
626
- };
627
- const end = () => {
628
- const token = tokenize.end();
629
- if (token) {
630
- parser.write(token);
631
- }
632
- };
633
- const getCurrent = () => {
634
- const root = parser.getRoot();
635
- if (root) {
636
- return getValueFromNode(root);
637
- } else {
638
- return void 0;
639
- }
640
- };
641
- return {
642
- getCurrent,
643
- write,
644
- end
645
- };
646
- }
647
- // Annotate the CommonJS export names for ESM import in node:
648
- 0 && (module.exports = {
649
- createJsonBrook,
650
- createParser,
651
- createTokenize
652
- });