lyb-js 1.6.9 → 1.6.10
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/Data/LibJsDeepJSONParse.d.ts +2 -2
- package/Data/LibJsDeepJSONParse.js +18 -19
- package/libJs.d.ts +1 -1
- package/lyb.js +15 -20
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @description 递归将JSON
|
|
1
|
+
/** @description 递归将JSON字符串深度解析为对象
|
|
2
2
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsDeepJSONParse-深度解析JSON
|
|
3
3
|
*/
|
|
4
|
-
export declare const libJsDeepJSONParse:
|
|
4
|
+
export declare const libJsDeepJSONParse: (data: any) => any;
|
|
@@ -1,31 +1,30 @@
|
|
|
1
|
-
/** @description 递归将JSON
|
|
1
|
+
/** @description 递归将JSON字符串深度解析为对象
|
|
2
2
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsDeepJSONParse-深度解析JSON
|
|
3
3
|
*/
|
|
4
|
-
export const libJsDeepJSONParse = (data
|
|
5
|
-
//
|
|
6
|
-
if (typeof data === "string"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
export const libJsDeepJSONParse = (data) => {
|
|
5
|
+
// 检查是否为字符串并尝试解析
|
|
6
|
+
if (typeof data === "string") {
|
|
7
|
+
const trimmed = data.trim();
|
|
8
|
+
// 仅当字符串看起来是对象或数组时才尝试解析
|
|
9
|
+
if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
10
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]"))) {
|
|
11
|
+
try {
|
|
12
|
+
return libJsDeepJSONParse(JSON.parse(trimmed));
|
|
13
|
+
}
|
|
14
|
+
catch (_a) {
|
|
15
|
+
return data; // 如果解析失败,返回原始字符串
|
|
16
|
+
}
|
|
10
17
|
}
|
|
11
|
-
|
|
12
|
-
return data;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
// 循环引用检测
|
|
16
|
-
if (data !== null && typeof data === "object") {
|
|
17
|
-
if (seen.has(data))
|
|
18
|
-
return data;
|
|
19
|
-
seen.add(data);
|
|
18
|
+
return data; // 非 JSON 字符串直接返回
|
|
20
19
|
}
|
|
21
20
|
// 如果是数组,递归处理每个元素
|
|
22
21
|
if (Array.isArray(data)) {
|
|
23
|
-
return data.map((item) => libJsDeepJSONParse(item
|
|
22
|
+
return data.map((item) => libJsDeepJSONParse(item));
|
|
24
23
|
}
|
|
25
24
|
// 如果是对象,递归处理每个属性值
|
|
26
25
|
if (data !== null && typeof data === "object") {
|
|
27
|
-
return Object.
|
|
28
|
-
acc[key] = libJsDeepJSONParse(
|
|
26
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
27
|
+
acc[key] = libJsDeepJSONParse(data[key]);
|
|
29
28
|
return acc;
|
|
30
29
|
}, {});
|
|
31
30
|
}
|
package/libJs.d.ts
CHANGED
|
@@ -78,7 +78,7 @@ export declare const Data: {
|
|
|
78
78
|
/** @description 递归将JSON字符串深度解析为对象
|
|
79
79
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsDeepJSONParse-深度解析JSON
|
|
80
80
|
*/
|
|
81
|
-
libJsDeepJSONParse:
|
|
81
|
+
libJsDeepJSONParse: (data: any) => any;
|
|
82
82
|
/**
|
|
83
83
|
* @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
|
|
84
84
|
* @param arr 要分组的数组
|
package/lyb.js
CHANGED
|
@@ -133,31 +133,26 @@ ${log3.label}:`, log3.value];
|
|
|
133
133
|
}
|
|
134
134
|
return result;
|
|
135
135
|
};
|
|
136
|
-
const libJsDeepJSONParse = (data
|
|
137
|
-
if (typeof data === "string"
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
136
|
+
const libJsDeepJSONParse = (data) => {
|
|
137
|
+
if (typeof data === "string") {
|
|
138
|
+
const trimmed = data.trim();
|
|
139
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
140
|
+
try {
|
|
141
|
+
return libJsDeepJSONParse(JSON.parse(trimmed));
|
|
142
|
+
} catch {
|
|
143
|
+
return data;
|
|
144
|
+
}
|
|
143
145
|
}
|
|
144
|
-
|
|
145
|
-
if (data !== null && typeof data === "object") {
|
|
146
|
-
if (seen.has(data))
|
|
147
|
-
return data;
|
|
148
|
-
seen.add(data);
|
|
146
|
+
return data;
|
|
149
147
|
}
|
|
150
148
|
if (Array.isArray(data)) {
|
|
151
|
-
return data.map((item) => libJsDeepJSONParse(item
|
|
149
|
+
return data.map((item) => libJsDeepJSONParse(item));
|
|
152
150
|
}
|
|
153
151
|
if (data !== null && typeof data === "object") {
|
|
154
|
-
return Object.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
},
|
|
159
|
-
{}
|
|
160
|
-
);
|
|
152
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
153
|
+
acc[key] = libJsDeepJSONParse(data[key]);
|
|
154
|
+
return acc;
|
|
155
|
+
}, {});
|
|
161
156
|
}
|
|
162
157
|
return data;
|
|
163
158
|
};
|