@zwa73/utils 1.0.21 → 1.0.22

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.
@@ -65,7 +65,7 @@ export declare function loadJSONFile(filePath: string): Promise<JObject>;
65
65
  * void (string,Object)
66
66
  * @async
67
67
  * @param {string} filePath - 文件路径
68
- * @param {JObject} obj - 所要写入的JObject
68
+ * @param {JObject} obj - 所要写入的JObject
69
69
  * @returns {Promise<void>}
70
70
  */
71
71
  export declare function writeJSONFile(filePath: string, obj: JObject): Promise<void>;
@@ -165,7 +165,7 @@ exports.loadJSONFile = loadJSONFile;
165
165
  * void (string,Object)
166
166
  * @async
167
167
  * @param {string} filePath - 文件路径
168
- * @param {JObject} obj - 所要写入的JObject
168
+ * @param {JObject} obj - 所要写入的JObject
169
169
  * @returns {Promise<void>}
170
170
  */
171
171
  async function writeJSONFile(filePath, obj) {
@@ -1,7 +1,7 @@
1
- /**可以序列化为JSON文件的Object*/
2
1
  export type JToken = JObject | JArray | JValue;
3
2
  export type JValue = number | string | boolean | null;
4
3
  export type JArray = Array<JToken>;
4
+ /**可以序列化为JSON文件的Object*/
5
5
  export type JObject = {
6
6
  [key: string]: JToken;
7
7
  };
@@ -17,10 +17,21 @@ export type MessageEntity = {
17
17
  role: string;
18
18
  content: string;
19
19
  };
20
- export interface IJObjectData {
20
+ /**可以保存为JObject的类
21
+ */
22
+ export interface IJData {
23
+ /**保存为JObject
24
+ */
21
25
  saveToJObject(): JObject;
22
26
  }
23
- export type JObjectData = {
24
- [key: string]: JToken | JObjectData | IJObjectData;
27
+ export type JDToken = JToken | IJData | JDArray;
28
+ export type JDArray = Array<JDToken>;
29
+ /**可以转换为JObject的Object*/
30
+ export type JDObject = {
31
+ [key: string]: JDToken;
25
32
  };
26
- export declare function parseJObjectData(): void;
33
+ /**将JDToken转换为JToken
34
+ * @param {JDToken} token - 待转换的JDToken
35
+ * @returns {JToken} - 转换完成的JToken
36
+ */
37
+ export declare function parseJOData(token: JDToken): JToken;
@@ -1,5 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseJObjectData = void 0;
4
- function parseJObjectData() { }
5
- exports.parseJObjectData = parseJObjectData;
3
+ exports.parseJOData = void 0;
4
+ /**将JDToken转换为JToken
5
+ * @param {JDToken} token - 待转换的JDToken
6
+ * @returns {JToken} - 转换完成的JToken
7
+ */
8
+ function parseJOData(token) {
9
+ if (token && typeof token.saveToJObject === 'function')
10
+ return token.saveToJObject();
11
+ if (token && (typeof token === 'number' ||
12
+ typeof token === 'boolean' ||
13
+ typeof token === 'string' ||
14
+ token === null))
15
+ return token;
16
+ if (token && Array.isArray(token)) {
17
+ let outArr = [];
18
+ for (let item of token)
19
+ outArr.push(parseJOData(item));
20
+ return outArr;
21
+ }
22
+ let nobj = {};
23
+ for (let key in token) {
24
+ let value = token[key];
25
+ nobj[key] = parseJOData(value);
26
+ }
27
+ return nobj;
28
+ }
29
+ exports.parseJOData = parseJOData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.21",
3
+ "version": "1.0.22",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -168,7 +168,7 @@ export async function loadJSONFile(filePath: string): Promise<JObject> {
168
168
  * void (string,Object)
169
169
  * @async
170
170
  * @param {string} filePath - 文件路径
171
- * @param {JObject} obj - 所要写入的JObject
171
+ * @param {JObject} obj - 所要写入的JObject
172
172
  * @returns {Promise<void>}
173
173
  */
174
174
  export async function writeJSONFile(
@@ -1,10 +1,8 @@
1
- /**可以序列化为JSON文件的Object*/
2
- //export type JObject={
3
- // [key: string]: JObject | string | number | boolean | null | Array<JObject| string | number | boolean | null>;
4
- //}
1
+
5
2
  export type JToken = JObject|JArray|JValue;
6
3
  export type JValue = number|string|boolean|null;
7
4
  export type JArray = Array<JToken>;
5
+ /**可以序列化为JSON文件的Object*/
8
6
  export type JObject = {
9
7
  [key:string]:JToken;
10
8
  }
@@ -23,11 +21,44 @@ export type MessageEntity={
23
21
  content:string;
24
22
  }
25
23
 
26
- export interface IJObjectData{
24
+ /**可以保存为JObject的类
25
+ */
26
+ export interface IJData{
27
+ /**保存为JObject
28
+ */
27
29
  saveToJObject():JObject;
28
30
  }
29
- export type JObjectData = {
30
- [key:string]:JToken|JObjectData|IJObjectData;
31
+ export type JDToken = JToken|IJData|JDArray;
32
+ export type JDArray = Array<JDToken>;
33
+ /**可以转换为JObject的Object*/
34
+ export type JDObject = {
35
+ [key:string]:JDToken;
31
36
  }
37
+ /**将JDToken转换为JToken
38
+ * @param {JDToken} token - 待转换的JDToken
39
+ * @returns {JToken} - 转换完成的JToken
40
+ */
41
+ export function parseJOData(token:JDToken):JToken{
42
+ if(token && typeof (token as any as IJData).saveToJObject === 'function')
43
+ return (token as any as IJData).saveToJObject();
32
44
 
33
- export function parseJObjectData(){}
45
+ if(token && (typeof token === 'number' ||
46
+ typeof token === 'boolean' ||
47
+ typeof token === 'string' ||
48
+ token === null)
49
+ )return token;
50
+
51
+ if(token && Array.isArray(token)){
52
+ let outArr = [];
53
+ for(let item of token)
54
+ outArr.push(parseJOData(item));
55
+ return outArr;
56
+ }
57
+
58
+ let nobj = {} as JObject;
59
+ for(let key in (token as any as JDObject)){
60
+ let value = (token as any as JDObject)[key];
61
+ nobj[key] = parseJOData(value);
62
+ }
63
+ return nobj;
64
+ }