ph-utils 0.16.2 → 0.16.3

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
- ## ph-utils
2
-
3
- 整理了 js 前后端开发(web + nodejs)时常用的一些工具;[详细文档](https://gitee.com/towardly/ph/wikis/Home?sort_id=4035190)
4
-
5
- ### 包含如下工具文件
6
-
7
- `index` 基础工具类、`date` 跟日期相关的工具类、`file` 文件操作相关工具类[**服务端**]、`server` 服务端工具类、`validator` 数据验证、`dom` 浏览器节点操作相关[**前端**]、`web` 一些只适用于前端相关的工具、`color` 颜色相关工具
1
+ ## ph-utils
2
+
3
+ 整理了 js 前后端开发(web + nodejs)时常用的一些工具;[详细文档](https://gitee.com/towardly/ph/wikis/Home?sort_id=4035190)
4
+
5
+ ### 包含如下工具文件
6
+
7
+ `index` 基础工具类、`date` 跟日期相关的工具类、`file` 文件操作相关工具类[**服务端**]、`server` 服务端工具类、`validator` 数据验证、`dom` 浏览器节点操作相关[**前端**]、`web` 一些只适用于前端相关的工具、`color` 颜色相关工具
package/lib/array.d.ts CHANGED
@@ -59,14 +59,14 @@ export declare function symmetricDifference<T>(...arrs: T[][]): T[];
59
59
  * @param a2
60
60
  * @returns
61
61
  */
62
- export declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): any;
62
+ export declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): boolean;
63
63
  /**
64
64
  * 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。
65
65
  * @param arr1
66
66
  * @param arr2
67
67
  * @returns
68
68
  */
69
- export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
69
+ export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): boolean;
70
70
  /**
71
71
  * 返回一个布尔值,指示此集合是否与给定集合没有公共元素。
72
72
  *
@@ -76,4 +76,4 @@ export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>):
76
76
  * @param arr2
77
77
  * @returns
78
78
  */
