ph-utils 0.9.2 → 0.9.4
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 +7 -7
- package/lib/clipboard.d.ts +11 -0
- package/lib/clipboard.js +101 -0
- package/lib/index.d.ts +4 -4
- package/lib/index.js +6 -4
- package/lib/theme.js +1 -1
- package/lib/validator.d.ts +13 -0
- package/lib/validator.js +13 -0
- package/package.json +1 -1
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` 颜色相关工具
|
@@ -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>;
|
package/lib/clipboard.js
ADDED
@@ -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/index.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
*
|
3
|
-
* @param str
|
4
|
-
* @param ignoreWhitespace
|
2
|
+
* 验证参数是否为空
|
3
|
+
* @param str 待验证的参数
|
4
|
+
* @param ignoreWhitespace 如果是字符串是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
5
5
|
*/
|
6
|
-
export declare function isBlank(str?:
|
6
|
+
export declare function isBlank(str?: any, ignoreWhitespace?: boolean): boolean;
|
7
7
|
/**
|
8
8
|
* 屏蔽手机号,中间部分用 * 展示
|
9
9
|
* @param mobile 待屏蔽的手机号
|
package/lib/index.js
CHANGED
@@ -6,15 +6,17 @@ const RANDOM_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345
|
|
6
6
|
/** 只包含字母的随机数字符 */
|
7
7
|
const NUMBER_RANDOM_CHARTS = "0123456789";
|
8
8
|
/**
|
9
|
-
*
|
10
|
-
* @param str
|
11
|
-
* @param ignoreWhitespace
|
9
|
+
* 验证参数是否为空
|
10
|
+
* @param str 待验证的参数
|
11
|
+
* @param ignoreWhitespace 如果是字符串是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
12
12
|
*/
|
13
13
|
export function isBlank(str, ignoreWhitespace = true) {
|
14
14
|
if (str == null) {
|
15
15
|
return true;
|
16
16
|
}
|
17
|
-
return (ignoreWhitespace
|
17
|
+
return ((ignoreWhitespace && typeof str === "string"
|
18
|
+
? str.trim().length
|
19
|
+
: str.length) === 0);
|
18
20
|
}
|
19
21
|
/**
|
20
22
|
* 屏蔽手机号,中间部分用 * 展示
|
package/lib/theme.js
CHANGED
@@ -136,7 +136,7 @@ export async function toggleColorTheme(color) {
|
|
136
136
|
*/
|
137
137
|
export function applyColorTheme(color, cache = true) {
|
138
138
|
if (cache === true) {
|
139
|
-
localStorage.setItem("web-theme-color", color
|
139
|
+
localStorage.setItem("web-theme-color", color);
|
140
140
|
}
|
141
141
|
return toggleColorTheme(color);
|
142
142
|
}
|
package/lib/validator.d.ts
CHANGED
@@ -26,7 +26,20 @@ declare class Validator {
|
|
26
26
|
};
|
27
27
|
/**
|
28
28
|
* 构造数据验证转换器
|
29
|
+
*
|
30
|
+
* See {@link https://gitee.com/towardly/ph/wikis/utils/validator|Validator文档}.
|
31
|
+
*
|
29
32
|
* @param schemas 配置验证转换规则
|
33
|
+
*
|
34
|
+
* @example
|
35
|
+
*
|
36
|
+
* const validator = new Validator([
|
37
|
+
* { key: 'mobile', rules: ['required', 'mobile'] },
|
38
|
+
* { key: 'code': rules: /^\d{6}$/, message: '请输入正确的验证码' },
|
39
|
+
* { key: 'confirmPassword', rules: ['required', 'same:password'] }
|
40
|
+
* ])
|
41
|
+
* // 验证某一个字段
|
42
|
+
* validator.validateKey().then(res => {})
|
30
43
|
*/
|
31
44
|
constructor(schemas: SchemaType[]);
|
32
45
|
/**
|
package/lib/validator.js
CHANGED
@@ -47,7 +47,20 @@ class Validator {
|
|
47
47
|
rules;
|
48
48
|
/**
|
49
49
|
* 构造数据验证转换器
|
50
|
+
*
|
51
|
+
* See {@link https://gitee.com/towardly/ph/wikis/utils/validator|Validator文档}.
|
52
|
+
*
|
50
53
|
* @param schemas 配置验证转换规则
|
54
|
+
*
|
55
|
+
* @example
|
56
|
+
*
|
57
|
+
* const validator = new Validator([
|
58
|
+
* { key: 'mobile', rules: ['required', 'mobile'] },
|
59
|
+
* { key: 'code': rules: /^\d{6}$/, message: '请输入正确的验证码' },
|
60
|
+
* { key: 'confirmPassword', rules: ['required', 'same:password'] }
|
61
|
+
* ])
|
62
|
+
* // 验证某一个字段
|
63
|
+
* validator.validateKey().then(res => {})
|
51
64
|
*/
|
52
65
|
constructor(schemas) {
|
53
66
|
let parsedRules = {};
|