ph-utils 0.2.21 → 0.2.23
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.js +20 -6
- package/lib/web.d.ts +3 -4
- package/lib/web.js +13 -24
- 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;
|
package/lib/web.d.ts
CHANGED
@@ -3,15 +3,14 @@
|
|
3
3
|
* @param form {object} Form 节点对象
|
4
4
|
*/
|
5
5
|
export declare const formJson: <T>(form: HTMLFormElement) => T;
|
6
|
-
interface QueryRes {
|
7
|
-
[index: string]: number | boolean | string | string[];
|
8
|
-
}
|
9
6
|
/**
|
10
7
|
* 获取 url query 参数 (get 请求的参数)
|
11
8
|
* @param search 如果是 React 应用就需要传递 useLocation().search
|
12
9
|
* @returns
|
13
10
|
*/
|
14
|
-
export declare function query(search?: string):
|
11
|
+
export declare function query(search?: string): {
|
12
|
+
[index: string]: string;
|
13
|
+
};
|
15
14
|
interface RandomOpts {
|
16
15
|
/** 生成的随机数是整形还是小数形 */
|
17
16
|
isInt?: boolean;
|
package/lib/web.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* web(浏览器) 端工具类
|
3
3
|
*/
|
4
|
-
import { isBlank
|
4
|
+
import { isBlank } from './index_m';
|
5
5
|
/**
|
6
6
|
* 解析 Form 表单中的 input 元素的数据为 JSON 格式,key: input-name;value: input-value
|
7
7
|
* @param form {object} Form 节点对象
|
@@ -38,32 +38,21 @@ export function query(search) {
|
|
38
38
|
if (isBlank(search)) {
|
39
39
|
search = location.search;
|
40
40
|
}
|
41
|
+
const searchParams = new URLSearchParams(search);
|
41
42
|
let query = {};
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
if (isBoolean(value)) {
|
50
|
-
value = Boolean(value);
|
51
|
-
}
|
52
|
-
else if (isNumeric(value)) {
|
53
|
-
value = Number(value);
|
43
|
+
for (const [key, value] of searchParams) {
|
44
|
+
let oldValue = query[key];
|
45
|
+
let newValue = value;
|
46
|
+
if (oldValue != null) {
|
47
|
+
if (oldValue instanceof Array) {
|
48
|
+
oldValue.push(value);
|
49
|
+
newValue = oldValue;
|
54
50
|
}
|
55
|
-
|
56
|
-
|
57
|
-
oldValue.push(value);
|
58
|
-
value = oldValue;
|
59
|
-
}
|
60
|
-
else {
|
61
|
-
value = [value, oldValue];
|
62
|
-
}
|
51
|
+
else {
|
52
|
+
newValue = [value, oldValue];
|
63
53
|
}
|
64
|
-
|
65
|
-
|
66
|
-
});
|
54
|
+
}
|
55
|
+
query[key] = newValue;
|
67
56
|
}
|
68
57
|
return query;
|
69
58
|
}
|