ph-utils 0.2.10 → 0.2.13
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/dom.d.ts +10 -0
- package/lib/dom.js +24 -0
- package/lib/index.d.ts +8 -1
- package/lib/index.js +29 -17
- package/lib/index_m.d.ts +8 -1
- package/lib/index_m.js +16 -0
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
@@ -80,3 +80,13 @@ export declare function attr(elem: HTMLElement, key: string, value?: string): st
|
|
80
80
|
* @returns
|
81
81
|
*/
|
82
82
|
export declare function parent(el: HTMLElement): HTMLElement;
|
83
|
+
/**
|
84
|
+
* 获取隐藏节点的尺寸
|
85
|
+
* @param {string | HTMLElement} hideNode - The node to hide.
|
86
|
+
* @param parent - 添加临时节点的父节点,默认为: body.
|
87
|
+
* @returns The DOMRect of the element.
|
88
|
+
*/
|
89
|
+
export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent?: HTMLElement): {
|
90
|
+
width: number;
|
91
|
+
height: number;
|
92
|
+
};
|
package/lib/dom.js
CHANGED
@@ -164,3 +164,27 @@ export function attr(elem, key, value) {
|
|
164
164
|
export function parent(el) {
|
165
165
|
return el.parentNode;
|
166
166
|
}
|
167
|
+
/**
|
168
|
+
* 获取隐藏节点的尺寸
|
169
|
+
* @param {string | HTMLElement} hideNode - The node to hide.
|
170
|
+
* @param parent - 添加临时节点的父节点,默认为: body.
|
171
|
+
* @returns The DOMRect of the element.
|
172
|
+
*/
|
173
|
+
export function queryHideNodeSize(hideNode, parent = document.body) {
|
174
|
+
// 计算折叠菜单的高度
|
175
|
+
let $tmp = document.createElement('div');
|
176
|
+
$tmp.style.cssText = 'position:fixed;left:-1000px;top:-1000px;opacity:0;';
|
177
|
+
let $tmpInner = document.createElement('div');
|
178
|
+
$tmpInner.style.cssText = 'position:relative;';
|
179
|
+
if (typeof hideNode === 'string') {
|
180
|
+
$tmpInner.innerHTML = hideNode;
|
181
|
+
}
|
182
|
+
else {
|
183
|
+
$tmpInner.appendChild(hideNode.cloneNode(true));
|
184
|
+
}
|
185
|
+
$tmp.appendChild($tmpInner);
|
186
|
+
parent.appendChild($tmp);
|
187
|
+
let rect = $tmpInner.children[0].getBoundingClientRect();
|
188
|
+
parent.removeChild($tmp);
|
189
|
+
return { width: rect.width, height: rect.height };
|
190
|
+
}
|
package/lib/index.d.ts
CHANGED
@@ -50,5 +50,12 @@ export declare class BaseError extends Error {
|
|
50
50
|
* @param name 错误名称
|
51
51
|
* @param message 错误描述
|
52
52
|
*/
|
53
|
-
constructor(name:
|
53
|
+
constructor(name: string, message: string);
|
54
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;
|
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.round(mobile.length / 2);
|
22
26
|
let x2 = Math.round(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);
|
29
33
|
}
|
34
|
+
exports.shieldMobile = shieldMobile;
|
30
35
|
/**
|
31
36
|
* 验证参数是否是数字
|
32
37
|
* @param str 待验证的字符串
|
@@ -35,36 +40,26 @@ 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
|
-
|
56
|
-
constructor() {
|
57
|
-
if (arguments.length === 1) {
|
58
|
-
super(arguments[0]);
|
59
|
-
this.name = 'BaseError';
|
60
|
-
}
|
61
|
-
else {
|
62
|
-
super(arguments[1]);
|
63
|
-
this.name = arguments[0];
|
64
|
-
}
|
65
|
-
}
|
66
|
-
}
|
67
|
-
ass BaseError extends Error {
|
62
|
+
class BaseError extends Error {
|
68
63
|
/**
|
69
64
|
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
70
65
|
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
@@ -82,3 +77,20 @@ ass BaseError extends Error {
|
|
82
77
|
}
|
83
78
|
}
|
84
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;
|
package/lib/index_m.d.ts
CHANGED
@@ -50,5 +50,12 @@ export declare class BaseError extends Error {
|
|
50
50
|
* @param name 错误名称
|
51
51
|
* @param message 错误描述
|
52
52
|
*/
|
53
|
-
constructor(name:
|
53
|
+
constructor(name: string, message: string);
|
54
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;
|
package/lib/index_m.js
CHANGED
@@ -64,3 +64,19 @@ export class BaseError extends Error {
|
|
64
64
|
}
|
65
65
|
}
|
66
66
|
}
|
67
|
+
/**
|
68
|
+
* 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
|
69
|
+
* @param func 要节流的函数
|
70
|
+
* @param wait 需要节流的毫秒
|
71
|
+
* @returns
|
72
|
+
*/
|
73
|
+
export function throttle(func, wait = 300) {
|
74
|
+
let t = -1;
|
75
|
+
return function (...args) {
|
76
|
+
const self = this;
|
77
|
+
clearTimeout(t);
|
78
|
+
t = setTimeout(() => {
|
79
|
+
func.apply(self, args);
|
80
|
+
}, wait);
|
81
|
+
};
|
82
|
+
}
|