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