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.
@@ -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: <T>(data: any, seen?: WeakSet<object>) => T;
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, seen = new WeakSet()) => {
5
- // 检查是否为字符串并尝试解析(仅在可能是JSON时才解析)
6
- if (typeof data === "string" && /^[\[{]/.test(data.trim())) {
7
- try {
8
- const parsed = JSON.parse(data);
9
- return libJsDeepJSONParse(parsed, seen);
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
- catch (_a) {
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, seen));
22
+ return data.map((item) => libJsDeepJSONParse(item));
24
23
  }
25
24
  // 如果是对象,递归处理每个属性值
26
25
  if (data !== null && typeof data === "object") {
27
- return Object.entries(data).reduce((acc, [key, value]) => {
28
- acc[key] = libJsDeepJSONParse(value, seen);
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: <T>(data: any, seen?: WeakSet<object>) => T;
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, seen = /* @__PURE__ */ new WeakSet()) => {
137
- if (typeof data === "string" && /^[\[{]/.test(data.trim())) {
138
- try {
139
- const parsed = JSON.parse(data);
140
- return libJsDeepJSONParse(parsed, seen);
141
- } catch {
142
- return data;
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, seen));
149
+ return data.map((item) => libJsDeepJSONParse(item));
152
150
  }
153
151
  if (data !== null && typeof data === "object") {
154
- return Object.entries(data).reduce(
155
- (acc, [key, value]) => {
156
- acc[key] = libJsDeepJSONParse(value, seen);
157
- return acc;
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.9",
3
+ "version": "1.6.10",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",