ph-utils 0.16.3 → 0.16.5

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/crypto.js CHANGED
@@ -193,7 +193,7 @@ export async function aesEncrypt(message, key, encode = "hex", iv = null) {
193
193
  else if (typeof iv === "string") {
194
194
  iv = hexToBuffer(iv);
195
195
  }
196
- const encodeData = await encrypt({ ...algorithm, iv }, cryptoKey, message, encode);
196
+ const encodeData = await encrypt({ ...algorithm, iv: iv }, cryptoKey, message, encode);
197
197
  ciphertext = encodeData;
198
198
  resIv = bufferToHex(iv);
199
199
  return { ciphertext, iv: resIv, key };
package/lib/date.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 将日期格式化为指定形式的字符串
3
3
  * @param date 日期
4
- * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
4
+ * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时(24时制), MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
5
5
  */
6
6
  export declare function format(date?: Date | string | number | null, pattern?: string): string;
7
7
  /**
@@ -13,24 +13,24 @@ export declare function parse(date?: Date | string | number | null): Date;
13
13
  /**
14
14
  * 设置日期的开始或者结束的点
15
15
  * @param date 日期,能够被 parse 解析的日期
16
- * @param unit 单位,Date|date, 默认为 Date
16
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
17
17
  * @param isEnd true则为 endOf
18
18
  */
19
- export declare function dateOf(date?: Date | string | number, unit?: string, isEnd?: boolean): Date;
19
+ export declare function dateOf(date?: Date | string | number | null, unit?: string, isEnd?: boolean): Date;
20
20
  /**
21
21
  * 设置日期的开始的点
22
22
  * @param date 日期,能够被 parse 解析的日期
23
- * @param unit 单位,Date|date, 默认为 Date
23
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
24
24
  * @returns
25
25
  */
26
- export declare function startOf(date?: Date | string | number, unit?: string): Date;
26
+ export declare function startOf(date?: Date | string | number | null, unit?: string): Date;
27
27
  /**
28
28
  * 设置日期的结束点
29
29
  * @param date 日期,能够被 parse 解析的日期
30
- * @param unit 单位,Date|date, 默认为 Date
30
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
31
31
  * @returns
32
32
  */
33
- export declare function endOf(date?: Date | string | number, unit?: string): Date;
33
+ export declare function endOf(date?: Date | string | number | null, unit?: string): Date;
34
34
  /**
35
35
  * 获取时间戳
36
36
  * @param ctime 时间
package/lib/date.js CHANGED
@@ -72,7 +72,7 @@ function getLastDayOfMonth(date, month) {
72
72
  /**
73
73
  * 将日期格式化为指定形式的字符串
74
74
  * @param date 日期
75
- * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
75
+ * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时(24时制), MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
76
76
  */
77
77
  export function format(date, pattern = "yyyy-mm-dd HH:MM:ss") {
78
78
  // eslint-disable-next-line
@@ -130,7 +130,7 @@ export function parse(date) {
130
130
  /**
131
131
  * 设置日期的开始或者结束的点
132
132
  * @param date 日期,能够被 parse 解析的日期
133
- * @param unit 单位,Date|date, 默认为 Date
133
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
134
134
  * @param isEnd true则为 endOf
135
135
  */
136
136
  export function dateOf(date, unit, isEnd = false) {
@@ -163,7 +163,7 @@ export function dateOf(date, unit, isEnd = false) {
163
163
  /**
164
164
  * 设置日期的开始的点
165
165
  * @param date 日期,能够被 parse 解析的日期
166
- * @param unit 单位,Date|date, 默认为 Date
166
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
167
167
  * @returns
168
168
  */
169
169
  export function startOf(date, unit) {
@@ -172,7 +172,7 @@ export function startOf(date, unit) {
172
172
  /**
173
173
  * 设置日期的结束点
174
174
  * @param date 日期,能够被 parse 解析的日期
175
- * @param unit 单位,Date|date, 默认为 Date
175
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
176
176
  * @returns
177
177
  */
178
178
  export function endOf(date, unit) {
package/lib/dom.d.ts CHANGED
@@ -10,14 +10,14 @@
10
10
  type FormatStyleParam = (string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string;
11
11
  type FormatClassParam = (string | undefined | null | boolean)[] | Record<string, boolean | string | undefined | null | boolean> | string;
12
12
  type DocumentContext = HTMLElement | ShadowRoot | Document;
13
- export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
13
+ export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
14
14
  /**
15
15
  * 根据选择器获取 DOM 元素。
16
16
  * @param selector - 选择器字符串或 HTMLElement 实例。
17
17
  * @param dom - 可选参数,指定在哪个 DOM 节点下查找元素,默认为 document。
18
18
  * @returns 返回匹配到的 HTMLElement 实例。
19
19
  */
20
- export declare function $(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
20
+ export declare function $(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
21
21
  /**
22
22
  * 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
23
23
  * @param tag - 元素标签名或 HTML 字符串。
@@ -46,7 +46,7 @@ export declare function $$(tag: string, option?: {
46
46
  * 根据选择器获取匹配的第一个 DOM 元素。
47
47
  * @param selector - 选择器字符串或直接的 HTMLElement。
48
48
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
49
- * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined
49
+ * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null
50
50
  */
51
51
  export declare function $one(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement | null;
52
52
  /**
@@ -80,8 +80,8 @@ type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => vo
80
80
  * 为节点添加事件处理
81
81
  * @param {HTMLElement} element 添加事件的节点
82
82
  * @param {string} listener 事件名称
83
- * @param {function} event 事件处理函数
84
- * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
83
+ * @param {function} fn 事件处理函数
84
+ * @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
85
85
  */
86
86
  export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | (AddEventListenerOptions & {
87
87
  eventFlag?: string;
@@ -119,7 +119,7 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
119
119
  * @param textstr 可选,如果传递该参数,则表示设置;否则表示获取
120
120
  * @returns
121
121
  */
122
- export declare function text(element: HTMLElement, textstr?: string): string | null | undefined;
122
+ export declare function text(element: HTMLElement, textstr?: string): string | undefined;
123
123
  export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
124
124
  export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>, fn: (el: HTMLElement, index: number) => any): void;
125
125
  /**
package/lib/dom.js CHANGED
@@ -75,7 +75,7 @@ export function $$(tag, option = {}, ctx) {
75
75
  * 根据选择器获取匹配的第一个 DOM 元素。
76
76
  * @param selector - 选择器字符串或直接的 HTMLElement。
77
77
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
78
- * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined
78
+ * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null
79
79
  */
80
80
  export function $one(selector, dom) {
81
81
  if (typeof selector === "string") {
@@ -126,8 +126,8 @@ export function toggleClass(el, clazz) {
126
126
  * 为节点添加事件处理
127
127
  * @param {HTMLElement} element 添加事件的节点
128
128
  * @param {string} listener 事件名称
129
- * @param {function} event 事件处理函数
130
- * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
129
+ * @param {function} fn 事件处理函数
130
+ * @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
131
131
  */
132
132
  export function on(element, listener, fn, option) {
133
133
  if (element.length != null) {
@@ -527,7 +527,7 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
527
527
  });
528
528
  }
529
529
  }
530
- el.addEventListener("transitionend", (_e) => {
530
+ el.addEventListener("transitionend", () => {
531
531
  if (status === 0) {
532
532
  status = 1;
533
533
  if (nameClass) {
package/lib/file.js CHANGED
@@ -41,7 +41,7 @@ export async function write(file, data, opts) {
41
41
  if (opts.json === true && typeof data === "object") {
42
42
  writeData = JSON.stringify(data, null, opts.format === true ? 2 : 0);
43
43
  }
44
- return await fs.writeFile(path.resolve(file), writeData);
44
+ await fs.writeFile(path.resolve(file), writeData);
45
45
  }
46
46
  /**
47
47
  * 根据文件的 stat 获取文件的 etag
package/lib/id.d.ts CHANGED
@@ -1,3 +1,12 @@
1
+ type SnowflakeIDInfo = {
2
+ value: string;
3
+ timeOffset: bigint;
4
+ timestamp: number;
5
+ machineId: bigint;
6
+ sequence: bigint;
7
+ epoch: number;
8
+ version: string | undefined;
9
+ };
1
10
  /** 雪花ID, 推荐在全局构造一个对象用于生成id */
2
11
  export declare class SnowflakeID {
3
12
  /** 机器码, 默认为: 1 */
@@ -24,15 +33,7 @@ export declare class SnowflakeID {
24
33
  * @throws 如果时钟回退,抛出错误
25
34
  */
26
35
  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
+ parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): SnowflakeIDInfo;
36
37
  }
37
38
  /** 将uuid转换为更简单的唯一标记id */
38
39
  export declare class ShortUUID {
@@ -64,3 +65,4 @@ export declare class ShortUUID {
64
65
  private _uuidHexToInt;
65
66
  private _uuidIntToHex;
66
67
  }
68
+ export {};
package/lib/id.js CHANGED
@@ -76,7 +76,7 @@ export class SnowflakeID {
76
76
  machineId: machineId,
77
77
  sequence: sequence,
78
78
  epoch: epochTime,
79
- version,
79
+ version: version,
80
80
  };
81
81
  }
82
82
  }
package/lib/logger.js CHANGED
@@ -1,4 +1,4 @@
1
- import { format } from "./date";
1
+ import { format } from "./date.js";
2
2
  /**
3
3
  * 日志记录器
4
4
  */
package/lib/server.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SpawnOptions } from "node:child_process";
1
+ import type { SpawnOptionsWithoutStdio } from "node:child_process";
2
2
  /**
3
3
  * 执行命令
4
4
  * @param command 待执行的命令
@@ -16,3 +16,18 @@ export declare function exec(command: string, args?: string[], options?: SpawnOp
16
16
  stdout: string;
17
17
  stderr: string;
18
18
  }>;
19
+ type SpawnOptions = SpawnOptionsWithoutStdio & {
20
+ shell?: 'powershell';
21
+ };
22
+ /**
23
+ * 执行命令并返回执行结果的Promise
24
+ * @param command 要执行的命令
25
+ * @param args 命令参数数组
26
+ * @param options 执行选项,支持指定shell类型
27
+ * @returns Promise对象,成功时resolve包含stdout和stderr的对象,失败时reject包含错误信息
28
+ */
29
+ export declare function spawn(command: string, args?: string[], options?: SpawnOptions): Promise<{
30
+ stdout: string;
31
+ stderr: string;
32
+ }>;
33
+ export {};
package/lib/server.js CHANGED
@@ -1,4 +1,4 @@
1
- import { execFile } from "node:child_process";
1
+ import { execFile, spawn as spawnOri } from "node:child_process";
2
2
  import { promisify } from "node:util";
3
3
  const execFilePromise = promisify(execFile);
4
4
  /**
@@ -27,3 +27,39 @@ export function exec(command, ...params) {
27
27
  }
28
28
  return execFilePromise(cmd, argvs, opts);
29
29
  }
30
+ /**
31
+ * 执行命令并返回执行结果的Promise
32
+ * @param command 要执行的命令
33
+ * @param args 命令参数数组
34
+ * @param options 执行选项,支持指定shell类型
35
+ * @returns Promise对象,成功时resolve包含stdout和stderr的对象,失败时reject包含错误信息
36
+ */
37
+ export function spawn(command, args, options = {}) {
38
+ return new Promise((resolve, reject) => {
39
+ let execArgs = [];
40
+ let cmd;
41
+ // 根据是否指定powershell shell来设置实际执行的命令和参数
42
+ if (options.shell === 'powershell') {
43
+ cmd = 'powershell.exe';
44
+ execArgs = ['-NoProfile', '-Command', command, ...(args || [])];
45
+ }
46
+ else {
47
+ cmd = command;
48
+ execArgs = args || [];
49
+ }
50
+ delete options.shell;
51
+ const child = spawnOri(cmd, execArgs, options);
52
+ let stdout = '', stderr = '';
53
+ child.stdout.on('data', d => stdout += d);
54
+ child.stderr.on('data', d => stderr += d);
55
+ // 监听子进程关闭事件,根据退出码决定resolve或reject
56
+ child.on('close', code => {
57
+ if (code === 0)
58
+ resolve({ stdout, stderr });
59
+ else
60
+ reject(new Error(`spawn failed (${code}): ${stderr}`));
61
+ });
62
+ // 监听子进程错误事件,发生错误时直接reject
63
+ child.on('error', reject);
64
+ });
65
+ }
package/lib/theme.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  /** 获取当前系统的主题 */
2
- export declare function getSystemTheme(): "dark" | "light" | "auto";
2
+ export declare function getSystemTheme(): "light" | "dark" | "auto";
3
3
  /**
4
4
  * 初始化主题, 让网页能够适应系统主题, 同时根据缓存的主题切换主题
5
5
  * @returns 当前应用的主题
6
6
  */
7
- export declare function initTheme(): Promise<unknown>;
7
+ export declare function initTheme(): Promise<"light" | "dark" | "auto">;
8
8
  /**
9
9
  * 切换主题, 通常用于预览
10
10
  * @param theme 切换的主题
11
11
  * @param transition 是否使用过渡动画, 注意浏览器必须支持 document.startViewTransition, 默认: true
12
12
  * @returns 切换后的主题
13
13
  */
14
- export declare function toggleTheme(theme?: "light" | "dark" | "auto", transition?: boolean): Promise<unknown>;
14
+ export declare function toggleTheme(theme?: "light" | "dark" | "auto", transition?: boolean): Promise<"light" | "dark" | "auto">;
15
15
  /** 获取当前主题 */
16
16
  export declare function getTheme(): string;
17
17
  /**
@@ -21,7 +21,7 @@ export declare function getTheme(): string;
21
21
  * @param transition 是否使用过渡动画, 注意浏览器必须支持 document.startViewTransition, 默认: true
22
22
  * @returns 应用的主题
23
23
  */
24
- export declare function applyTheme(theme?: "light" | "dark" | "auto", cache?: boolean, transition?: boolean): Promise<unknown>;
24
+ export declare function applyTheme(theme?: "light" | "dark" | "auto", cache?: boolean, transition?: boolean): Promise<"light" | "dark" | "auto">;
25
25
  /** 获取当前主题色 */
26
26
  export declare function getColorTheme(defaultValue?: string): string | undefined;
27
27
  /**
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
@@ -1,102 +1,99 @@
1
- {
2
- "name": "ph-utils",
3
- "description": "js 开发工具集,前后端都可以使用(commonjs和es module)",
4
- "main": "lib/index.js",
5
- "module": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "browser": "lib/index.js",
8
- "exports": {
9
- ".": {
10
- "types": "./lib/index.d.ts",
11
- "import": "./lib/index.js"
12
- },
13
- "./file": {
14
- "types": "./lib/file.d.ts",
15
- "import": "./lib/file.js"
16
- },
17
- "./date": {
18
- "types": "./lib/date.d.ts",
19
- "import": "./lib/date.js"
20
- },
21
- "./server": {
22
- "types": "./lib/server.d.ts",
23
- "import": "./lib/server.js"
24
- },
25
- "./validator": {
26
- "types": "./lib/validator.d.ts",
27
- "import": "./lib/validator.js"
28
- },
29
- "./crypto": {
30
- "types": "./lib/crypto.d.ts",
31
- "import": "./lib/crypto.js"
32
- },
33
- "./crypto_node": {
34
- "types": "./lib/crypto_node.d.ts",
35
- "import": "./lib/crypto_node.js"
36
- },
37
- "./web": {
38
- "types": "./lib/web.d.ts",
39
- "import": "./lib/web.js"
40
- },
41
- "./dom": {
42
- "types": "./lib/dom.d.ts",
43
- "import": "./lib/dom.js"
44
- },
45
- "./copy": {
46
- "types": "./lib/copy.d.ts",
47
- "import": "./lib/copy.js"
48
- },
49
- "./storage": {
50
- "types": "./lib/storage.d.ts",
51
- "import": "./lib/storage.js"
52
- },
53
- "./color": {
54
- "types": "./lib/color.d.ts",
55
- "import": "./lib/color.js"
56
- },
57
- "./logger": {
58
- "types": "./lib/logger.d.ts",
59
- "import": "./lib/logger.js"
60
- },
61
- "./array": {
62
- "types": "./lib/array.d.ts",
63
- "import": "./lib/array.js"
64
- },
65
- "./theme": {
66
- "types": "./lib/theme.d.ts",
67
- "import": "./lib/theme.js"
68
- },
69
- "./*": "./lib/*"
70
- },
71
- "version": "0.16.3",
72
- "type": "module",
73
- "repository": {
74
- "type": "git",
75
- "url": "git+https//gitee.com/towardly/ph.git",
76
- "directory": "packages/utils"
77
- },
78
- "license": "MIT",
79
- "author": "Tenny <tenny.shu@foxmail.com>",
80
- "bugs": {
81
- "url": "https://gitee.com/towardly/ph/issues"
82
- },
83
- "homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
84
- "devDependencies": {
85
- "@types/node": "^22.8.2",
86
- "typescript": "^5.6.3"
87
- },
88
- "files": [
89
- "lib"
90
- ],
91
- "keywords": [
92
- "node",
93
- "utils",
94
- "javascript",
95
- "date",
96
- "dom",
97
- "file"
98
- ],
99
- "scripts": {
100
- "build": "node scripts/build.js"
101
- }
102
- }
1
+ {
2
+ "name": "ph-utils",
3
+ "description": "js 开发工具集,前后端都可以使用(commonjs和es module)",
4
+ "main": "lib/index.js",
5
+ "module": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "browser": "lib/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/index.js"
12
+ },
13
+ "./file": {
14
+ "types": "./lib/file.d.ts",
15
+ "import": "./lib/file.js"
16
+ },
17
+ "./date": {
18
+ "types": "./lib/date.d.ts",
19
+ "import": "./lib/date.js"
20
+ },
21
+ "./server": {
22
+ "types": "./lib/server.d.ts",
23
+ "import": "./lib/server.js"
24
+ },
25
+ "./validator": {
26
+ "types": "./lib/validator.d.ts",
27
+ "import": "./lib/validator.js"
28
+ },
29
+ "./crypto": {
30
+ "types": "./lib/crypto.d.ts",
31
+ "import": "./lib/crypto.js"
32
+ },
33
+ "./crypto_node": {
34
+ "types": "./lib/crypto_node.d.ts",
35
+ "import": "./lib/crypto_node.js"
36
+ },
37
+ "./web": {
38
+ "types": "./lib/web.d.ts",
39
+ "import": "./lib/web.js"
40
+ },
41
+ "./dom": {
42
+ "types": "./lib/dom.d.ts",
43
+ "import": "./lib/dom.js"
44
+ },
45
+ "./copy": {
46
+ "types": "./lib/copy.d.ts",
47
+ "import": "./lib/copy.js"
48
+ },
49
+ "./storage": {
50
+ "types": "./lib/storage.d.ts",
51
+ "import": "./lib/storage.js"
52
+ },
53
+ "./color": {
54
+ "types": "./lib/color.d.ts",
55
+ "import": "./lib/color.js"
56
+ },
57
+ "./logger": {
58
+ "types": "./lib/logger.d.ts",
59
+ "import": "./lib/logger.js"
60
+ },
61
+ "./array": {
62
+ "types": "./lib/array.d.ts",
63
+ "import": "./lib/array.js"
64
+ },
65
+ "./theme": {
66
+ "types": "./lib/theme.d.ts",
67
+ "import": "./lib/theme.js"
68
+ },
69
+ "./*": "./lib/*"
70
+ },
71
+ "version": "0.16.5",
72
+ "type": "module",
73
+ "repository": {
74
+ "type": "git",
75
+ "url": "git+https//gitee.com/towardly/ph.git",
76
+ "directory": "packages/utils"
77
+ },
78
+ "license": "MIT",
79
+ "author": "Tenny <tenny.shu@foxmail.com>",
80
+ "bugs": {
81
+ "url": "https://gitee.com/towardly/ph/issues"
82
+ },
83
+ "homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
84
+ "devDependencies": {
85
+ "@types/node": "^22.8.2",
86
+ "typescript": "^5.6.3"
87
+ },
88
+ "files": [
89
+ "lib"
90
+ ],
91
+ "keywords": [
92
+ "node",
93
+ "utils",
94
+ "javascript",
95
+ "date",
96
+ "dom",
97
+ "file"
98
+ ]
99
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Tenny
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,11 +0,0 @@
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>;
package/lib/clipboard.js DELETED
@@ -1,101 +0,0 @@
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
- }