lyb-js 1.6.30 → 1.6.32
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/LibIsNull.d.ts +2 -0
- package/Base/LibIsNull.js +2 -0
- package/Base/LibJsIsNull.d.ts +2 -0
- package/Base/LibJsIsNull.js +2 -0
- package/Base/LibJsPromiseTimeout.js +1 -1
- package/Base/LibJsResizeWatcher.js +20 -18
- package/Data/LibJsDeepJSONParse.js +1 -1
- package/Formatter/LibJsFormatterByte.js +1 -1
- package/Misc/LibJsClassObservable.js +13 -13
- package/Misc/LibJsEmitter.js +1 -2
- package/Misc/LibJsEmitterClose.js +1 -1
- package/Misc/LibJsNumberStepper.js +8 -2
- package/Misc/LibJsPruneEmpty.d.ts +2 -0
- package/Misc/LibJsPruneEmpty.js +22 -0
- package/README.md +10 -0
- package/package.json +2 -2
|
@@ -26,7 +26,7 @@ export const libJsPromiseTimeout = (delay = 1, fn) => {
|
|
|
26
26
|
};
|
|
27
27
|
const startTimeout = (resolve) => {
|
|
28
28
|
timeoutId = setTimeout(() => {
|
|
29
|
-
fn
|
|
29
|
+
fn?.();
|
|
30
30
|
resolve();
|
|
31
31
|
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
32
32
|
}, remainingTime);
|
|
@@ -1,24 +1,12 @@
|
|
|
1
1
|
/** @description 监听窗口变化,内部只注册一次resize事件,调用监听自身可取消监听 */
|
|
2
2
|
export class LibJsResizeWatcher {
|
|
3
|
+
/** 存储所有监听器及其是否需要立即执行的标志 */
|
|
4
|
+
_listeners = [];
|
|
5
|
+
/** 按id存储监听器 */
|
|
6
|
+
_listenerMap = new Map();
|
|
7
|
+
/** 当前适配模式 */
|
|
8
|
+
_mode;
|
|
3
9
|
constructor(mode = "hv") {
|
|
4
|
-
/** 存储所有监听器及其是否需要立即执行的标志 */
|
|
5
|
-
this._listeners = [];
|
|
6
|
-
/** 按id存储监听器 */
|
|
7
|
-
this._listenerMap = new Map();
|
|
8
|
-
/** @description 内部 resize 回调函数,调用所有注册的监听器 */
|
|
9
|
-
this._handleResize = () => {
|
|
10
|
-
const w = window.innerWidth;
|
|
11
|
-
const h = window.innerHeight;
|
|
12
|
-
let s;
|
|
13
|
-
const orientation = w > h ? "h" : "v";
|
|
14
|
-
if (orientation === "h") {
|
|
15
|
-
s = Math.min(w / 1920, h / 1080);
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
s = Math.min(w / 1080, h / 1920);
|
|
19
|
-
}
|
|
20
|
-
this._listeners.forEach(({ cb }) => cb(w, h, s));
|
|
21
|
-
};
|
|
22
10
|
this._mode = mode;
|
|
23
11
|
if (mode === "h" || mode === "v")
|
|
24
12
|
return;
|
|
@@ -73,4 +61,18 @@ export class LibJsResizeWatcher {
|
|
|
73
61
|
this._listenerMap.delete(`${id}_remove`);
|
|
74
62
|
}
|
|
75
63
|
}
|
|
64
|
+
/** @description 内部 resize 回调函数,调用所有注册的监听器 */
|
|
65
|
+
_handleResize = () => {
|
|
66
|
+
const w = window.innerWidth;
|
|
67
|
+
const h = window.innerHeight;
|
|
68
|
+
let s;
|
|
69
|
+
const orientation = w > h ? "h" : "v";
|
|
70
|
+
if (orientation === "h") {
|
|
71
|
+
s = Math.min(w / 1920, h / 1080);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
s = Math.min(w / 1080, h / 1920);
|
|
75
|
+
}
|
|
76
|
+
this._listeners.forEach(({ cb }) => cb(w, h, s));
|
|
77
|
+
};
|
|
76
78
|
}
|
|
@@ -10,6 +10,6 @@ export const libJsFormatterByte = (bytes) => {
|
|
|
10
10
|
const k = 1024;
|
|
11
11
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
12
12
|
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
|
|
13
|
-
const size = (bytes /
|
|
13
|
+
const size = (bytes / k ** i).toFixed(2);
|
|
14
14
|
return [size, sizes[i], `${size} ${sizes[i]}`];
|
|
15
15
|
};
|
|
@@ -3,19 +3,21 @@ import { libJsDecimal } from "../Math/LibJsDecimal";
|
|
|
3
3
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsClassObservable-类属性监听器
|
|
4
4
|
*/
|
|
5
5
|
export class LibJsClassObservable {
|
|
6
|
+
/** 可监听的对象 */
|
|
7
|
+
data;
|
|
8
|
+
/** 监听器 */
|
|
9
|
+
listeners = new Map();
|
|
10
|
+
/** 全局监听器(监听所有键的更改) */
|
|
11
|
+
globalListeners = new Map();
|
|
12
|
+
/** 按id映射监听索引 */
|
|
13
|
+
listenerIds = new Map();
|
|
14
|
+
/** 监听函数索引ID */
|
|
15
|
+
index = 0;
|
|
6
16
|
/**
|
|
7
17
|
* @param initialData 监听的数据
|
|
8
18
|
*/
|
|
9
19
|
constructor(initialData) {
|
|
10
|
-
|
|
11
|
-
this.listeners = new Map();
|
|
12
|
-
/** 全局监听器(监听所有键的更改) */
|
|
13
|
-
this.globalListeners = new Map();
|
|
14
|
-
/** 按id映射监听索引 */
|
|
15
|
-
this.listenerIds = new Map();
|
|
16
|
-
/** 监听函数索引ID */
|
|
17
|
-
this.index = 0;
|
|
18
|
-
this.data = Object.assign({}, initialData);
|
|
20
|
+
this.data = { ...initialData };
|
|
19
21
|
}
|
|
20
22
|
/** @description 获取所有数据 */
|
|
21
23
|
getData() {
|
|
@@ -35,13 +37,12 @@ export class LibJsClassObservable {
|
|
|
35
37
|
* @returns 设置后的值
|
|
36
38
|
*/
|
|
37
39
|
setValue(key, value, immediately = true) {
|
|
38
|
-
var _a;
|
|
39
40
|
const oldValue = this.data[key];
|
|
40
41
|
if (oldValue === value)
|
|
41
42
|
return value;
|
|
42
43
|
this.data[key] = value;
|
|
43
44
|
if (immediately) {
|
|
44
|
-
|
|
45
|
+
this.listeners.get(key)?.forEach((fn) => fn(value, oldValue));
|
|
45
46
|
this.globalListeners.forEach((fn) => fn(key, value, oldValue));
|
|
46
47
|
}
|
|
47
48
|
return value;
|
|
@@ -95,9 +96,8 @@ export class LibJsClassObservable {
|
|
|
95
96
|
* @returns 触发的键值
|
|
96
97
|
*/
|
|
97
98
|
updateFake(key) {
|
|
98
|
-
var _a;
|
|
99
99
|
const value = this.data[key];
|
|
100
|
-
|
|
100
|
+
this.listeners.get(key)?.forEach((fn) => fn(value, value));
|
|
101
101
|
return value;
|
|
102
102
|
}
|
|
103
103
|
/** @description 数字类型的键值累加或累减
|
package/Misc/LibJsEmitter.js
CHANGED
|
@@ -10,8 +10,7 @@ export const LibJsEmitter = () => {
|
|
|
10
10
|
_eventMap.get(event).push(listener);
|
|
11
11
|
};
|
|
12
12
|
const emit = (event, ...args) => {
|
|
13
|
-
|
|
14
|
-
(_a = _eventMap.get(event)) === null || _a === void 0 ? void 0 : _a.forEach((listener) => listener(...args));
|
|
13
|
+
_eventMap.get(event)?.forEach((listener) => listener(...args));
|
|
15
14
|
};
|
|
16
15
|
const off = (event, listener) => {
|
|
17
16
|
if (!listener) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** @description 关闭监听 */
|
|
2
2
|
export class LibJsEmitterClose {
|
|
3
|
+
static listeners = new Map();
|
|
3
4
|
static on(eventName, listener) {
|
|
4
5
|
if (!this.listeners.has(eventName))
|
|
5
6
|
this.listeners.set(eventName, new Set());
|
|
@@ -14,4 +15,3 @@ export class LibJsEmitterClose {
|
|
|
14
15
|
this.listeners.delete(eventName);
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
LibJsEmitterClose.listeners = new Map();
|
|
@@ -2,13 +2,19 @@
|
|
|
2
2
|
* @link 使用方法:https://www.npmjs.com/package/lyb-js#LibJsNumberStepper-数字步进器
|
|
3
3
|
*/
|
|
4
4
|
export class LibJsNumberStepper {
|
|
5
|
+
/** 当前按下状态 */
|
|
6
|
+
_isDown = false;
|
|
7
|
+
/** 定时器ID */
|
|
8
|
+
_timerId;
|
|
9
|
+
/** 计时器ID */
|
|
10
|
+
_intervalId;
|
|
11
|
+
/** 数字变动时触发 */
|
|
12
|
+
_onChange;
|
|
5
13
|
/**
|
|
6
14
|
* @param numsLength 数字长度
|
|
7
15
|
* @param onChange 数字变动时触发
|
|
8
16
|
*/
|
|
9
17
|
constructor(onChange) {
|
|
10
|
-
/** 当前按下状态 */
|
|
11
|
-
this._isDown = false;
|
|
12
18
|
this._onChange = onChange;
|
|
13
19
|
window.addEventListener("pointerup", () => {
|
|
14
20
|
this._isDown && this._up();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { libJsIsNull } from "../Base/LibJsIsNull";
|
|
2
|
+
/** @description 去掉对象内的空值 */
|
|
3
|
+
export const libJsPruneEmpty = (obj) => {
|
|
4
|
+
const result = Object.entries(obj).reduce((acc, [key, value]) => {
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
if (value.length)
|
|
7
|
+
acc[key] = value;
|
|
8
|
+
return acc;
|
|
9
|
+
}
|
|
10
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
11
|
+
const next = libJsPruneEmpty(value);
|
|
12
|
+
if (next)
|
|
13
|
+
acc[key] = next;
|
|
14
|
+
return acc;
|
|
15
|
+
}
|
|
16
|
+
if (!libJsIsNull(value)) {
|
|
17
|
+
acc[key] = value;
|
|
18
|
+
}
|
|
19
|
+
return acc;
|
|
20
|
+
}, {});
|
|
21
|
+
return Object.keys(result).length ? result : undefined;
|
|
22
|
+
};
|
package/README.md
CHANGED
|
@@ -53,6 +53,8 @@ conosle.log(v); //0.6
|
|
|
53
53
|
|
|
54
54
|
\- [LibJsResizeWatcher-窗口监听](#LibJsResizeWatcher-窗口监听)
|
|
55
55
|
|
|
56
|
+
\- [LibIsNull-是否为空值](#LibIsNull-是否为空值)
|
|
57
|
+
|
|
56
58
|
|
|
57
59
|
### Browser-浏览器
|
|
58
60
|
|
|
@@ -207,6 +209,10 @@ const off = libJsResizeWatcher.on((w,h)=>{})
|
|
|
207
209
|
off()
|
|
208
210
|
```
|
|
209
211
|
|
|
212
|
+
### LibIsNull-是否为空值
|
|
213
|
+
|
|
214
|
+
> 判断是否为空值
|
|
215
|
+
|
|
210
216
|
## Browser-浏览器
|
|
211
217
|
|
|
212
218
|
### LibJsColorConsole-有色打印
|
|
@@ -675,6 +681,10 @@ setTimeout(()=>{
|
|
|
675
681
|
|
|
676
682
|
> 需要传递当前游戏的适配模式
|
|
677
683
|
|
|
684
|
+
### LibJsPruneEmpty-对象属性去空值
|
|
685
|
+
|
|
686
|
+
> 递归对象并去掉对象内的 `undefined`、`null`、`""`
|
|
687
|
+
|
|
678
688
|
## Random-随机
|
|
679
689
|
|
|
680
690
|
### LibJsProbabilityResult-概率触发
|