ph-utils 0.15.5 → 0.16.1

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/lib/color.d.ts CHANGED
@@ -9,6 +9,7 @@ type HSVColorObject = {
9
9
  s: number;
10
10
  v: number;
11
11
  };
12
+ type ColorType = string | RGBColorObject | HSVColorObject;
12
13
  /**
13
14
  * 将输入的颜色值转换为RGB对象格式。
14
15
  *
@@ -16,14 +17,14 @@ type HSVColorObject = {
16
17
  * @returns 返回一个包含r、g、b和a(透明度)属性的RGB对象。
17
18
  * @throws 如果输入的字符串不是有效的颜色表示,则抛出错误。
18
19
  */
19
- export declare function toRgb(color: any): RGBColorObject;
20
+ export declare function toRgb(color: ColorType): RGBColorObject;
20
21
  /**
21
22
  * 将颜色转换为HSV颜色模型。
22
23
  *
23
24
  * @param color - 字符串或者RGB对象
24
25
  * @returns 返回一个包含h、s、v属性的对象,代表HSV颜色值,其中h是色相(取值范围0到360),s是饱和度(取值范围0到1),v是明度(取值范围0到1)。
25
26
  */
26
- export declare function toHsv(color: any): HSVColorObject;
27
+ export declare function toHsv(color: ColorType): HSVColorObject;
27
28
  /**
28
29
  * 将RGB颜色对象转换为十六进制颜色字符串。
29
30
  * @param rgb - 包含红色(r), 绿色(g), 蓝色(b)成分的对象。
@@ -35,7 +36,7 @@ export declare function rgbToHex(rgb: RGBColorObject): string;
35
36
  * @param color - 颜色, 可以 rgb(0,0,0),rgba(0,0,0,0)字符串, 也可以是 rgb、hsv对象
36
37
  * @returns 返回颜色的十六进制字符串,例如"#FF0000"
37
38
  */
38
- export declare function toHex(color: any): string;
39
+ export declare function toHex(color: ColorType): string;
39
40
  /**
40
41
  * 调整给定颜色深[darken]浅[lighten]
41
42
  * @param color - 输入的颜色,可以是任意颜色表示方式
@@ -50,5 +51,5 @@ export declare function toHex(color: any): string;
50
51
  *
51
52
  * @returns 返回调整后颜色的十六进制字符串表示。
52
53
  */
53
- export declare function adjust(color: any, level?: number, light?: boolean): string;
54
+ export declare function adjust(color: ColorType, level?: number, light?: boolean): string;
54
55
  export {};
package/lib/color.js CHANGED
@@ -265,7 +265,6 @@ export function toHex(color) {
265
265
  if (hexRegex.test(color)) {
266
266
  return color;
267
267
  }
268
- return rgbToHex(toRgb(color));
269
268
  }
270
269
  return rgbToHex(toRgb(color));
271
270
  }
package/lib/dom.d.ts CHANGED
@@ -120,7 +120,7 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
120
120
  */
121
121
  export declare function text(element: HTMLElement, textstr?: string): string | null | undefined;
122
122
  export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
