Skip to content

Commit 6833265

Browse files
committed
optimization utils
1 parent dcdc93f commit 6833265

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const directive = {
9797
inserted(el: any, binding: any) {
9898
const { arg = "is", value } = binding;
9999
const validateFunc = validateFunMaps?.[arg];
100-
if (!validateFunc && !isType(validateFunc, "Function")) {
100+
if (!validateFunc && !isType<Function>(validateFunc, "Function")) {
101101
return;
102102
}
103103
const hasPermission = validateFunc(value);

utils/index.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
/**
22
* 简单的工具集
3-
*
3+
*
44
*/
55

6-
export function isType(value: any, type: string): boolean {
7-
return Object.prototype.toString.call(value).includes(type);
6+
/**
7+
* 获取值的类型,并返回全小写的字符串
8+
* @export
9+
* @param {*} value
10+
* @returns {string} 全小写的字符串
11+
*/
12+
export function getType(value: any): string {
13+
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
14+
}
15+
16+
/**
17+
* 判断值的类型
18+
* @param value 目标值
19+
* @param type 目标类型
20+
* @returns boolean
21+
*/
22+
export function isType<P>(value: any, type: string): value is P {
23+
return getType(value) === type.toLowerCase();
824
}
925

1026
export function isBlankValue(param: any): boolean {
11-
const noVal = ['', null, NaN, undefined].includes(param);
12-
if (noVal) return true;
13-
if (Array.isArray(param)) {
14-
return param.length === 0;
15-
}
16-
if (typeof param === 'object') {
17-
return Object.keys(param).length === 0;
18-
}
19-
return false;
20-
}
27+
const noVal = ["", null, NaN, undefined].includes(param);
28+
if (noVal) return true;
29+
if (Array.isArray(param)) {
30+
return param.length === 0;
31+
}
32+
if (typeof param === "object") {
33+
return Object.keys(param).length === 0;
34+
}
35+
return false;
36+
}

0 commit comments

Comments
 (0)