79
- export declare function isDisjointFrom<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
79
+ export declare function isDisjointFrom<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): boolean;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 复制数据, 可以从多种类型的数据
3
+ * 1. 直接复制文本: await copy("待复制的文本")
4
+ * 2. 复制节点上的 data-copy-text:
5
+ * <button data-copy-text="这是待复制的文本">复制</button>
6
+ * await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
7
+ * 3. 直接复制节点本身数据: await copy('#a')
8
+ * @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
9
+ * @returns {Promise<boolean>} 是否复制成功
10
+ */
11
+ export declare function copy(source: string | HTMLElement): Promise<boolean>;
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 创建一个临时节点缓存待复制数据
3
+ * @param {String} value - 待复制文本
4
+ * @return {HTMLElement}
5
+ */
6
+ function createFakeElement(value) {
7
+ const fakeElement = document.createElement("textarea");
8
+ fakeElement.style.border = "0";
9
+ fakeElement.style.padding = "0";
10
+ fakeElement.style.margin = "0";
11
+ fakeElement.style.position = "absolute";
12
+ fakeElement.style.left = "-9999px";
13
+ fakeElement.style.top = "-9999";
14
+ fakeElement.setAttribute("readonly", "");
15
+ fakeElement.value = value;
16
+ return fakeElement;
17
+ }
18
+ /** 通过执行 execCommand 来执行复制 */
19
+ function copyFromCommand(text) {
20
+ // 添加节点
21
+ const fakeEl = createFakeElement(text);
22
+ document.body.append(fakeEl);
23
+ fakeEl.focus();
24
+ fakeEl.select();
25
+ // 执行复制
26
+ const res = document.execCommand("copy");
27
+ fakeEl.remove(); // 删除节点
28
+ return Promise.resolve(res);
29
+ }
30
+ /** 使用 navigator.clipboard 复制 */
31
+ function copyFromClipboard(text) {
32
+ const theClipboard = navigator.clipboard;
33
+ if (theClipboard != null) {
34
+ return theClipboard
35
+ .writeText(text)
36
+ .then(() => {
37
+ Promise.resolve(true);
38
+ })
39
+ .catch(() => Promise.resolve(false));
40
+ }
41
+ return Promise.resolve(false);
42
+ }
43
+ /** 解析待复制的文本 */
44
+ function parseCopyText(source) {
45
+ let copyText = null; // 待复制文本
46
+ let sourceEl = null;
47
+ // 获取待复制数据
48
+ if (typeof source === "string") {
49
+ // 从节点拿数据
50
+ if (source.startsWith("#") || source.startsWith(".")) {
51
+ sourceEl = document.querySelector(source);
52
+ if (sourceEl == null) {
53
+ copyText = source;
54
+ }
55
+ }
56
+ else {
57
+ copyText = source;
58
+ }
59
+ }
60
+ if (source instanceof HTMLElement) {
61
+ sourceEl = source;
62
+ }
63
+ // 从节点获取待复制数据
64
+ if (sourceEl != null) {
65
+ if (sourceEl.hasAttribute("data-copy-text")) {
66
+ copyText = sourceEl.getAttribute("data-copy-text");
67
+ }
68
+ else {
69
+ const tagName = sourceEl.tagName;
70
+ if (tagName === "INPUT" || tagName === "TEXTAREA") {
71
+ copyText = sourceEl.value;
72
+ }
73
+ else {
74
+ copyText = sourceEl.textContent;
75
+ }
76
+ }
77
+ }
78
+ return copyText;
79
+ }
80
+ /**
81
+ * 复制数据, 可以从多种类型的数据
82
+ * 1. 直接复制文本: await copy("待复制的文本")
83
+ * 2. 复制节点上的 data-copy-text:
84
+ * <button data-copy-text="这是待复制的文本">复制</button>
85
+ * await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
86
+ * 3. 直接复制节点本身数据: await copy('#a')
87
+ * @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
88
+ * @returns {Promise<boolean>} 是否复制成功
89
+ */
90
+ export async function copy(source) {
91
+ // 待复制文本
92
+ const copyText = parseCopyText(source);
93
+ if (copyText == null) {
94
+ return Promise.resolve(false);
95
+ }
96
+ const v = await copyFromClipboard(copyText);
97
+ if (v === false) {
98
+ return copyFromCommand(copyText);
99
+ }
100
+ return Promise.resolve(true);
101
+ }
package/lib/crypto.d.ts CHANGED
@@ -12,7 +12,7 @@ export declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolea
12
12
  * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
13
13
  * @returns
14
14
  */
15
- export declare function sha(message: string, upper?: boolean, algorithm?: string): Promise<string>;
15
+ export declare function sha(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
16
16
  /**
17
17
  * 哈希算法
18
18
  * @param message 待进行 hash 的数据
@@ -20,7 +20,7 @@ export declare function sha(message: string, upper?: boolean, algorithm?: string
20
20
  * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
21
21
  * @returns
22
22
  */
23
- export declare function hash(message: string, upper?: boolean, algorithm?: string): Promise<string>;
23
+ export declare function hash(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
24
24
  type HMACAlgorithm = "SHA-256" | "SHA-512";
25
25
  /**
26
26
  * 使用 HMAC 算法计算消息的哈希值
package/lib/crypto.js CHANGED
@@ -59,8 +59,11 @@ function base64ToBuffer(data) {
59
59
  * @returns
60
60
  */
61
61
  export async function sha(message, upper = false, algorithm = "SHA-256") {
62
- const msgUint8 = new TextEncoder().encode(message);
63
- const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgUint8);
62
+ let msgBuffer = message;
63
+ if (typeof message === "string") {
64
+ msgBuffer = new TextEncoder().encode(message);
65
+ }
66
+ const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgBuffer);
64
67
  return bufferToHex(hashBuffer, upper);
65
68
  }
66
69
  /**
@@ -43,7 +43,7 @@ export declare function aesEncrypt(key: string, input: string, upper?: boolean):
43
43
  * @param iv 向量
44
44
  * @returns
45
45
  */
