ph-utils 0.2.23 → 0.3.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/LICENSE +21 -0
- package/README.md +11 -11
- package/lib/date.d.ts +55 -34
- package/lib/date.js +203 -126
- package/lib/dom.d.ts +92 -92
- package/lib/dom.js +190 -190
- package/lib/file.d.ts +31 -34
- package/lib/file.js +96 -99
- package/lib/index.d.ts +97 -61
- package/lib/index.js +171 -96
- package/lib/server.d.ts +32 -39
- package/lib/server.js +77 -93
- package/lib/validator.d.ts +47 -47
- package/lib/validator.js +212 -215
- package/lib/web.d.ts +13 -55
- package/lib/web.js +57 -141
- package/package.json +10 -9
- package/lib/date_m.d.ts +0 -34
- package/lib/date_m.js +0 -119
- package/lib/index_m.d.ts +0 -61
- package/lib/index_m.js +0 -82
- package/lib/validator_m.d.ts +0 -47
- package/lib/validator_m.js +0 -210
package/lib/index.d.ts
CHANGED
@@ -1,61 +1,97 @@
|
|
1
|
-
/**
|
2
|
-
*
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
*
|
18
|
-
* @param
|
19
|
-
* @
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
/**
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
*
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
1
|
+
/**
|
2
|
+
* 验证字符串是否为空
|
3
|
+
* @param str 待验证的字符串
|
4
|
+
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
5
|
+
*/
|
6
|
+
export declare function isBlank(str?: string | null, ignoreWhitespace?: boolean): boolean;
|
7
|
+
/**
|
8
|
+
* 屏蔽手机号,中间部分用 * 展示
|
9
|
+
* @param mobile 待屏蔽的手机号
|
10
|
+
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
11
|
+
*/
|
12
|
+
export declare function shieldMobile(mobile: string): string;
|
13
|
+
/**
|
14
|
+
* 验证参数是否是数字
|
15
|
+
* @param str 待验证的字符串
|
16
|
+
* @param numericParam 通过参数标记是否包含小数、正数
|
17
|
+
* @param numericParam.isPositive 是否是正数
|
18
|
+
* @param numericParam.isFloat 是否是小数
|
19
|
+
* @returns true 是数字, false 不是数字
|
20
|
+
*/
|
21
|
+
export declare function isNumeric(str: string, numericParam?: {
|
22
|
+
isPositive?: boolean;
|
23
|
+
isFloat?: boolean;
|
24
|
+
}): boolean;
|
25
|
+
/**
|
26
|
+
* 验证参数是否是Boolean 类型
|
27
|
+
* @param str 待验证的字符串
|
28
|
+
* @returns
|
29
|
+
*/
|
30
|
+
export declare function isBoolean(str: string): boolean;
|
31
|
+
/** 生成随机数的选项 */
|
32
|
+
interface RandomConfig {
|
33
|
+
/** 生成指定长度的随机字符串 */
|
34
|
+
length?: number;
|
35
|
+
/** 是否包含英文字母, 默认为: true */
|
36
|
+
hasLetter?: boolean;
|
37
|
+
/** 生成纯数字的随机数时, 首位是否允许为 0, 默认为: true */
|
38
|
+
firstIsZero?: boolean;
|
39
|
+
/** 配合 max 生成 [min~max] 之间的随机数 */
|
40
|
+
min?: number;
|
41
|
+
/** 配合 min 生成 [min~max] 之间的随机数 */
|
42
|
+
max?: number;
|
43
|
+
/** 生成的随机数,是否包含 max, 默认: false */
|
44
|
+
hasEnd?: boolean;
|
45
|
+
/** 生成的随机数是否是整数, 默认: true */
|
46
|
+
isInteger?: boolean;
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* 生成随机数
|
50
|
+
* 1. 生成指定长度的随机数
|
51
|
+
* 2. 生成介于 [min, max] 之间的随机数
|
52
|
+
* @param opts 生成随机数的配置
|
53
|
+
* @returns
|
54
|
+
*/
|
55
|
+
export declare function random(opts: number | RandomConfig): string | number;
|
56
|
+
/**
|
57
|
+
* 带有错误名称标记的错误类型
|
58
|
+
*/
|
59
|
+
export declare class BaseError extends Error {
|
60
|
+
/**
|
61
|
+
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
62
|
+
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
63
|
+
*/
|
64
|
+
name: string;
|
65
|
+
/**
|
66
|
+
* 构造一个 name = BaseError 的错误信息
|
67
|
+
* @param message 错误描述
|
68
|
+
*/
|
69
|
+
constructor(message: string);
|
70
|
+
/**
|
71
|
+
*
|
72
|
+
* @param name 错误名称
|
73
|
+
* @param message 错误描述
|
74
|
+
*/
|
75
|
+
constructor(name: string, message: string);
|
76
|
+
}
|
77
|
+
/**
|
78
|
+
* 函数节流 - 每隔单位时间,只执行一次
|
79
|
+
* @param cb 待节流的函数
|
80
|
+
* @param wait 间隔时间
|
81
|
+
* @returns
|
82
|
+
*/
|
83
|
+
export declare function throttle<R extends any[], T>(fn: (...args: R) => T, wait?: number): (...args: R) => void;
|
84
|
+
/**
|
85
|
+
* 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
|
86
|
+
* @param fn 防抖函数
|
87
|
+
* @param interval 间隔时间段
|
88
|
+
* @returns
|
89
|
+
*/
|
90
|
+
export declare function debounce<R extends any[], T>(fn: (...args: R) => T, interval?: number): (...args: R) => void;
|
91
|
+
/**
|
92
|
+
* 将金额数字格式化为金额格式显示并且会保留两位小数[去除多余的位数,不是四舍五入,而是直接舍去] 1234523432.23 => 123,123,123.23
|
93
|
+
* @param {number} number 待转换的金额数字
|
94
|
+
* @return string
|
95
|
+
*/
|
96
|
+
export declare function formatMoney(number: number): string;
|
97
|
+
export {};
|
package/lib/index.js
CHANGED
@@ -1,96 +1,171 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
*/
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
*
|
10
|
-
* @param
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
/**
|
20
|
-
* 屏蔽手机号,中间部分用 * 展示
|
21
|
-
* @param mobile 待屏蔽的手机号
|
22
|
-
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
23
|
-
*/
|
24
|
-
function shieldMobile(mobile) {
|
25
|
-
let x1 = Math.floor(mobile.length / 2);
|
26
|
-
let x2 = Math.ceil(x1 / 2);
|
27
|
-
let shields = [' '];
|
28
|
-
for (let i = 0; i < x1 - 1; i++) {
|
29
|
-
shields.push('*');
|
30
|
-
}
|
31
|
-
shields.push(' ');
|
32
|
-
return
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
*
|
37
|
-
* @param
|
38
|
-
* @param numericParam
|
39
|
-
* @param numericParam.
|
40
|
-
* @
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
let
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
*
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
*
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
1
|
+
/**
|
2
|
+
* node 和 web 通用的工具类
|
3
|
+
*/
|
4
|
+
/** 包含字母+数字的随机数字符 */
|
5
|
+
const RANDOM_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
6
|
+
/** 只包含字母的随机数字符 */
|
7
|
+
const NUMBER_RANDOM_CHARTS = '0123456789';
|
8
|
+
/**
|
9
|
+
* 验证字符串是否为空
|
10
|
+
* @param str 待验证的字符串
|
11
|
+
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
12
|
+
*/
|
13
|
+
export function isBlank(str, ignoreWhitespace = true) {
|
14
|
+
if (str == null) {
|
15
|
+
return true;
|
16
|
+
}
|
17
|
+
return (ignoreWhitespace ? str.trim().length : str.length) === 0;
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* 屏蔽手机号,中间部分用 * 展示
|
21
|
+
* @param mobile 待屏蔽的手机号
|
22
|
+
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
23
|
+
*/
|
24
|
+
export function shieldMobile(mobile) {
|
25
|
+
let x1 = Math.floor(mobile.length / 2);
|
26
|
+
let x2 = Math.ceil(x1 / 2);
|
27
|
+
let shields = [' '];
|
28
|
+
for (let i = 0; i < x1 - 1; i++) {
|
29
|
+
shields.push('*');
|
30
|
+
}
|
31
|
+
shields.push(' ');
|
32
|
+
return mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1);
|
33
|
+
}
|
34
|
+
/**
|
35
|
+
* 验证参数是否是数字
|
36
|
+
* @param str 待验证的字符串
|
37
|
+
* @param numericParam 通过参数标记是否包含小数、正数
|
38
|
+
* @param numericParam.isPositive 是否是正数
|
39
|
+
* @param numericParam.isFloat 是否是小数
|
40
|
+
* @returns true 是数字, false 不是数字
|
41
|
+
*/
|
42
|
+
export function isNumeric(str, numericParam) {
|
43
|
+
numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
|
44
|
+
let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
|
45
|
+
let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
|
46
|
+
return new RegExp('^' + symbol + main + '$').test(str);
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* 验证参数是否是Boolean 类型
|
50
|
+
* @param str 待验证的字符串
|
51
|
+
* @returns
|
52
|
+
*/
|
53
|
+
export function isBoolean(str) {
|
54
|
+
return ['true', 'false'].indexOf(str) >= 0;
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* 生成随机数
|
58
|
+
* 1. 生成指定长度的随机数
|
59
|
+
* 2. 生成介于 [min, max] 之间的随机数
|
60
|
+
* @param opts 生成随机数的配置
|
61
|
+
* @returns
|
62
|
+
*/
|
63
|
+
export function random(opts) {
|
64
|
+
if (typeof opts === 'object' && opts.min != null && opts.max != null) {
|
65
|
+
const randomNum = Math.random();
|
66
|
+
/* 生成两个数字之间的随机数(number) */
|
67
|
+
const end = opts.hasEnd ? 1 : 0;
|
68
|
+
const resRandom = randomNum * (opts.max - opts.min + end) + opts.min;
|
69
|
+
return opts.isInteger !== false ? Math.floor(resRandom) : resRandom;
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
if (typeof opts === 'object' && opts.length == null) {
|
73
|
+
throw new Error('random_length_cannot_null');
|
74
|
+
}
|
75
|
+
const len = typeof opts === 'object' ? opts.length : opts;
|
76
|
+
/* 生成指定长度的随机数 */
|
77
|
+
const charLens = RANDOM_CHARS.length;
|
78
|
+
let chars = RANDOM_CHARS;
|
79
|
+
if (typeof opts === 'object' && opts.hasLetter === false) {
|
80
|
+
chars = NUMBER_RANDOM_CHARTS;
|
81
|
+
}
|
82
|
+
const resRandom = Array.from({ length: len }, () => chars.charAt(Math.floor(Math.random() * charLens))).join('');
|
83
|
+
if (typeof opts === 'object' && opts.firstIsZero === false && resRandom.indexOf('0') === 0) {
|
84
|
+
return random(opts);
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
return resRandom;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
/**
|
92
|
+
* 带有错误名称标记的错误类型
|
93
|
+
*/
|
94
|
+
export class BaseError extends Error {
|
95
|
+
/**
|
96
|
+
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
97
|
+
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
98
|
+
*/
|
99
|
+
name;
|
100
|
+
constructor() {
|
101
|
+
if (arguments.length === 1) {
|
102
|
+
super(arguments[0]);
|
103
|
+
this.name = 'BaseError';
|
104
|
+
}
|
105
|
+
else {
|
106
|
+
super(arguments[1]);
|
107
|
+
this.name = arguments[0];
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
/**
|
112
|
+
* 函数节流 - 每隔单位时间,只执行一次
|
113
|
+
* @param cb 待节流的函数
|
114
|
+
* @param wait 间隔时间
|
115
|
+
* @returns
|
116
|
+
*/
|
117
|
+
export function throttle(fn, wait = 500) {
|
118
|
+
// 上一次的请求时间
|
119
|
+
let last = 0;
|
120
|
+
return (...args) => {
|
121
|
+
// 当前时间戳
|
122
|
+
const now = Date.now();
|
123
|
+
if (now - last > wait) {
|
124
|
+
fn(...args);
|
125
|
+
last = now;
|
126
|
+
}
|
127
|
+
};
|
128
|
+
}
|
129
|
+
/**
|
130
|
+
* 函数防抖 - 当重复触发某一个行为(事件时),只执行最后一次触发
|
131
|
+
* @param fn 防抖函数
|
132
|
+
* @param interval 间隔时间段
|
133
|
+
* @returns
|
134
|
+
*/
|
135
|
+
export function debounce(fn, interval = 500) {
|
136
|
+
let _t = -1;
|
137
|
+
return (...args) => {
|
138
|
+
clearTimeout(_t);
|
139
|
+
_t = setTimeout(() => {
|
140
|
+
fn(...args);
|
141
|
+
}, interval);
|
142
|
+
};
|
143
|
+
}
|
144
|
+
/**
|
145
|
+
* 将金额数字格式化为金额格式显示并且会保留两位小数[去除多余的位数,不是四舍五入,而是直接舍去] 1234523432.23 => 123,123,123.23
|
146
|
+
* @param {number} number 待转换的金额数字
|
147
|
+
* @return string
|
148
|
+
*/
|
149
|
+
export function formatMoney(number) {
|
150
|
+
if (typeof Intl.NumberFormat !== 'undefined') {
|
151
|
+
const formatter = new Intl.NumberFormat('zh-CN', {
|
152
|
+
style: 'decimal',
|
153
|
+
maximumFractionDigits: 2,
|
154
|
+
});
|
155
|
+
return formatter.format(number);
|
156
|
+
}
|
157
|
+
else {
|
158
|
+
number = number || 0;
|
159
|
+
let negative = '';
|
160
|
+
let base = String(parseInt(number, 10)); // 获取数字整数部分
|
161
|
+
let mod = base.length > 3 ? base.length % 3 : 0;
|
162
|
+
/*
|
163
|
+
* 利用 正则前瞻 (?=) 将3位数字后面还紧跟一位数字的三位数字替换为 数字, 的形式
|
164
|
+
*/
|
165
|
+
let numberStr = String(number);
|
166
|
+
let usePrecision = numberStr.indexOf('.');
|
167
|
+
let dotStr = usePrecision > 0 ? numberStr.slice(usePrecision + 1) : '00';
|
168
|
+
dotStr = dotStr.length > 2 ? dotStr.slice(0, 2) : dotStr;
|
169
|
+
return (negative + (mod ? base.slice(0, mod) + ',' : '') + base.slice(mod).replace(/(\d{3})(?=\d)/g, '$1,') + '.' + dotStr);
|
170
|
+
}
|
171
|
+
}
|
package/lib/server.d.ts
CHANGED
@@ -1,39 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
/**
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
* @param options 命令参数,基于 exec-options 基础上,增加 errorName 字段
|
34
|
-
*/
|
35
|
-
execPromise(cmd: string, options?: ExecPromiseOptions & {
|
36
|
-
encoding?: BufferEncoding;
|
37
|
-
} & ExecOptions): Promise<unknown>;
|
38
|
-
};
|
39
|
-
export = _default;
|
1
|
+
/**
|
2
|
+
* 执行命令
|
3
|
+
* @param cmd 执行的命令
|
4
|
+
* @returns
|
5
|
+
*/
|
6
|
+
export declare function exec(cmd: string): Promise<string>;
|
7
|
+
interface SpawnCmdOptions {
|
8
|
+
/** 命令运行目录 */
|
9
|
+
cwd?: string;
|
10
|
+
/** 每一行的输出 */
|
11
|
+
data?: (lineText?: string) => void;
|
12
|
+
/** 错误输出 */
|
13
|
+
error?: (err: Error) => void;
|
14
|
+
/** 最终结果 */
|
15
|
+
finally?: (err?: Error) => void;
|
16
|
+
}
|
17
|
+
/**
|
18
|
+
* 执行 spawn 命令
|
19
|
+
* @param command 执行的命令: 例如: git
|
20
|
+
* @param args 命令参数: ['clone', 'https://xxxx.git']
|
21
|
+
* @param options 参数
|
22
|
+
*/
|
23
|
+
export declare function spawnCmd(command: string, args?: string[], options?: SpawnCmdOptions): void;
|
24
|
+
/**
|
25
|
+
* 执行 spawn 命令
|
26
|
+
* @param command 执行的命令: 例如: git
|
27
|
+
* @param args 命令参数: ['clone', 'https://xxxx.git']
|
28
|
+
* @param options 参数
|
29
|
+
* @returns
|
30
|
+
*/
|
31
|
+
export declare function spawnPromise(command: string, args?: string[], options?: SpawnCmdOptions): Promise<unknown>;
|
32
|
+
export {};
|