lyb-js 1.6.7 → 1.6.8

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) => T;
4
+ export declare const libJsDeepJSONParse: <T>(data: any, seen?: WeakSet<object>) => T;
@@ -1,30 +1,34 @@
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") {
4
+ export const libJsDeepJSONParse = (data, seen = new WeakSet()) => {
5
+ // 检查是否为字符串并尝试解析(仅在可能是JSON时才解析)
6
+ if (typeof data === "string" && /^[\[{]/.test(data.trim())) {
7
7
  try {
8
8
  const parsed = JSON.parse(data);
9
- //递归解析解析后的结果
10
- return libJsDeepJSONParse(parsed);
9
+ return libJsDeepJSONParse(parsed, seen);
11
10
  }
12
11
  catch (_a) {
13
- //如果解析失败,返回原始字符串
14
12
  return data;
15
13
  }
16
14
  }
17
- //如果是数组,递归处理每个元素
15
+ // 循环引用检测
16
+ if (data !== null && typeof data === "object") {
17
+ if (seen.has(data))
18
+ return data;
19
+ seen.add(data);
20
+ }
21
+ // 如果是数组,递归处理每个元素
18
22
  if (Array.isArray(data)) {
19
- return data.map((item) => libJsDeepJSONParse(item));
23
+ return data.map((item) => libJsDeepJSONParse(item, seen));
20
24
  }
21
- //如果是对象,递归处理每个属性值
25
+ // 如果是对象,递归处理每个属性值
22
26
  if (data !== null && typeof data === "object") {
23
- return Object.keys(data).reduce((acc, key) => {
24
- acc[key] = libJsDeepJSONParse(data[key]);
27
+ return Object.entries(data).reduce((acc, [key, value]) => {
28
+ acc[key] = libJsDeepJSONParse(value, seen);
25
29
  return acc;
26
30
  }, {});
27
31
  }
28
- //其他情况返回原始值
32
+ // 其他情况返回原始值
29
33
  return data;
30
34
  };
@@ -0,0 +1,25 @@
1
+ type Callback<T> = (newValue: T[keyof T], oldValue: T[keyof T]) => void;
2
+ export declare class LibJsObserver<Store> {
3
+ /** 递增索引 */
4
+ private _index;
5
+ private _store;
6
+ private _lastData;
7
+ private _listeners;
8
+ constructor(store: Store);
9
+ /** @description 监听
10
+ * @param key 要监听的键
11
+ * @param callback key值变化时执行的回调函数
12
+ * @param immediately 监听时是否立即执行回调函数
13
+ * @returns 返回一个函数用于注销监听器
14
+ */
15
+ on(key: keyof Store, callback: Callback<Store>, immediately?: boolean): () => void;
16
+ /** @description 获取数据 */
17
+ getData(key: keyof Store): Store[keyof Store];
18
+ /** @description 手动更新数据 */
19
+ setData(newData: Partial<Store>): void;
20
+ /** @description 通知监听器更新 */
21
+ private _notifyListeners;
22
+ /** @description 判断数据变化并通知监听器 */
23
+ private _update;
24
+ }
25
+ export {};
@@ -0,0 +1,100 @@
1
+ export class LibJsObserver {
2
+ constructor(store) {
3
+ /** 递增索引 */
4
+ this._index = 0;
5
+ // 监听器映射,用于存储不同键的回调函数
6
+ this._listeners = new Map();
7
+ this._store = store;
8
+ this._lastData = Object.assign({}, store);
9
+ }
10
+ /** @description 监听
11
+ * @param key 要监听的键
12
+ * @param callback key值变化时执行的回调函数
13
+ * @param immediately 监听时是否立即执行回调函数
14
+ * @returns 返回一个函数用于注销监听器
15
+ */
16
+ on(key, callback, immediately = true) {
17
+ this._index += 1;
18
+ const index = this._index;
19
+ // 如果该键没有对应的监听器集合,则初始化一个新的集合
20
+ if (!this._listeners.has(key)) {
21
+ this._listeners.set(key, new Map());
22
+ }
23
+ // 将回调函数添加到对应键的监听器集合中
24
+ this._listeners.get(key).set(index, callback);
25
+ immediately && callback(this._store[key], this._lastData[key]);
26
+ return () => {
27
+ // 从特定键的监听器集合中移除指定标识符的回调函数
28
+ const listenerMap = this._listeners.get(key);
29
+ if (listenerMap) {
30
+ listenerMap.delete(index);
31
+ }
32
+ else {
33
+ console.warn(`监听 Key "${key.toString()}" 重复注销事件`);
34
+ }
35
+ };
36
+ }
37
+ /** @description 获取数据 */
38
+ getData(key) {
39
+ return this._store[key];
40
+ }
41
+ /** @description 手动更新数据 */
42
+ setData(newData) {
43
+ this._store = Object.assign(Object.assign({}, this._store), newData);
44
+ this._update();
45
+ }
46
+ /** @description 通知监听器更新 */
47
+ _notifyListeners(key, newValue, oldValue) {
48
+ // 获取特定键的所有监听器
49
+ const keyListeners = this._listeners.get(key);
50
+ if (keyListeners) {
51
+ // 遍历并执行每个监听器的回调函数
52
+ keyListeners.forEach((callback) => callback(newValue, oldValue));
53
+ }
54
+ }
55
+ /** @description 判断数据变化并通知监听器 */
56
+ _update() {
57
+ // 遍历当前数据对象的每个键,检查是否有变化
58
+ for (const key in this._store) {
59
+ const typedKey = key;
60
+ const newValue = this._store[typedKey];
61
+ const oldValue = this._lastData[typedKey];
62
+ //如果旧值与新值不相等,则通知监听器
63
+ if (newValue !== oldValue) {
64
+ this._notifyListeners(typedKey, newValue, oldValue);
65
+ this._lastData[typedKey] = this._store[typedKey];
66
+ }
67
+ }
68
+ }
69
+ }
70
+ class LibJsStore extends LibJsObserver {
71
+ constructor() {
72
+ super({
73
+ name: "张三",
74
+ age: 20,
75
+ });
76
+ }
77
+ }
78
+ const store = new LibJsStore();
79
+ const offA = store.on("age", (newValue) => {
80
+ console.log(`A收到年龄更新:`, newValue);
81
+ });
82
+ store.on("age", (newValue) => {
83
+ console.log(`B收到年龄更新:`, newValue);
84
+ });
85
+ // 模拟更新数据
86
+ setTimeout(() => {
87
+ console.log("尝试更新年龄值为:25");
88
+ store.setData({ age: 25 });
89
+ setTimeout(() => {
90
+ console.warn("注销A监听器");
91
+ offA();
92
+ setTimeout(() => {
93
+ console.warn("尝试更新年龄值为:26");
94
+ store.setData({ age: 26 });
95
+ setTimeout(() => {
96
+ console.log("尝试获取年龄值:", store.getData("age"));
97
+ }, 1000);
98
+ }, 1000);
99
+ }, 1000);
100
+ }, 1000);
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) => T;
81
+ libJsDeepJSONParse: <T>(data: any, seen?: WeakSet<object>) => T;
82
82
  /**
83
83
  * @description 分类汇总,将数组对象按照指定键值整理成一个以键值为键名的对象
84
84
  * @param arr 要分组的数组
package/lyb.js CHANGED
@@ -133,23 +133,31 @@ ${log3.label}:`, log3.value];
133
133
  }
134
134
  return result;
135
135
  };
136
- const libJsDeepJSONParse = (data) => {
137
- if (typeof data === "string") {
136
+ const libJsDeepJSONParse = (data, seen = /* @__PURE__ */ new WeakSet()) => {
137
+ if (typeof data === "string" && /^[\[{]/.test(data.trim())) {
138
138
  try {
139
139
  const parsed = JSON.parse(data);
140
- return libJsDeepJSONParse(parsed);
140
+ return libJsDeepJSONParse(parsed, seen);
141
141
  } catch {
142
142
  return data;
143
143
  }
144
144
  }
145
+ if (data !== null && typeof data === "object") {
146
+ if (seen.has(data))
147
+ return data;
148
+ seen.add(data);
149
+ }
145
150
  if (Array.isArray(data)) {
146
- return data.map((item) => libJsDeepJSONParse(item));
151
+ return data.map((item) => libJsDeepJSONParse(item, seen));
147
152
  }
148
153
  if (data !== null && typeof data === "object") {
149
- return Object.keys(data).reduce((acc, key) => {
150
- acc[key] = libJsDeepJSONParse(data[key]);
151
- return acc;
152
- }, {});
154
+ return Object.entries(data).reduce(
155
+ (acc, [key, value]) => {
156
+ acc[key] = libJsDeepJSONParse(value, seen);
157
+ return acc;
158
+ },
159
+ {}
160
+ );
153
161
  }
154
162
  return data;
155
163
  };
@@ -305,10 +313,10 @@ ${log3.label}:`, log3.value];
305
313
  return distance;
306
314
  };
307
315
  /*!
308
- * decimal.js v10.5.0
316
+ * decimal.js v10.4.3
309
317
  * An arbitrary-precision Decimal type for JavaScript.
310
318
  * https://github.com/MikeMcl/decimal.js
311
- * Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
319
+ * Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
312
320
  * MIT Licence
313
321
  */
314
322
  var EXP_LIMIT = 9e15, MAX_DIGITS = 1e9, NUMERALS = "0123456789abcdef", LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058", PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789", DEFAULTS = {
@@ -577,7 +585,7 @@ ${log3.label}:`, log3.value];
577
585
  return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
578
586
  };
579
587
  P.inverseCosine = P.acos = function() {
580
- var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
588
+ var halfPi, x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
581
589
  if (k !== -1) {
582
590
  return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
583
591
  }
@@ -585,10 +593,11 @@ ${log3.label}:`, log3.value];
585
593
  return getPi(Ctor, pr + 4, rm).times(0.5);
586
594
  Ctor.precision = pr + 6;
587
595
  Ctor.rounding = 1;
588
- x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
596
+ x = x.asin();
597
+ halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
589
598
  Ctor.precision = pr;
590
599
  Ctor.rounding = rm;
591
- return x.times(2);
600
+ return halfPi.minus(x);
592
601
  };
593
602
  P.inverseHyperbolicCosine = P.acosh = function() {
594
603
  var pr, rm, x = this, Ctor = x.constructor;
@@ -1800,16 +1809,14 @@ ${log3.label}:`, log3.value];
1800
1809
  function isOdd(n) {
1801
1810
  return n.d[n.d.length - 1] & 1;
1802
1811
  }
1803
- function maxOrMin(Ctor, args, n) {
1804
- var k, y, x = new Ctor(args[0]), i = 0;
1812
+ function maxOrMin(Ctor, args, ltgt) {
1813
+ var y, x = new Ctor(args[0]), i = 0;
1805
1814
  for (; ++i < args.length; ) {
1806
1815
  y = new Ctor(args[i]);
1807
1816
  if (!y.s) {
1808
1817
  x = y;
1809
1818
  break;
1810
- }
1811
- k = x.cmp(y);
1812
- if (k === n || k === 0 && x.s === n) {
1819
+ } else if (x[ltgt](y)) {
1813
1820
  x = y;
1814
1821
  }
1815
1822
  }
@@ -2390,8 +2397,7 @@ ${log3.label}:`, log3.value];
2390
2397
  x.d = [v];
2391
2398
  }
2392
2399
  return;
2393
- }
2394
- if (v * 0 !== 0) {
2400
+ } else if (v * 0 !== 0) {
2395
2401
  if (!v)
2396
2402
  x.s = NaN;
2397
2403
  x.e = NaN;
@@ -2399,28 +2405,18 @@ ${log3.label}:`, log3.value];
2399
2405
  return;
2400
2406
  }
2401
2407
  return parseDecimal(x, v.toString());
2408
+ } else if (t !== "string") {
2409
+ throw Error(invalidArgument + v);
2402
2410
  }
2403
- if (t === "string") {
2404
- if ((i2 = v.charCodeAt(0)) === 45) {
2411
+ if ((i2 = v.charCodeAt(0)) === 45) {
2412
+ v = v.slice(1);
2413
+ x.s = -1;
2414
+ } else {
2415
+ if (i2 === 43)
2405
2416
  v = v.slice(1);
2406
- x.s = -1;
2407
- } else {
2408
- if (i2 === 43)
2409
- v = v.slice(1);
2410
- x.s = 1;
2411
- }
2412
- return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
2413
- }
2414
- if (t === "bigint") {
2415
- if (v < 0) {
2416
- v = -v;
2417
- x.s = -1;
2418
- } else {
2419
- x.s = 1;
2420
- }
2421
- return parseDecimal(x, v.toString());
2417
+ x.s = 1;
2422
2418
  }
2423
- throw Error(invalidArgument + v);
2419
+ return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
2424
2420
  }
2425
2421
  Decimal2.prototype = P;
2426
2422
  Decimal2.ROUND_UP = 0;
@@ -2530,10 +2526,10 @@ ${log3.label}:`, log3.value];
2530
2526
  return new this(x).log(10);
2531
2527
  }
2532
2528
  function max() {
2533
- return maxOrMin(this, arguments, -1);
2529
+ return maxOrMin(this, arguments, "lt");
2534
2530
  }
2535
2531
  function min() {
2536
- return maxOrMin(this, arguments, 1);
2532
+ return maxOrMin(this, arguments, "gt");
2537
2533
  }
2538
2534
  function mod(x, y) {
2539
2535
  return new this(x).mod(y);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.7",
3
+ "version": "1.6.8",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",