lyb-js 1.6.4 → 1.6.6
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/Base/LibJsResizeWatcher.d.ts +3 -1
- package/Base/LibJsResizeWatcher.js +14 -1
- package/Math/LibJsCoordsAngle.d.ts +1 -1
- package/Math/LibJsCoordsAngle.js +12 -9
- package/libJs.d.ts +1 -1
- package/lyb.js +56 -28
- package/package.json +1 -1
- package/Misc/LibJsObserver.d.ts +0 -25
- package/Misc/LibJsObserver.js +0 -100
|
@@ -7,7 +7,9 @@ type Listener = (w: number, h: number) => void;
|
|
|
7
7
|
export declare class LibJsResizeWatcher {
|
|
8
8
|
/** 存储所有监听器及其是否需要立即执行的标志 */
|
|
9
9
|
private _listeners;
|
|
10
|
-
|
|
10
|
+
/** 当前适配模式 */
|
|
11
|
+
private _mode;
|
|
12
|
+
constructor(mode: "h" | "v" | "hv");
|
|
11
13
|
/**
|
|
12
14
|
* @description 注册一个窗口尺寸变化的监听器
|
|
13
15
|
* @param cb 回调函数,参数为当前窗口宽度和高度
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
/** @description 监听窗口变化,内部只注册一次resize事件,调用监听自身可取消监听 */
|
|
2
2
|
export class LibJsResizeWatcher {
|
|
3
|
-
constructor() {
|
|
3
|
+
constructor(mode) {
|
|
4
4
|
/** 存储所有监听器及其是否需要立即执行的标志 */
|
|
5
5
|
this._listeners = [];
|
|
6
|
+
/** 当前适配模式 */
|
|
7
|
+
this._mode = "hv";
|
|
6
8
|
/** @description 内部 resize 回调函数,调用所有注册的监听器 */
|
|
7
9
|
this._handleResize = () => {
|
|
8
10
|
const w = window.innerWidth;
|
|
9
11
|
const h = window.innerHeight;
|
|
10
12
|
this._listeners.forEach(({ cb }) => cb(w, h));
|
|
11
13
|
};
|
|
14
|
+
this._mode = mode;
|
|
15
|
+
if (mode === "h" || mode === "v")
|
|
16
|
+
return;
|
|
12
17
|
//初始化时绑定窗口 resize 事件
|
|
13
18
|
window.addEventListener("resize", this._handleResize);
|
|
14
19
|
}
|
|
@@ -19,6 +24,14 @@ export class LibJsResizeWatcher {
|
|
|
19
24
|
* @returns 一个函数,用于移除该监听器
|
|
20
25
|
*/
|
|
21
26
|
on(cb, immediate = true) {
|
|
27
|
+
if (this._mode === "h") {
|
|
28
|
+
immediate && cb(1920, 1080);
|
|
29
|
+
return () => { };
|
|
30
|
+
}
|
|
31
|
+
else if (this._mode === "v") {
|
|
32
|
+
immediate && cb(1080, 1920);
|
|
33
|
+
return () => { };
|
|
34
|
+
}
|
|
22
35
|
const item = { cb, immediate };
|
|
23
36
|
this._listeners.push(item);
|
|
24
37
|
if (immediate)
|
package/Math/LibJsCoordsAngle.js
CHANGED
|
@@ -3,19 +3,22 @@
|
|
|
3
3
|
* @param coord2 终点坐标
|
|
4
4
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsCoordsAngle-两点角度
|
|
5
5
|
*/
|
|
6
|
-
export const libJsCoordsAngle = (coord1, coord2) => {
|
|
6
|
+
export const libJsCoordsAngle = (coord1, coord2, mode = "deg") => {
|
|
7
7
|
//计算相对于第一个坐标的水平和垂直距离
|
|
8
8
|
const deltaX = coord2.x - coord1.x;
|
|
9
9
|
const deltaY = coord2.y - coord1.y;
|
|
10
10
|
//使用反三角函数计算角度(以弧度为单位)
|
|
11
11
|
const angleRad = Math.atan2(deltaY, deltaX);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
angleDeg
|
|
12
|
+
if (mode === "deg") {
|
|
13
|
+
//将弧度转换为角度
|
|
14
|
+
let angleDeg = angleRad * (180 / Math.PI);
|
|
15
|
+
//将角度转换为顺时针方向为正方向的角度
|
|
16
|
+
angleDeg = -angleDeg + 90;
|
|
17
|
+
//调整角度使得右边成为 360 度的位置变为 0 度
|
|
18
|
+
if (angleDeg < 0) {
|
|
19
|
+
angleDeg += 360;
|
|
20
|
+
}
|
|
21
|
+
return angleDeg;
|
|
19
22
|
}
|
|
20
|
-
return
|
|
23
|
+
return angleRad;
|
|
21
24
|
};
|
package/libJs.d.ts
CHANGED
package/lyb.js
CHANGED
|
@@ -284,16 +284,19 @@ ${log3.label}:`, log3.value];
|
|
|
284
284
|
throw new Error("请使用正确类型");
|
|
285
285
|
}
|
|
286
286
|
};
|
|
287
|
-
const libJsCoordsAngle = (coord1, coord2) => {
|
|
287
|
+
const libJsCoordsAngle = (coord1, coord2, mode = "deg") => {
|
|
288
288
|
const deltaX = coord2.x - coord1.x;
|
|
289
289
|
const deltaY = coord2.y - coord1.y;
|
|
290
290
|
const angleRad = Math.atan2(deltaY, deltaX);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
angleDeg
|
|
291
|
+
if (mode === "deg") {
|
|
292
|
+
let angleDeg = angleRad * (180 / Math.PI);
|
|
293
|
+
angleDeg = -angleDeg + 90;
|
|
294
|
+
if (angleDeg < 0) {
|
|
295
|
+
angleDeg += 360;
|
|
296
|
+
}
|
|
297
|
+
return angleDeg;
|
|
295
298
|
}
|
|
296
|
-
return
|
|
299
|
+
return angleRad;
|
|
297
300
|
};
|
|
298
301
|
const libJsCoordsDistance = (coord1, coord2) => {
|
|
299
302
|
const deltaX = coord2.x - coord1.x;
|
|
@@ -302,10 +305,10 @@ ${log3.label}:`, log3.value];
|
|
|
302
305
|
return distance;
|
|
303
306
|
};
|
|
304
307
|
/*!
|
|
305
|
-
* decimal.js v10.
|
|
308
|
+
* decimal.js v10.5.0
|
|
306
309
|
* An arbitrary-precision Decimal type for JavaScript.
|
|
307
310
|
* https://github.com/MikeMcl/decimal.js
|
|
308
|
-
* Copyright (c)
|
|
311
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
309
312
|
* MIT Licence
|
|
310
313
|
*/
|
|
311
314
|
var EXP_LIMIT = 9e15, MAX_DIGITS = 1e9, NUMERALS = "0123456789abcdef", LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058", PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789", DEFAULTS = {
|
|
@@ -574,7 +577,7 @@ ${log3.label}:`, log3.value];
|
|
|
574
577
|
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
575
578
|
};
|
|
576
579
|
P.inverseCosine = P.acos = function() {
|
|
577
|
-
var
|
|
580
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
578
581
|
if (k !== -1) {
|
|
579
582
|
return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
580
583
|
}
|
|
@@ -582,11 +585,10 @@ ${log3.label}:`, log3.value];
|
|
|
582
585
|
return getPi(Ctor, pr + 4, rm).times(0.5);
|
|
583
586
|
Ctor.precision = pr + 6;
|
|
584
587
|
Ctor.rounding = 1;
|
|
585
|
-
x = x.
|
|
586
|
-
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
588
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
587
589
|
Ctor.precision = pr;
|
|
588
590
|
Ctor.rounding = rm;
|
|
589
|
-
return
|
|
591
|
+
return x.times(2);
|
|
590
592
|
};
|
|
591
593
|
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
592
594
|
var pr, rm, x = this, Ctor = x.constructor;
|
|
@@ -1798,14 +1800,16 @@ ${log3.label}:`, log3.value];
|
|
|
1798
1800
|
function isOdd(n) {
|
|
1799
1801
|
return n.d[n.d.length - 1] & 1;
|
|
1800
1802
|
}
|
|
1801
|
-
function maxOrMin(Ctor, args,
|
|
1802
|
-
var y, x = new Ctor(args[0]), i = 0;
|
|
1803
|
+
function maxOrMin(Ctor, args, n) {
|
|
1804
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
1803
1805
|
for (; ++i < args.length; ) {
|
|
1804
1806
|
y = new Ctor(args[i]);
|
|
1805
1807
|
if (!y.s) {
|
|
1806
1808
|
x = y;
|
|
1807
1809
|
break;
|
|
1808
|
-
}
|
|
1810
|
+
}
|
|
1811
|
+
k = x.cmp(y);
|
|
1812
|
+
if (k === n || k === 0 && x.s === n) {
|
|
1809
1813
|
x = y;
|
|
1810
1814
|
}
|
|
1811
1815
|
}
|
|
@@ -2386,7 +2390,8 @@ ${log3.label}:`, log3.value];
|
|
|
2386
2390
|
x.d = [v];
|
|
2387
2391
|
}
|
|
2388
2392
|
return;
|
|
2389
|
-
}
|
|
2393
|
+
}
|
|
2394
|
+
if (v * 0 !== 0) {
|
|
2390
2395
|
if (!v)
|
|
2391
2396
|
x.s = NaN;
|
|
2392
2397
|
x.e = NaN;
|
|
@@ -2394,18 +2399,28 @@ ${log3.label}:`, log3.value];
|
|
|
2394
2399
|
return;
|
|
2395
2400
|
}
|
|
2396
2401
|
return parseDecimal(x, v.toString());
|
|
2397
|
-
} else if (t !== "string") {
|
|
2398
|
-
throw Error(invalidArgument + v);
|
|
2399
2402
|
}
|
|
2400
|
-
if (
|
|
2401
|
-
|
|
2402
|
-
x.s = -1;
|
|
2403
|
-
} else {
|
|
2404
|
-
if (i2 === 43)
|
|
2403
|
+
if (t === "string") {
|
|
2404
|
+
if ((i2 = v.charCodeAt(0)) === 45) {
|
|
2405
2405
|
v = v.slice(1);
|
|
2406
|
-
|
|
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);
|
|
2407
2413
|
}
|
|
2408
|
-
|
|
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());
|
|
2422
|
+
}
|
|
2423
|
+
throw Error(invalidArgument + v);
|
|
2409
2424
|
}
|
|
2410
2425
|
Decimal2.prototype = P;
|
|
2411
2426
|
Decimal2.ROUND_UP = 0;
|
|
@@ -2515,10 +2530,10 @@ ${log3.label}:`, log3.value];
|
|
|
2515
2530
|
return new this(x).log(10);
|
|
2516
2531
|
}
|
|
2517
2532
|
function max() {
|
|
2518
|
-
return maxOrMin(this, arguments,
|
|
2533
|
+
return maxOrMin(this, arguments, -1);
|
|
2519
2534
|
}
|
|
2520
2535
|
function min() {
|
|
2521
|
-
return maxOrMin(this, arguments,
|
|
2536
|
+
return maxOrMin(this, arguments, 1);
|
|
2522
2537
|
}
|
|
2523
2538
|
function mod(x, y) {
|
|
2524
2539
|
return new this(x).mod(y);
|
|
@@ -3508,13 +3523,17 @@ ${log3.label}:`, log3.value];
|
|
|
3508
3523
|
}
|
|
3509
3524
|
};
|
|
3510
3525
|
class LibJsResizeWatcher {
|
|
3511
|
-
constructor() {
|
|
3526
|
+
constructor(mode) {
|
|
3512
3527
|
this._listeners = [];
|
|
3528
|
+
this._mode = "hv";
|
|
3513
3529
|
this._handleResize = () => {
|
|
3514
3530
|
const w = window.innerWidth;
|
|
3515
3531
|
const h = window.innerHeight;
|
|
3516
3532
|
this._listeners.forEach(({ cb }) => cb(w, h));
|
|
3517
3533
|
};
|
|
3534
|
+
this._mode = mode;
|
|
3535
|
+
if (mode === "h" || mode === "v")
|
|
3536
|
+
return;
|
|
3518
3537
|
window.addEventListener("resize", this._handleResize);
|
|
3519
3538
|
}
|
|
3520
3539
|
/**
|
|
@@ -3524,6 +3543,15 @@ ${log3.label}:`, log3.value];
|
|
|
3524
3543
|
* @returns 一个函数,用于移除该监听器
|
|
3525
3544
|
*/
|
|
3526
3545
|
on(cb, immediate = true) {
|
|
3546
|
+
if (this._mode === "h") {
|
|
3547
|
+
immediate && cb(1920, 1080);
|
|
3548
|
+
return () => {
|
|
3549
|
+
};
|
|
3550
|
+
} else if (this._mode === "v") {
|
|
3551
|
+
immediate && cb(1080, 1920);
|
|
3552
|
+
return () => {
|
|
3553
|
+
};
|
|
3554
|
+
}
|
|
3527
3555
|
const item = { cb, immediate };
|
|
3528
3556
|
this._listeners.push(item);
|
|
3529
3557
|
if (immediate)
|
package/package.json
CHANGED
package/Misc/LibJsObserver.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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 {};
|
package/Misc/LibJsObserver.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
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);
|