lyb-js 1.6.29 → 1.6.31

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.
@@ -0,0 +1,2 @@
1
+ /** @description 判断是否为空值 */
2
+ export declare const libIsNull: (value: any) => boolean;
@@ -0,0 +1,2 @@
1
+ /** @description 判断是否为空值 */
2
+ export const libIsNull = (value) => value === null || value === undefined || value === "";
@@ -26,7 +26,7 @@ export const libJsPromiseTimeout = (delay = 1, fn) => {
26
26
  };
27
27
  const startTimeout = (resolve) => {
28
28
  timeoutId = setTimeout(() => {
29
- fn === null || fn === void 0 ? void 0 : 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
  }
@@ -11,7 +11,7 @@ export const libJsDeepJSONParse = (data) => {
11
11
  try {
12
12
  return libJsDeepJSONParse(JSON.parse(trimmed));
13
13
  }
14
- catch (_a) {
14
+ catch {
15
15
  return data; // 如果解析失败,返回原始字符串
16
16
  }
17
17
  }
@@ -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 / Math.pow(k, i)).toFixed(2);
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
- (_a = this.listeners.get(key)) === null || _a === void 0 ? void 0 : _a.forEach((fn) => fn(value, oldValue));
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
- (_a = this.listeners.get(key)) === null || _a === void 0 ? void 0 : _a.forEach((fn) => fn(value, value));
100
+ this.listeners.get(key)?.forEach((fn) => fn(value, value));
101
101
  return value;
102
102
  }
103
103
  /** @description 数字类型的键值累加或累减
@@ -10,8 +10,7 @@ export const LibJsEmitter = () => {
10
10
  _eventMap.get(event).push(listener);
11
11
  };
12
12
  const emit = (event, ...args) => {
13
- var _a;
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,2 @@
1
+ /** @description 去掉对象内的空值 */
2
+ export declare const libJsPruneEmpty: (obj: Record<string, any>) => Record<string, any> | undefined;
@@ -0,0 +1,22 @@
1
+ import { libIsNull } from "../Base/LibIsNull";
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 (!libIsNull(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-概率触发
@@ -1,4 +1,5 @@
1
- export declare const libJsCountdown: (endTime: number) => {
1
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
2
+ export declare const libJsCountdown: (endTime: number, keepUnit: "year" | "month" | "day" | "hour" | "minute" | "second") => {
2
3
  years: string;
3
4
  months: string;
4
5
  days: string;
@@ -1,18 +1,49 @@
1
1
  import dayjs from "dayjs";
2
2
  import duration from "dayjs/plugin/duration";
3
3
  dayjs.extend(duration);
4
- export const libJsCountdown = (endTime) => {
4
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
5
+ export const libJsCountdown = (endTime, keepUnit) => {
5
6
  const startTime = dayjs();
6
7
  const diff = dayjs(endTime).diff(startTime);
7
8
  const time = dayjs.duration(diff);
8
9
  const pad = (n) => n.toString().padStart(2, "0");
10
+ if (diff <= 0) {
11
+ return {
12
+ years: "00",
13
+ months: "00",
14
+ days: "00",
15
+ hours: "00",
16
+ minutes: "00",
17
+ seconds: "00",
18
+ ended: true,
19
+ };
20
+ }
21
+ // 先获取原始值
22
+ const years = time.years();
23
+ const months = time.months();
24
+ const days = time.days();
25
+ let hours = time.hours();
26
+ let minutes = time.minutes();
27
+ let seconds = time.seconds();
28
+ // 将整个时长打平成秒
29
+ const totalSec = time.asSeconds();
30
+ // 根据保留单位展开
31
+ if (keepUnit === "hour") {
32
+ hours = Math.floor(totalSec / 3600); // total hours
33
+ minutes = Math.floor((totalSec % 3600) / 60);
34
+ seconds = Math.floor(totalSec % 60);
35
+ }
36
+ else if (keepUnit === "minute") {
37
+ minutes = Math.floor(totalSec / 60); // total minutes
38
+ seconds = Math.floor(totalSec % 60);
39
+ }
9
40
  return {
10
- years: diff <= 0 ? "00" : pad(time.years()),
11
- months: diff <= 0 ? "00" : pad(time.months()),
12
- days: diff <= 0 ? "00" : pad(time.days()),
13
- hours: diff <= 0 ? "00" : pad(time.hours()),
14
- minutes: diff <= 0 ? "00" : pad(time.minutes()),
15
- seconds: diff <= 0 ? "00" : pad(time.seconds()),
16
- ended: diff <= 0,
41
+ years: pad(years),
42
+ months: pad(months),
43
+ days: pad(days),
44
+ hours: pad(hours),
45
+ minutes: pad(minutes),
46
+ seconds: pad(seconds),
47
+ ended: false,
17
48
  };
18
49
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.29",
3
+ "version": "1.6.31",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -37,4 +37,4 @@
37
37
  "decimal.js": "^10.4.3",
38
38
  "vite": "^4.5.5"
39
39
  }
40
- }
40
+ }