@uni-helper/jsonu 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.
- package/LICENSE.md +21 -0
- package/README.md +164 -0
- package/dist/index.d.mts +51 -0
- package/dist/index.mjs +1765 -0
- package/package.json +69 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-PRESENT FliPPeDround <https://github.com/flippedround>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
<img src="./banner.svg" alt="banner" width="100%"/>
|
|
2
|
+
|
|
3
|
+
<div style="display: flex; justify-content: center; align-items: center; gap: 8px;">
|
|
4
|
+
<a href="https://github.com/uni-helper/jsonu/stargazers"><img src="https://img.shields.io/github/stars/uni-helper/jsonu?colorA=005947&colorB=eee&style=for-the-badge" alt="stars"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@uni-helper/jsonu"><img src="https://img.shields.io/npm/dm/@uni-helper/jsonu?colorA=005947&colorB=eee&style=for-the-badge" alt="downloads"></a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/@uni-helper/jsonu"><img src="https://img.shields.io/npm/v/@uni-helper/jsonu?colorA=005947&colorB=eee&style=for-the-badge" alt="npm"></a>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
<div style="display: flex; justify-content: center; align-items: center; gap: 8px;">
|
|
10
|
+
<a href="https://deepwiki.com/uni-helper/jsonu"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
uniapp 条件编译的 json 解析工具。
|
|
14
|
+
|
|
15
|
+
支持 `pages.json` 等 json 文件中的 `#ifdef` / `#ifndef` / `#endif` 条件编译注释,提供解析、按平台裁剪、按平台增删条件数据、往返回写等能力。
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @uni-helper/jsonu
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## API
|
|
24
|
+
|
|
25
|
+
### parse(jsonu)
|
|
26
|
+
|
|
27
|
+
解析 jsonu 字符串,返回 `JsonuDocument`。
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { parse } from '@uni-helper/jsonu'
|
|
31
|
+
|
|
32
|
+
const doc = parse(`{
|
|
33
|
+
"pages": [
|
|
34
|
+
{ "path": "pages/index" },
|
|
35
|
+
// #ifdef MP-WEIXIN
|
|
36
|
+
{ "path": "pages/wx" }
|
|
37
|
+
// #endif
|
|
38
|
+
]
|
|
39
|
+
}`)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
支持两种注释格式:
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
// #ifdef MP-WEIXIN
|
|
47
|
+
/* #ifdef MP-WEIXIN */
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### doc.evaluate(platform)
|
|
51
|
+
|
|
52
|
+
按平台裁剪,返回普通 JS 对象(条件注释全部消除)。
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
doc.evaluate('H5')
|
|
56
|
+
// { pages: [{ path: 'pages/index' }] }
|
|
57
|
+
|
|
58
|
+
doc.evaluate('MP-WEIXIN')
|
|
59
|
+
// { pages: [{ path: 'pages/index' }, { path: 'pages/wx' }] }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
平台名与 uniapp 条件编译平台一致:`H5`、`MP-WEIXIN`、`APP-PLUS` 等。`#ifndef H5` 表示非 H5 平台均保留。多平台用 `||` 连接,如 `H5 || MP-WEIXIN`。
|
|
63
|
+
|
|
64
|
+
### doc.set(platform, data, path?)
|
|
65
|
+
|
|
66
|
+
覆盖写入:先移除该位置该平台已有的条件数据,再写入新的。`path` 可选,省略时操作根级。
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// 根级
|
|
70
|
+
doc.set('MP-WEIXIN', { globalStyle: { navigationBarTitleText: '微信' } })
|
|
71
|
+
|
|
72
|
+
// 嵌套路径(数组形式,数字为数组索引)
|
|
73
|
+
doc.set('MP-WEIXIN', { navigationBarTitleText: '微信' }, ['pages', 0, 'style'])
|
|
74
|
+
|
|
75
|
+
// 嵌套路径(字符串形式)
|
|
76
|
+
doc.set('MP-WEIXIN', { navigationBarTitleText: '微信' }, 'pages.0.style')
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### doc.merge(platform, data, path?)
|
|
80
|
+
|
|
81
|
+
增量写入:不移除已有条件数据。对象 → 合并字段,数组 → 追加元素。
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
doc.merge('MP-WEIXIN', [{ path: 'pages/wx2' }], 'pages')
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### doc.delete(platform, path?)
|
|
88
|
+
|
|
89
|
+
删除该位置该平台的条件数据。
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
doc.delete('MP-WEIXIN', 'pages')
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### doc.stringify(indent?)
|
|
96
|
+
|
|
97
|
+
往返回写为带条件编译注释的 jsonu 字符串。`indent` 默认 `2`。
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
doc.stringify()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 完整示例
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { parse } from '@uni-helper/jsonu'
|
|
107
|
+
|
|
108
|
+
const doc = parse(`{
|
|
109
|
+
"pages": [
|
|
110
|
+
{ "path": "pages/index", "style": { "navigationBarTitleText": "默认" } }
|
|
111
|
+
]
|
|
112
|
+
}`)
|
|
113
|
+
|
|
114
|
+
// 给微信小程序添加专属页面
|
|
115
|
+
doc.merge('MP-WEIXIN', [{ path: 'pages/wx', style: { navigationBarTitleText: '微信' } }], 'pages')
|
|
116
|
+
|
|
117
|
+
// 给微信小程序覆盖首页标题(在 style 对象内追加条件 member,同 key 覆盖)
|
|
118
|
+
doc.set('MP-WEIXIN', { navigationBarTitleText: '微信首页' }, ['pages', 0, 'style'])
|
|
119
|
+
|
|
120
|
+
// 输出带条件编译的 jsonu
|
|
121
|
+
console.log(doc.stringify())
|
|
122
|
+
// {
|
|
123
|
+
// "pages": [
|
|
124
|
+
// {
|
|
125
|
+
// "path": "pages/index",
|
|
126
|
+
// "style": {
|
|
127
|
+
// "navigationBarTitleText": "默认",
|
|
128
|
+
// // #ifdef MP-WEIXIN
|
|
129
|
+
// "navigationBarTitleText": "微信首页"
|
|
130
|
+
// // #endif
|
|
131
|
+
// }
|
|
132
|
+
// },
|
|
133
|
+
// // #ifdef MP-WEIXIN
|
|
134
|
+
// {
|
|
135
|
+
// "path": "pages/wx",
|
|
136
|
+
// "style": {
|
|
137
|
+
// "navigationBarTitleText": "微信"
|
|
138
|
+
// }
|
|
139
|
+
// }
|
|
140
|
+
// // #endif
|
|
141
|
+
// ]
|
|
142
|
+
// }
|
|
143
|
+
|
|
144
|
+
// 按平台裁剪
|
|
145
|
+
doc.evaluate('H5')
|
|
146
|
+
// { pages: [{ path: 'pages/index', style: { navigationBarTitleText: '默认' } }] }
|
|
147
|
+
|
|
148
|
+
doc.evaluate('MP-WEIXIN')
|
|
149
|
+
// { pages: [
|
|
150
|
+
// { path: 'pages/index', style: { navigationBarTitleText: '微信首页' } },
|
|
151
|
+
// { path: 'pages/wx', style: { navigationBarTitleText: '微信' } }
|
|
152
|
+
// ] }
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## 注意事项
|
|
156
|
+
|
|
157
|
+
- 逗号策略:仅剔除条件节点,不自动修正逗号。输入需保证编译前后都合法(贴合 uniapp 官方约束)。
|
|
158
|
+
- `evaluate` 中同 key 后出现的 member 覆盖前者(uniapp 行为)。
|
|
159
|
+
- `stringify` 统一输出 `//` 风格注释,即使输入是 `/* */`。
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
[MIT](./LICENSE) License © [uni-helper](https://github.com/uni-helper)
|
|
164
|
+
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface Condition {
|
|
3
|
+
kind: 'ifdef' | 'ifndef';
|
|
4
|
+
platform: string;
|
|
5
|
+
}
|
|
6
|
+
type TreeNode = {
|
|
7
|
+
type: 'value';
|
|
8
|
+
value: string | number | boolean | null;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'object';
|
|
11
|
+
members: MemberNode[];
|
|
12
|
+
} | {
|
|
13
|
+
type: 'array';
|
|
14
|
+
elements: ElementNode[];
|
|
15
|
+
};
|
|
16
|
+
interface MemberNode {
|
|
17
|
+
key: string;
|
|
18
|
+
value: TreeNode;
|
|
19
|
+
condition?: Condition;
|
|
20
|
+
}
|
|
21
|
+
interface ElementNode {
|
|
22
|
+
value: TreeNode;
|
|
23
|
+
condition?: Condition;
|
|
24
|
+
}
|
|
25
|
+
interface ConditionalRange {
|
|
26
|
+
start: number;
|
|
27
|
+
end: number;
|
|
28
|
+
condition: Condition;
|
|
29
|
+
}
|
|
30
|
+
type Path = (string | number)[] | string;
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/tree.d.ts
|
|
33
|
+
declare function evaluateTree(node: TreeNode, platform: string): unknown;
|
|
34
|
+
declare function stringifyTree(node: TreeNode, indent?: number): string;
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/index.d.ts
|
|
37
|
+
declare class JsonuDocument {
|
|
38
|
+
private root;
|
|
39
|
+
constructor(root: TreeNode);
|
|
40
|
+
evaluate(platform: string): unknown;
|
|
41
|
+
stringify(indent?: number): string;
|
|
42
|
+
set(platform: string, data: unknown, path?: Path): this;
|
|
43
|
+
merge(platform: string, data: unknown, path?: Path): this;
|
|
44
|
+
delete(platform: string, path?: Path): this;
|
|
45
|
+
private navigate;
|
|
46
|
+
private removeConditional;
|
|
47
|
+
private addConditional;
|
|
48
|
+
}
|
|
49
|
+
declare function parse(jsonu: string): JsonuDocument;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { Condition, ConditionalRange, ElementNode, JsonuDocument, MemberNode, Path, TreeNode, evaluateTree, parse, stringifyTree };
|