ph-utils 0.2.22 → 0.2.23
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.js +20 -6
- package/package.json +1 -1
package/lib/index.js
CHANGED
@@ -1,23 +1,27 @@
|
|
1
|
+
"use strict";
|
1
2
|
/**
|
2
3
|
* node 和 web 通用的工具类
|
3
4
|
*/
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.throttle = exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
|
4
7
|
/**
|
5
8
|
* 验证字符串是否为空
|
6
9
|
* @param str 待验证的字符串
|
7
10
|
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
8
11
|
*/
|
9
|
-
|
12
|
+
function isBlank(str, ignoreWhitespace = true) {
|
10
13
|
if (str == null) {
|
11
14
|
return true;
|
12
15
|
}
|
13
16
|
return (ignoreWhitespace ? str.trim().length : str.length) === 0;
|
14
17
|
}
|
18
|
+
exports.isBlank = isBlank;
|
15
19
|
/**
|
16
20
|
* 屏蔽手机号,中间部分用 * 展示
|
17
21
|
* @param mobile 待屏蔽的手机号
|
18
22
|
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
19
23
|
*/
|
20
|
-
|
24
|
+
function shieldMobile(mobile) {
|
21
25
|
let x1 = Math.floor(mobile.length / 2);
|
22
26
|
let x2 = Math.ceil(x1 / 2);
|
23
27
|
let shields = [' '];
|
@@ -27,6 +31,7 @@ export function shieldMobile(mobile) {
|
|
27
31
|
shields.push(' ');
|
28
32
|
return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
|
29
33
|
}
|
34
|
+
exports.shieldMobile = shieldMobile;
|
30
35
|
/**
|
31
36
|
* 验证参数是否是数字
|
32
37
|
* @param str 待验证的字符串
|
@@ -35,24 +40,31 @@ export function shieldMobile(mobile) {
|
|
35
40
|
* @param numericParam.isFloat 是否是小数
|
36
41
|
* @returns true 是数字, false 不是数字
|
37
42
|
*/
|
38
|
-
|
43
|
+
function isNumeric(str, numericParam) {
|
39
44
|
numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
|
40
45
|
let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
|
41
46
|
let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
|
42
47
|
return new RegExp('^' + symbol + main + '$').test(str);
|
43
48
|
}
|
49
|
+
exports.isNumeric = isNumeric;
|
44
50
|
/**
|
45
51
|
* 验证参数是否是Boolean 类型
|
46
52
|
* @param str 待验证的字符串
|
47
53
|
* @returns
|
48
54
|
*/
|
49
|
-
|
55
|
+
function isBoolean(str) {
|
50
56
|
return ['true', 'false'].indexOf(str) >= 0;
|
51
57
|
}
|
58
|
+
exports.isBoolean = isBoolean;
|
52
59
|
/**
|
53
60
|
* 带有错误名称标记的错误类型
|
54
61
|
*/
|
55
|
-
|
62
|
+
class BaseError extends Error {
|
63
|
+
/**
|
64
|
+
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
65
|
+
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
66
|
+
*/
|
67
|
+
name;
|
56
68
|
constructor() {
|
57
69
|
if (arguments.length === 1) {
|
58
70
|
super(arguments[0]);
|
@@ -64,13 +76,14 @@ export class BaseError extends Error {
|
|
64
76
|
}
|
65
77
|
}
|
66
78
|
}
|
79
|
+
exports.BaseError = BaseError;
|
67
80
|
/**
|
68
81
|
* 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
|
69
82
|
* @param func 要节流的函数
|
70
83
|
* @param wait 需要节流的毫秒
|
71
84
|
* @returns
|
72
85
|
*/
|
73
|
-
|
86
|
+
function throttle(func, wait = 300) {
|
74
87
|
let t = -1;
|
75
88
|
return function (...args) {
|
76
89
|
const self = this;
|
@@ -80,3 +93,4 @@ export function throttle(func, wait = 300) {
|
|
80
93
|
}, wait);
|
81
94
|
};
|
82
95
|
}
|
96
|
+
exports.throttle = throttle;
|