46
- export declare function aesDecrypt(input: string, key: string, iv: string): string;
46
+ export declare function aesDecrypt(input: string, key: string | Buffer, iv?: string | Buffer, algorithm?: string): string;
47
47
  /**
48
48
  * RSA 公钥加密
49
49
  * @param input 待加密字符串
@@ -73,6 +73,16 @@ export function aesEncrypt(key, input, upper = false) {
73
73
  iv.toString("hex"),
74
74
  ];
75
75
  }
76
+ function aesAlgorithm(key) {
77
+ if (key.startsWith("aes-"))
78
+ return key;
79
+ let prefix = "aes-";
80
+ // 如果 key 不是以数字开头,则加上数字,例如:128,256 等
81
+ if (!/^\d/.test(key)) {
82
+ prefix += "256-";
83
+ }
84
+ return `${prefix}${key}`;
85
+ }
76
86
  /**
77
87
  * AES 解密
78
88
  * @param input 加密后的数据
@@ -80,8 +90,19 @@ export function aesEncrypt(key, input, upper = false) {
80
90
  * @param iv 向量
81
91
  * @returns
82
92
  */
83
- export function aesDecrypt(input, key, iv) {
84
- const cipher = createDecipheriv("aes-256-cbc", Buffer.from(key, "hex"), Buffer.from(iv, "hex"));
93
+ export function aesDecrypt(input, key, iv, algorithm = "aes-256-cbc") {
94
+ let ivBuffer = null;
95
+ if (iv && !Buffer.isBuffer(iv)) {
96
+ ivBuffer = Buffer.from(iv, "hex");
97
+ }
98
+ let keyBuffer;
99
+ if (Buffer.isBuffer(key)) {
100
+ keyBuffer = key;
101
+ }
102
+ else {
103
+ keyBuffer = Buffer.from(key, "hex");
104
+ }
105
+ const cipher = createDecipheriv(aesAlgorithm(algorithm), keyBuffer, ivBuffer);
85
106
  let decryptedData = cipher.update(input, "hex", "utf-8");
86
107
  decryptedData += cipher.final("utf-8");
87
108
  return decryptedData;
package/lib/dom.d.ts CHANGED
@@ -75,6 +75,7 @@ export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
75
75
  * @param clazz - 要切换的类名。
76
76
  */
77
77
  export declare function toggleClass(el: HTMLElement, clazz: string): void;
78
+ type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
78
79
  /**
79
80
  * 为节点添加事件处理
80
81
  * @param {HTMLElement} element 添加事件的节点
@@ -82,7 +83,7 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
82
83
  * @param {function} event 事件处理函数
83
84
  * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
84
85
  */
85
- export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
86
+ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | (AddEventListenerOptions & {
86
87
  eventFlag?: string;
87
88
  })): void;
88
89
  /**
@@ -91,7 +92,7 @@ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCo
91
92
  * @param listener - 事件名称。
92
93
  * @param fn - 要移除的事件监听器函数。
93
94
  */
94
- export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
95
+ export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | EventListenerOptions): void;
95
96
  /**
96
97
  * 判断事件是否应该继续传递。
97
98
  * 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
package/lib/dom.js CHANGED
@@ -55,7 +55,7 @@ export function create(tag, option = {}, ctx) {
55
55
  if (value === true) {
56
56
  $el.setAttribute(key, "");
57
57
  }
58
- else if (typeof value === "string") {
58
+ else {
59
59
  $el.setAttribute(key, value);
60
60
  }
61
61
  }
package/lib/server.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { SpawnOptions } from "node:child_process";
3
2
  /**
4
3
  * 执行命令
package/lib/theme.js CHANGED
@@ -19,10 +19,10 @@ export async function initTheme() {
19
19
  if ($themeStyle == null) {
20
20
  $themeStyle = document.createElement("style");
21
21
  $themeStyle.id = "theme-style";
22
- $themeStyle.innerHTML = `
23
- :root{color-scheme:light dark;}
24
- html.light{color-scheme: light;}
25
- html.dark {color-scheme: dark;}
22
+ $themeStyle.innerHTML = `
23
+ :root{color-scheme:light dark;}
24
+ html.light{color-scheme: light;}
25
+ html.dark {color-scheme: dark;}
26
26
  `;
27
27
  document.head.appendChild($themeStyle);
28
28
  }
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.16.2",
71
+ "version": "0.16.3",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",