123
- export declare function iterate(elems: NodeList | HTMLCollection, fn: (el: HTMLElement, index: number) => any): void;
123
+ export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>, fn: (el: HTMLElement, index: number) => any): void;
124
124
  /**
125
125
  * 设置或获取节点 data-* 属性
126
126
  * @param elem
package/lib/dom.js CHANGED
@@ -296,9 +296,9 @@ export function queryHideNodeSize(hideNode, parent = document.body) {
296
296
  const originalVisibility = hideNode.style.visibility;
297
297
  const originalPosition = hideNode.style.position;
298
298
  // 设置为可见但不可见状态,不影响布局
299
- hideNode.style.display = "block";
300
- hideNode.style.visibility = "hidden";
301
299
  hideNode.style.position = "absolute";
300
+ hideNode.style.visibility = "hidden";
301
+ hideNode.style.display = "block";
302
302
  // 读取高度
303
303
  const rect = hideNode.getBoundingClientRect();
304
304
  // 恢复原样式
package/lib/id.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ /** 雪花ID, 推荐在全局构造一个对象用于生成id */
2
+ export declare class SnowflakeID {
3
+ /** 机器码, 默认为: 1 */
4
+ machineId: bigint;
5
+ /** 起始时间戳, 默认为:1288834974657 */
6
+ epoch: bigint;
7
+ _lastTimestamp: bigint;
8
+ /** 版本号, 默认为: 0 */
9
+ version: number;
10
+ private _sequence;
11
+ /**
12
+ * 构造函数
13
+ *
14
+ * @param machineId 机器标识,默认为1
15
+ * @param epoch 时间戳起始值,默认为1288834974657
16
+ */
17
+ constructor(machineId?: number, epoch?: number);
18
+ private _getTimestamp;
19
+ private _waitNextMillis;
20
+ /**
21
+ * 生成雪花ID
22
+ *
23
+ * @returns 返回生成的唯一ID字符串
24
+ * @throws 如果时钟回退,抛出错误
25
+ */
26
+ generate(): string;
27
+ parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): {
28
+ value: string;
29
+ timeOffset: bigint;
30
+ timestamp: number;
31
+ machineId: bigint;
32
+ sequence: bigint;
33
+ epoch: number;
34
+ version: string | undefined;
35
+ };
36
+ }
37
+ /** 将uuid转换为更简单的唯一标记id */
38
+ export declare class ShortUUID {
39
+ private alphabet;
40
+ /**
41
+ * 构造函数,用于初始化字母表
42
+ * @param {string} [alphabet] - 可选参数,用于指定自定义字母表
43
+ * 如果提供了alphabet参数,则将其设置为实例的字母表属性
44
+ * 如果未提供alphabet参数,则使用默认值
45
+ */
46
+ constructor(alphabet?: string);
47
+ /**
48
+ * 将UUID字符串进行编码处理
49
+ * @param uuid - 需要编码的UUID字符串
50
+ * @returns 编码后的字符串
51
+ */
52
+ encode(uuid: string, alphabet?: string): string;
53
+ /**
54
+ * 解码短UUID字符串,将其转换为UUID整数和十六进制格式
55
+ * @param shortUUID - 需要解码的短UUID字符串
56
+ * @returns 返回包含UUID整数和十六进制格式的对象
57
+ */
58
+ decode(shortUUID: string, alphabet?: string): {
59
+ uuidInt: bigint;
60
+ uuid: string;
61
+ };
62
+ private _stringToInt;
63
+ private _intToString;
64
+ private _uuidHexToInt;
65
+ private _uuidIntToHex;
66
+ }
package/lib/id.js ADDED
@@ -0,0 +1,170 @@
1
+ /** 雪花ID, 推荐在全局构造一个对象用于生成id */
2
+ export class SnowflakeID {
3
+ /**
4
+ * 构造函数
5
+ *
6
+ * @param machineId 机器标识,默认为1
7
+ * @param epoch 时间戳起始值,默认为1288834974657
8
+ */
9
+ constructor(machineId = 1, epoch = 1288834974657) {
10
+ /** 版本号, 默认为: 0 */
11
+ this.version = 0;
12
+ this.machineId = BigInt(machineId);
13
+ this.epoch = BigInt(epoch);
14
+ this._lastTimestamp = BigInt(0);
15
+ this._sequence = BigInt(0);
16
+ }
17
+ _getTimestamp() {
18
+ return BigInt(Date.now());
19
+ }
20
+ _waitNextMillis(cTimestamp) {
21
+ let timestamp = cTimestamp;
22
+ while (timestamp <= this._lastTimestamp) {
23
+ timestamp = BigInt(Date.now());
24
+ }
25
+ return timestamp;
26
+ }
27
+ /**
28
+ * 生成雪花ID
29
+ *
30
+ * @returns 返回生成的唯一ID字符串
31
+ * @throws 如果时钟回退,抛出错误
32
+ */
33
+ generate() {
34
+ let cTimestamp = this._getTimestamp();
35
+ if (cTimestamp < this._lastTimestamp) {
36
+ throw new Error("Clock moved backwards");
37
+ }
38
+ if (cTimestamp === this._lastTimestamp) {
39
+ // TODO: increment sequence
40
+ // 雪花id,末尾: 12位序列号(二进制12个1等于16进制4095),最多4096个(从0到4095)
41
+ this._sequence = (this._sequence + 1n) & BigInt("0xFFF");
42
+ if (this._sequence === 0n) {
43
+ // 同一时间戳,序列号溢出,等待下一毫秒
44
+ cTimestamp = this._waitNextMillis(cTimestamp);
45
+ }
46
+ }
47
+ else {
48
+ this._sequence = BigInt(0);
49
+ }
50
+ this._lastTimestamp = cTimestamp;
51
+ const timeDiff = cTimestamp - this.epoch;
52
+ let id = (timeDiff << BigInt(22)) |
53
+ (this.machineId << BigInt(12)) |
54
+ this._sequence;
55
+ let idstr = id.toString().padStart(19, "0");
56
+ return `${this.version}${idstr}`;
57
+ }
58
+ parse(snowflakeID, epoch, includeVersion = true) {
59
+ let version = undefined;
60
+ if (includeVersion) {
61
+ snowflakeID = snowflakeID.substring(1);
62
+ version = snowflakeID.substring(0, 1);
63
+ }
64
+ let epochTime = epoch || Number(this.epoch);
65
+ const id = BigInt(snowflakeID);
66
+ // 1FFFFFFFFFF 就是二进制的41个1, 雪花id前面41为为时间戳,间隔部分
67
+ let timeDiff = (id >> BigInt(22)) & BigInt("0x1FFFFFFFFFF");
68
+ const flowTime = Number(timeDiff) + epochTime;
69
+ // 雪花id中间10位为机器标识, 0x3FF(二进制10个1) 进行位运算
70
+ const machineId = (id >> BigInt(12)) & BigInt("0x3FF");
71
+ const sequence = id & BigInt("0xFFF");
72
+ return {
73
+ value: snowflakeID,
74
+ timeOffset: timeDiff,
75
+ timestamp: flowTime,
76
+ machineId: machineId,
77
+ sequence: sequence,
78
+ epoch: epochTime,
79
+ version,
80
+ };
81
+ }
82
+ }
83
+ /** 将uuid转换为更简单的唯一标记id */
84
+ export class ShortUUID {
85
+ /**
86
+ * 构造函数,用于初始化字母表
87
+ * @param {string} [alphabet] - 可选参数,用于指定自定义字母表
88
+ * 如果提供了alphabet参数,则将其设置为实例的字母表属性
89
+ * 如果未提供alphabet参数,则使用默认值
90
+ */
91
+ constructor(alphabet) {
92
+ this.alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
93
+ if (alphabet) {
94
+ this.alphabet = alphabet;
95
+ }
96
+ }
97
+ /**
98
+ * 将UUID字符串进行编码处理
99
+ * @param uuid - 需要编码的UUID字符串
100
+ * @returns 编码后的字符串
101
+ */
102
+ encode(uuid, alphabet) {
103
+ // 将UUID字符串转换为整数
104
+ const uuidInt = this._uuidHexToInt(uuid);
105
+ // 将整数转换为特定格式的字符串
106
+ return this._intToString(uuidInt, alphabet);
107
+ }
108
+ /**
109
+ * 解码短UUID字符串,将其转换为UUID整数和十六进制格式
110
+ * @param shortUUID - 需要解码的短UUID字符串
111
+ * @returns 返回包含UUID整数和十六进制格式的对象
112
+ */
113
+ decode(shortUUID, alphabet) {
114
+ const uuidInt = this._stringToInt(shortUUID, alphabet);
115
+ return {
116
+ uuidInt: uuidInt,
117
+ uuid: this._uuidIntToHex(uuidInt),
118
+ };
119
+ }
120
+ _stringToInt(str, alphabet) {
121
+ if (!alphabet)
122
+ alphabet = this.alphabet;
123
+ const alphabetlen = BigInt(alphabet.length);
124
+ let result = BigInt(0);
125
+ let multiplier = BigInt(1);
126
+ const strlen = str.length;
127
+ for (let i = strlen - 1; i >= 0; i--) {
128
+ const char = str[i];
129
+ const index = alphabet.indexOf(char);
130
+ if (index === -1) {
131
+ throw new Error(`Character "${char}" not found in alphabet`);
132
+ }
133
+ result += BigInt(index) * multiplier;
134
+ multiplier *= alphabetlen;
135
+ }
136
+ return result;
137
+ }
138
+ _intToString(uuidInt, alphabet) {
139
+ if (!alphabet)
140
+ alphabet = this.alphabet;
141
+ const alphabetlen = BigInt(alphabet.length);
142
+ let num = uuidInt;
143
+ const result = [];
144
+ // uuidInt = d₀ × 1(base的0次方) + d₁ × base + d₂ × base²...
145
+ while (num) {
146
+ const index = Number(num % alphabetlen);
147
+ num = num / alphabetlen;
148
+ result.push(alphabet.charAt(index));
149
+ }
150
+ // 倒序,保证高位在前
151
+ return result.reverse().join("");
152
+ }
153
+ _uuidHexToInt(uuid) {
154
+ const uuidHex = uuid.replaceAll("-", "");
155
+ return BigInt(`0x${uuidHex}`);
156
+ }
157
+ _uuidIntToHex(uuidInt) {
158
+ const uuidHexStr = uuidInt.toString(16);
159
+ if (!/^[0-9a-fA-F]{32}$/.test(uuidHexStr)) {
160
+ throw new Error("Invalid UUID string (must be 32 hex characters)");
161
+ }
162
+ return [
163
+ uuidHexStr.slice(0, 8),
164
+ uuidHexStr.slice(8, 12),
165
+ uuidHexStr.slice(12, 16),
166
+ uuidHexStr.slice(16, 20),
167
+ uuidHexStr.slice(20),
168
+ ].join("-");
169
+ }
170
+ }
package/lib/web.d.ts CHANGED
@@ -24,4 +24,7 @@ export declare function throttle<R extends any[], T>(fn: (...args: R) => T, wait
24
24
  * @param interval 间隔时间段
25
25
  * @returns
26
26
  */
27
- export declare function debounce<R extends any[], T>(fn: (...args: R) => T, interval?: number): (...args: R) => void;
27
+ export declare function debounce<R extends any[], T>(fn: (...args: R) => T, interval?: number): {
28
+ (...args: R): void;
29
+ cancel(): void;
30
+ };
package/lib/web.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * web(浏览器) 端工具类
3
3
  */
4
- import { isBlank } from './index.js';
4
+ import { isBlank } from "./index.js";
5
5
  /**
6
6
  * 解析 Form 表单中的 input 元素的数据为 JSON 格式,key: input-name;value: input-value
7
7
  * @param form {object} Form 节点对象
@@ -12,16 +12,17 @@ export const formJson = function (form) {
12
12
  for (let i = 0, len = elems.length; i < len; i++) {
13
13
  let item = elems[i];
14
14
  if (!isBlank(item.name)) {
15
- if ((item.tagName === 'INPUT' || item.tagName === 'TEXTAREA') && !isBlank(item.value)) {
16
- let dataType = item.getAttribute('data-type');
17
- if (dataType === 'number') {
15
+ if ((item.tagName === "INPUT" || item.tagName === "TEXTAREA") &&
16
+ !isBlank(item.value)) {
17
+ let dataType = item.getAttribute("data-type");
18
+ if (dataType === "number") {
18
19
  value[item.name] = Number(item.value);
19
20
  }
20
21
  else {
21
22
  value[item.name] = item.value;
22
23
  }
23
24
  }
24
- else if (item.tagName === 'SELECT') {
25
+ else if (item.tagName === "SELECT") {
25
26
  value[item.name] = item.options[item.selectedIndex].value;
26
27
  }
27
28
  }
@@ -80,11 +81,19 @@ export function throttle(fn, wait = 500) {
80
81
  * @returns
81
82
  */
82
83
  export function debounce(fn, interval = 500) {
83
- let _t = -1;
84
- return (...args) => {
85
- clearTimeout(_t);
84
+ let _t;
85
+ const handle = (...args) => {
86
+ if (_t)
87
+ clearTimeout(_t);
86
88
  _t = setTimeout(() => {
87
89
  fn(...args);
88
90
  }, interval);
89
91
  };
92
+ handle.cancel = function () {
93
+ if (_t) {
94
+ clearTimeout(_t);
95
+ _t = null;
96
+ }
97
+ };
98
+ return handle;
90
99
  }
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.15.5",
71
+ "version": "0.16.1",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",