json-brook 0.0.2 → 0.0.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # json-brook
2
2
 
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 调整打包产物
8
+
9
+ ## 0.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - 删除无效依赖
14
+
3
15
  ## 0.0.2
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1,12 +1,40 @@
1
1
  # JsonBrook
2
2
 
3
- > 流式解析Json数据
3
+ ![演示视频](assets/showcase.gif)
4
+
5
+ > 流式解析Json数据
4
6
  > 实现上参考了[json-to-ast](https://github.com/vtrushin/json-to-ast)
5
7
 
6
- ## 当前进度
8
+ ## 快速入门
9
+
10
+ ```typescript
11
+ import { createJsonBrook } from "json-brook";
12
+
13
+ const jsonBrook = createJsonBrook();
14
+
15
+ const sample = `{
16
+ "string": "welcome to json brook",
17
+ "number": 20241102,
18
+ "boolean": true,
19
+ "array": ["a", "b", "c"],
20
+ "null": null
21
+ }`;
22
+
23
+ for (const char of sample) {
24
+ jsonBrook.write(char);
25
+ console.log(jsonBrook.getCurrent());
26
+ }
27
+
28
+ jsonBrook.end();
29
+ console.log(jsonBrook.getCurrent());
30
+ ```
31
+
32
+ ## API
33
+ `createJsonBrook` 返回一个JsonBrook实例,JsonBrook实例具有以下方法:
34
+ * `write` 接受字符串并解析,如果解析失败会抛错
35
+ * `end` 结束输入并解析Json数据(针对比较极端的纯数字形式解析,需要知道当前输入已结束),如果解析失败会抛错
36
+ * `getCurrent` 获取当前解析结果
37
+
38
+ ## 在线尝试
39
+ [codesandbox](https://codesandbox.io/p/sandbox/4v5slw)
7
40
 
8
- - [x] token 解析
9
- - [x] ast 解析
10
- - [x] 生成数据
11
- - [ ] 添加单测
12
- - [ ] 新增主页
package/dist/es/index.js CHANGED
@@ -617,5 +617,7 @@ function createJsonBrook() {
617
617
  };
618
618
  }
619
619
  export {
620
- createJsonBrook
620
+ createJsonBrook,
621
+ createParser,
622
+ createTokenize
621
623
  };
package/dist/lib/index.js CHANGED
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- createJsonBrook: () => createJsonBrook
23
+ createJsonBrook: () => createJsonBrook,
24
+ createParser: () => createParser,
25
+ createTokenize: () => createTokenize
24
26
  });
25
27
  module.exports = __toCommonJS(src_exports);
26
28
 
@@ -644,5 +646,7 @@ function createJsonBrook() {
644
646
  }
645
647
  // Annotate the CommonJS export names for ESM import in node:
646
648
  0 && (module.exports = {
647
- createJsonBrook
649
+ createJsonBrook,
650
+ createParser,
651
+ createTokenize
648
652
  });
@@ -4,4 +4,111 @@ declare function createJsonBrook(): {
4
4
  end: () => void;
5
5
  };
6
6
 
7
- export { createJsonBrook };
7
+ type SymbolValue = '{' | '}' | '[' | ']' | ':' | ',';
8
+ type SymbolToken = {
9
+ type: 'Symbol';
10
+ value: SymbolValue;
11
+ };
12
+ type KeywordValue = 'true' | 'false' | 'null';
13
+ type KeywordToken = {
14
+ type: 'Keyword';
15
+ value: boolean | null;
16
+ };
17
+ type KeywordCurrent = {
18
+ type: 'Keyword';
19
+ value: KeywordValue;
20
+ matchedIndex: number;
21
+ };
22
+ declare enum StringState {
23
+ Normal = 0,
24
+ Escape = 1
25
+ }
26
+ type StringToken = {
27
+ type: 'String';
28
+ value: string;
29
+ };
30
+ type StringCurrent = {
31
+ type: 'String';
32
+ state: StringState;
33
+ value: string;
34
+ escapeIndex: number;
35
+ };
36
+ declare enum NumberState {
37
+ Negative = 1,
38
+ Zero = 2,
39
+ Digit = 3,
40
+ Point = 4,
41
+ DigitFraction = 5,
42
+ Exp = 6,
43
+ ExpDigitOrSign = 7
44
+ }
45
+ type NumberToken = {
46
+ type: 'Number';
47
+ value: number;
48
+ };
49
+ type NumberCurrent = {
50
+ type: 'Number';
51
+ state: NumberState;
52
+ value: string;
53
+ passed: boolean;
54
+ };
55
+ type Token = SymbolToken | KeywordToken | StringToken | NumberToken;
56
+ declare function createTokenize(): {
57
+ write: (char: string) => Token | Token[] | null;
58
+ end: () => NumberToken | null;
59
+ };
60
+
61
+ type RootNode = {
62
+ type: 'Root';
63
+ current: ObjectNode | ArrayNode | LiteralNode | null;
64
+ };
65
+ type LiteralNode = {
66
+ type: 'Literal';
67
+ value: string | number | boolean | null;
68
+ };
69
+ declare enum ArrayNodeState {
70
+ Start = 0,
71
+ Value = 1,
72
+ Comma = 2
73
+ }
74
+ type ArrayNode = {
75
+ type: 'Array';
76
+ children: (LiteralNode | ArrayNode | ObjectNode)[];
77
+ state: ArrayNodeState;
78
+ parent: RootNode | ArrayNode | PropertyNode;
79
+ current: ArrayNode | ObjectNode | null;
80
+ };
81
+ type IdentifierNode = {
82
+ type: 'Identifier';
83
+ value: string;
84
+ };
85
+ declare enum PropertyNodeState {
86
+ Key = 0,
87
+ Colon = 1,
88
+ Value = 2
89
+ }
90
+ type PropertyNode = {
91
+ type: 'Property';
92
+ key: IdentifierNode;
93
+ current: LiteralNode | ArrayNode | ObjectNode | null;
94
+ state: PropertyNodeState;
95
+ parent: ObjectNode;
96
+ };
97
+ declare enum ObjectNodeState {
98
+ Start = 0,
99
+ Property = 1,
100
+ Comma = 2
101
+ }
102
+ type ObjectNode = {
103
+ type: 'Object';
104
+ children: PropertyNode[];
105
+ state: ObjectNodeState;
106
+ parent: RootNode | ArrayNode | PropertyNode;
107
+ current: PropertyNode | null;
108
+ };
109
+ declare function createParser(): {
110
+ getRoot: () => LiteralNode | ArrayNode | ObjectNode | null;
111
+ write: (token: Token) => void;
112
+ };
113
+
114
+ export { type ArrayNode, ArrayNodeState, type IdentifierNode, type KeywordCurrent, type KeywordToken, type KeywordValue, type LiteralNode, type NumberCurrent, NumberState, type NumberToken, type ObjectNode, ObjectNodeState, type PropertyNode, PropertyNodeState, type RootNode, type StringCurrent, StringState, type StringToken, type SymbolToken, type SymbolValue, type Token, createJsonBrook, createParser, createTokenize };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-brook",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "parse json data streamly",
5
5
  "keywords": [
6
6
  "json",
@@ -31,7 +31,6 @@
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "1.8.3",
33
33
  "@modern-js/module-tools": "2.60.3",
34
- "@modern-js/plugin-rspress": "1.33.1",
35
34
  "@types/jest": "^29.5.14",
36
35
  "@types/node": "~16.11.7",
37
36
  "jest": "^29.7.0",