lyb-js 1.6.8 → 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
  }
@@ -2,10 +2,6 @@
2
2
  * @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumberStepper-数字步进器
3
3
  */
4
4
  export declare class LibJsNumberStepper {
5
- /** 当前数字索引 */
6
- private _currentIndex;
7
- /** 金额数 */
8
- private _numsLength;
9
5
  /** 当前按下状态 */
10
6
  private _isDown;
11
7
  /** 定时器ID */
@@ -18,13 +14,11 @@ export declare class LibJsNumberStepper {
18
14
  * @param numsLength 数字长度
19
15
  * @param onChange 数字变动时触发
20
16
  */
21
- constructor(numsLength: number, onChange: (index: number) => void);
17
+ constructor(onChange: (index: "add" | "sub") => void);
22
18
  /** @description 按下
23
19
  * @param type 操作类型 add:加 sub:减
24
20
  */
25
21
  down(type: "add" | "sub"): void;
26
- /** @description 更新索引 */
27
- updateIndex(index: number): void;
28
22
  /** @description 抬起 */
29
23
  private _up;
30
24
  /** @description 处理数字变化
@@ -6,15 +6,10 @@ export class LibJsNumberStepper {
6
6
  * @param numsLength 数字长度
7
7
  * @param onChange 数字变动时触发
8
8
  */
9
- constructor(numsLength, onChange) {
10
- /** 当前数字索引 */
11
- this._currentIndex = 0;
12
- /** 金额数 */
13
- this._numsLength = 0;
9
+ constructor(onChange) {
14
10
  /** 当前按下状态 */
15
11
  this._isDown = false;
16
12
  this._onChange = onChange;
17
- this._numsLength = numsLength;
18
13
  window.addEventListener("pointerup", () => {
19
14
  this._isDown && this._up();
20
15
  });
@@ -33,10 +28,6 @@ export class LibJsNumberStepper {
33
28
  }
34
29
  }, 100);
35
30
  }
36
- /** @description 更新索引 */
37
- updateIndex(index) {
38
- this._currentIndex = index;
39
- }
40
31
  /** @description 抬起 */
41
32
  _up() {
42
33
  this._isDown = false;
@@ -48,16 +39,10 @@ export class LibJsNumberStepper {
48
39
  */
49
40
  _handleChange(type) {
50
41
  if (type === "add") {
51
- if (this._currentIndex < this._numsLength - 1) {
52
- this._currentIndex++;
53
- this._onChange(this._currentIndex);
54
- }
42
+ this._onChange("add");
55
43
  }
56
44
  else if (type === "sub") {
57
- if (this._currentIndex > 0) {
58
- this._currentIndex--;
59
- this._onChange(this._currentIndex);
60
- }
45
+ this._onChange("sub");
61
46
  }
62
47
  }
63
48
  }
package/index.js CHANGED
@@ -1,2 +1 @@
1
- import * as LibJs_1 from "./libJs";
2
- export { LibJs_1 as LibJs };
1
+ export * as LibJs from "./libJs";
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
  };
@@ -3051,12 +3046,9 @@ ${log3.label}:`, log3.value];
3051
3046
  * @param numsLength 数字长度
3052
3047
  * @param onChange 数字变动时触发
3053
3048
  */
3054
- constructor(numsLength, onChange) {
3055
- this._currentIndex = 0;
3056
- this._numsLength = 0;
3049
+ constructor(onChange) {
3057
3050
  this._isDown = false;
3058
3051
  this._onChange = onChange;
3059
- this._numsLength = numsLength;
3060
3052
  window.addEventListener("pointerup", () => {
3061
3053
  this._isDown && this._up();
3062
3054
  });
@@ -3075,10 +3067,6 @@ ${log3.label}:`, log3.value];
3075
3067
  }
3076
3068
  }, 100);
3077
3069
  }
3078
- /** @description 更新索引 */
3079
- updateIndex(index) {
3080
- this._currentIndex = index;
3081
- }
3082
3070
  /** @description 抬起 */
3083
3071
  _up() {
3084
3072
  this._isDown = false;
@@ -3090,15 +3078,9 @@ ${log3.label}:`, log3.value];
3090
3078
  */
3091
3079
  _handleChange(type) {
3092
3080
  if (type === "add") {
3093
- if (this._currentIndex < this._numsLength - 1) {
3094
- this._currentIndex++;
3095
- this._onChange(this._currentIndex);
3096
- }
3081
+ this._onChange("add");
3097
3082
  } else if (type === "sub") {
3098
- if (this._currentIndex > 0) {
3099
- this._currentIndex--;
3100
- this._onChange(this._currentIndex);
3101
- }
3083
+ this._onChange("sub");
3102
3084
  }
3103
3085
  }
3104
3086
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.8",
3
+ "version": "1.6.10",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",