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/lib/index.d.ts CHANGED
@@ -1,61 +1,97 @@
1
- /**
2
- * node 和 web 通用的工具类
3
- */
4
- /**
5
- * 验证字符串是否为空
6
- * @param str 待验证的字符串
7
- * @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
8
- */
9
- export declare function isBlank(str?: string | null, ignoreWhitespace?: boolean): boolean;
10
- /**
11
- * 屏蔽手机号,中间部分用 * 展示
12
- * @param mobile 待屏蔽的手机号
13
- * @returns 屏蔽后的手机号,例如:123 **** 1234
14
- */
15
- export declare function shieldMobile(mobile: string): string;
16
- /**
17
- * 验证参数是否是数字
18
- * @param str 待验证的字符串
19
- * @param numericParam 通过参数标记是否包含小数、正数
20
- * @param numericParam.isPositive 是否是正数
21
- * @param numericParam.isFloat 是否是小数
22
- * @returns true 是数字, false 不是数字
23
- */
24
- export declare function isNumeric(str: string, numericParam?: {
25
- isPositive?: boolean;
26
- isFloat?: boolean;
27
- }): boolean;
28
- /**
29
- * 验证参数是否是Boolean 类型
30
- * @param str 待验证的字符串
31
- * @returns
32
- */
33
- export declare function isBoolean(str: string): boolean;
34
- /**
35
- * 带有错误名称标记的错误类型
36
- */
37
- export declare class BaseError extends Error {
38
- /**
39
- * 错误名称,类似于 Java 中的不同的 Exception[NullPointerException]
40
- * 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
41
- */
42
- name: string;
43
- /**
44
- * 构造一个 name = BaseError 的错误信息
45
- * @param message 错误描述
46
- */
47
- constructor(message: string);
48
- /**
49
- *
50
- * @param name 错误名称
51
- * @param message 错误描述
52
- */
53
- constructor(name: string, message: string);
54
- }
55
- /**
56
- * 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
57
- * @param func 要节流的函数
58
- * @param wait 需要节流的毫秒
59
- * @returns
60
- */
61
- export declare function throttle<T extends (...args: any) => any>(func: T, wait?: number): (...args: any[]) => void;
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
- "use strict";
2
- /**
3
- * node 和 web 通用的工具类
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.throttle = exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
7
- /**
8
- * 验证字符串是否为空
9
- * @param str 待验证的字符串
10
- * @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
11
- */
12
- function isBlank(str, ignoreWhitespace = true) {
13
- if (str == null) {
14
- return true;
15
- }
16
- return (ignoreWhitespace ? str.trim().length : str.length) === 0;
17
- }
18
- exports.isBlank = isBlank;
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 (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
33
- }
34
- exports.shieldMobile = shieldMobile;
35
- /**
36
- * 验证参数是否是数字
37
- * @param str 待验证的字符串
38
- * @param numericParam 通过参数标记是否包含小数、正数
39
- * @param numericParam.isPositive 是否是正数
40
- * @param numericParam.isFloat 是否是小数
41
- * @returns true 是数字, false 不是数字
42
- */
43
- function isNumeric(str, numericParam) {
44
- numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
45
- let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
46
- let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
47
- return new RegExp('^' + symbol + main + '$').test(str);
48
- }
49
- exports.isNumeric = isNumeric;
50
- /**
51
- * 验证参数是否是Boolean 类型
52
- * @param str 待验证的字符串
53
- * @returns
54
- */
55
- function isBoolean(str) {
56
- return ['true', 'false'].indexOf(str) >= 0;
57
- }
58
- exports.isBoolean = isBoolean;
59
- /**
60
- * 带有错误名称标记的错误类型
61
- */
62
- class BaseError extends Error {
63
- /**
64
- * 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
65
- * 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
66
- */
67
- name;
68
- constructor() {
69
- if (arguments.length === 1) {
70
- super(arguments[0]);
71
- this.name = 'BaseError';
72
- }
73
- else {
74
- super(arguments[1]);
75
- this.name = arguments[0];
76
- }
77
- }
78
- }
79
- exports.BaseError = BaseError;
80
- /**
81
- * 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
82
- * @param func 要节流的函数
83
- * @param wait 需要节流的毫秒
84
- * @returns
85
- */
86
- function throttle(func, wait = 300) {
87
- let t = -1;
88
- return function (...args) {
89
- const self = this;
90
- clearTimeout(t);
91
- t = setTimeout(() => {
92
- func.apply(self, args);
93
- }, wait);
94
- };
95
- }
96
- exports.throttle = throttle;
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
- /// <reference types="node" />
2
- import { ExecOptions } from 'child_process';
3
- interface ExecPromiseOptions {
4
- /** 执行失败后的错误信息添加 name 属性标记 */
5
- errorName?: string;
6
- }
7
- declare const _default: {
8
- /**
9
- * 进行 MD5 加密
10
- * @param {String} data 待加密的数据
11
- */
12
- md5(data: string): string;
13
- /**
14
- * 生成随机数:
15
- * 1. 生成指定长度的随机字符串(包含字母), 传递的第二个参数为 boolean 类型;如果想实现纯数字的,可以考虑通过 min - max 的形式,例如:
16
- *
17
- * a. 生成 4 位长度纯数字(首位不包含0):random(1000, 10000)
18
- *
19
- * b. 生成 4 位长度(首位可以包含0):random(0, 10) + random(1000, 10000)
20
- *
21
- * c. 生成 4 位长度(首位可以包含0,使用 Math.random()实现),具体实现可以参考该工具类的 web 端的 random 代码
22
- *
23
- * 2. 生成 [min, max) 之间的随机数字(**整形**),传递的第二个参数为 number 类型,如果想要生成包含 max 的传递参数的时候,手动将 max + 1 即可
24
- * @param minOrLen 如果第二个参数为 number 类型,则表示 min,生成 [min,max) 之间的随机数,返回值类型为 int;
25
- * 如果第二个参数为 boolean 类型,则表示 len,生成的随机数的长度
26
- * @param maxOrUpper 如果类型为 number 类型则表示 max,生成 [min,max) 之间的随机数,返回值类型为 int;
27
- * 如果类型为 boolean 类型则表示 upper,生成指定长度(len)的随机字符串(包含字母),true - 返回大写, 默认为 false
28
- */
29
- random(minOrLen: number, maxOrUpper: number | boolean): string | number;
30
- /**
31
- * exec Promise 版本
32
- * @param cmd 命令名称
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 {};