common-utils-kit 1.0.0 → 1.0.5
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/README.md +12 -28
- package/index.js +9 -3
- package/package.json +1 -1
- package/src/format.js +85 -0
- package/src/test.js +163 -138
- package/src/tool.js +40 -0
package/README.md
CHANGED
|
@@ -1,33 +1,17 @@
|
|
|
1
|
-
# fullstack-admin-serve
|
|
2
1
|
|
|
3
|
-
##
|
|
4
|
-
```
|
|
5
|
-
# 查看配置是否为npm源
|
|
6
|
-
npm config get registry
|
|
7
|
-
# 配置为npm源
|
|
8
|
-
npm config set registry https://registry.npmjs.org/
|
|
9
|
-
#登录npm账号
|
|
10
|
-
npm adduser
|
|
11
|
-
例:如果报错为request to https://registry.npmjs.org/-/v1/login failed, reason: connect ETIMEDOUT 104.16.0.35:443
|
|
12
|
-
切换为:npm config set registry https://registry.npmmirror.com/后
|
|
13
|
-
再次切换到:npm config set registry https://registry.npmjs.org/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
# 启动服务
|
|
17
|
-
npm run dev
|
|
2
|
+
## Common Utils Kit
|
|
18
3
|
|
|
19
|
-
|
|
20
|
-
npm run preview
|
|
4
|
+
`common-utils-kit`是一个包含常用函数的npm包,提供了一系列验证、防抖节流、数据格式化等功能
|
|
21
5
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
6
|
+
## 使用
|
|
7
|
+
```
|
|
8
|
+
# 安装
|
|
9
|
+
npm i common-utils-kit
|
|
10
|
+
# 引入
|
|
11
|
+
import {test,format,tool} from "common-utils-kit/src/test";
|
|
12
|
+
# 使用
|
|
13
|
+
test //验证数据类型 例:test.number(1)
|
|
14
|
+
format // 格式化数据 例:
|
|
15
|
+
tool //工具函数
|
|
27
16
|
```
|
|
28
|
-
浏览器访问 [http://localhost:9528](http://localhost:9528)
|
|
29
17
|
|
|
30
|
-
## 依赖
|
|
31
|
-
1. ```depcheck``` 检查依赖是否使用。
|
|
32
|
-
2. https://gitee.com/panjiachen/vue-element-admin/
|
|
33
|
-
2.
|
package/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
|
|
2
|
+
import * as test from './src/test.js';
|
|
3
|
+
import * as format from './src/format.js';
|
|
4
|
+
import * as tool from './src/tool.js';
|
|
5
|
+
console.log(' ', tool.antiShake())
|
|
6
|
+
export default {
|
|
7
|
+
test: { test },
|
|
8
|
+
format: { format },
|
|
9
|
+
tool: { tool }
|
|
4
10
|
}
|
|
5
11
|
// https://www.cnblogs.com/oldCode/p/learnjts.html
|
package/package.json
CHANGED
package/src/format.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取对象属性值,支持可选链操作符的形式
|
|
3
|
+
* @param {Object} obj - 要获取属性值的对象
|
|
4
|
+
* @param {string} key - 属性路径,可以包含点号('.')表示深层嵌套属性
|
|
5
|
+
* @returns {*} - 返回属性值,如果属性不存在则返回 undefined
|
|
6
|
+
*/
|
|
7
|
+
export function getProperty(obj, key) {
|
|
8
|
+
if (!obj) { return }
|
|
9
|
+
if (typeof key !== 'string' || key === '') {
|
|
10
|
+
return ''
|
|
11
|
+
}
|
|
12
|
+
if (key.indexOf('.') !== -1) {
|
|
13
|
+
const keys = key.split('.')
|
|
14
|
+
let firstObj = obj[keys[0]] || {}
|
|
15
|
+
for (let i = 1; i < keys.length; i++) {
|
|
16
|
+
if (firstObj) {
|
|
17
|
+
firstObj = firstObj[keys[i]]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return firstObj
|
|
21
|
+
}
|
|
22
|
+
return obj[key]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @description 数组对象去重
|
|
27
|
+
* @param {array} data 需要去重的原数组对象
|
|
28
|
+
* @param {string} parameter 去重数组的唯一码(关键字)
|
|
29
|
+
* @returns {Array} - 返回去重后的数组对象
|
|
30
|
+
*/
|
|
31
|
+
export function unrepeated(data, parameter) {
|
|
32
|
+
var temp = {};
|
|
33
|
+
var tempData = JSON.parse(JSON.stringify(data)); // 深拷贝原来的数据
|
|
34
|
+
var newData = tempData.reduce((pre, cur) => {
|
|
35
|
+
if (!temp[cur[parameter]] && cur[parameter]) {
|
|
36
|
+
temp[cur[parameter]] = true;
|
|
37
|
+
pre.push(cur);
|
|
38
|
+
}
|
|
39
|
+
return pre;
|
|
40
|
+
}, []);
|
|
41
|
+
return newData;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @description 将树形数据转化为一维数组
|
|
46
|
+
* @param {Array} previous - 树形结构数据
|
|
47
|
+
* @param {string} bind - 树形结构中下级节点的绑定属性,默认为 'children'
|
|
48
|
+
* @returns {Array} - 一维数组
|
|
49
|
+
*/
|
|
50
|
+
export function treeToFlat(previous, bind = 'children') {
|
|
51
|
+
const result = []
|
|
52
|
+
previous.forEach(item => {
|
|
53
|
+
const clonedItem = { ...item };
|
|
54
|
+
if (clonedItem[bind]) {
|
|
55
|
+
result.push(clonedItem)
|
|
56
|
+
if (clonedItem[bind].length > 0) {
|
|
57
|
+
result.push(...treeToFlat(clonedItem[bind]))
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
result.push(clonedItem)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
return result
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @description 将一维数组转树形结构
|
|
68
|
+
* @param {Array} flatArray - 树形结构数据
|
|
69
|
+
* @param {string} [rootValue=null] - 树形结构一级父 id,默认为 null
|
|
70
|
+
* @param {string} [bind="pid"] - 父 id 的属性,默认为 "pid"
|
|
71
|
+
* @returns {Array} - 树形结构
|
|
72
|
+
*/
|
|
73
|
+
export function flatToTree(flatArray, rootValue = null, bind = "pid") {
|
|
74
|
+
const tree = [];
|
|
75
|
+
flatArray.forEach(item => {
|
|
76
|
+
if (item[bind] === rootValue) {
|
|
77
|
+
const children = flatToTree(flatArray, item.id);
|
|
78
|
+
if (children.length) {
|
|
79
|
+
item.children = children;
|
|
80
|
+
}
|
|
81
|
+
tree.push(item);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return tree.length > 0 ? tree : null; // 处理不存在的根节点
|
|
85
|
+
}
|
package/src/test.js
CHANGED
|
@@ -1,249 +1,306 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* 验证是否有值,除了 0、undefined、''、false、null、{}、[] 为空,其他都属于有值
|
|
4
|
+
* @param {*} value - 要验证的值
|
|
5
|
+
* @param {string} type - 类型检查(可选),当类型为 'number' 时会将 0 视为有值
|
|
6
|
+
* @returns {boolean} - 如果有值返回 true,否则返回 false
|
|
7
|
+
*/
|
|
8
|
+
export function hasValue(value, type) {
|
|
9
|
+
switch (typeof value) {
|
|
10
|
+
case 'undefined':
|
|
11
|
+
return false;
|
|
12
|
+
case 'string':
|
|
13
|
+
return value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length > 0;
|
|
14
|
+
case 'boolean':
|
|
15
|
+
return !!value;
|
|
16
|
+
case 'number':
|
|
17
|
+
return (type === 'number' || value !== 0) && !isNaN(value);
|
|
18
|
+
case 'object':
|
|
19
|
+
if (value === null || value.length === 0) return false;
|
|
20
|
+
for (const i in value) {
|
|
21
|
+
return true; // 只要对象有属性就认为有值
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 获取值的数据类型
|
|
30
|
+
* @param {*} value - 要获取类型的值
|
|
31
|
+
* @returns {string} - 返回值的数据类型,包括 'string'、'number'、'boolean'、'function'、'array'、'object'、'null' 等
|
|
32
|
+
*/
|
|
33
|
+
export function valueType(value) {
|
|
34
|
+
switch (typeof value) {
|
|
35
|
+
case 'string':
|
|
36
|
+
return 'string';
|
|
37
|
+
case 'number':
|
|
38
|
+
return 'number';
|
|
39
|
+
case 'boolean':
|
|
40
|
+
return 'boolean';
|
|
41
|
+
case 'function':
|
|
42
|
+
return 'function';
|
|
43
|
+
case 'object':
|
|
44
|
+
if (value === null) {
|
|
45
|
+
return 'null';
|
|
46
|
+
} else if (typeof Array.isArray === 'function' && Array.isArray(value)) {
|
|
47
|
+
return 'array';
|
|
48
|
+
} else if (Object.prototype.toString.call(value) === '[object Array]') {
|
|
49
|
+
return 'array';
|
|
50
|
+
} else if (Object.prototype.toString.call(value) === '[object Object]') {
|
|
51
|
+
return 'object';
|
|
52
|
+
}
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
1
57
|
/**
|
|
2
58
|
* 验证电子邮箱格式
|
|
3
59
|
*/
|
|
4
|
-
function email(value) {
|
|
5
|
-
|
|
60
|
+
export function email(value) {
|
|
61
|
+
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value)
|
|
6
62
|
}
|
|
7
63
|
|
|
8
64
|
/**
|
|
9
65
|
* 验证手机格式
|
|
10
66
|
*/
|
|
11
|
-
function mobile(value) {
|
|
12
|
-
|
|
67
|
+
export function mobile(value) {
|
|
68
|
+
return /^1[23456789]\d{9}$/.test(value)
|
|
13
69
|
}
|
|
14
70
|
|
|
15
71
|
/**
|
|
16
72
|
* 验证URL格式
|
|
17
73
|
*/
|
|
18
|
-
function url(value) {
|
|
19
|
-
|
|
20
|
-
|
|
74
|
+
export function url(value) {
|
|
75
|
+
return /^((https|http|ftp|rtsp|mms):\/\/)(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z].[a-zA-Z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/
|
|
76
|
+
.test(value)
|
|
21
77
|
}
|
|
22
78
|
|
|
23
79
|
/**
|
|
24
80
|
* 验证日期格式
|
|
25
81
|
*/
|
|
26
|
-
function date(value) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
82
|
+
export function date(value) {
|
|
83
|
+
if (!value) return false
|
|
84
|
+
// 判断是否数值或者字符串数值(意味着为时间戳),转为数值,否则new Date无法识别字符串时间戳
|
|
85
|
+
if (number(value)) value = +value
|
|
86
|
+
return !/Invalid|NaN/.test(new Date(value).toString())
|
|
31
87
|
}
|
|
32
88
|
|
|
33
89
|
/**
|
|
34
90
|
* 验证ISO类型的日期格式
|
|
35
91
|
*/
|
|
36
|
-
function dateISO(value) {
|
|
37
|
-
|
|
92
|
+
export function dateISO(value) {
|
|
93
|
+
return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value)
|
|
38
94
|
}
|
|
39
95
|
|
|
40
96
|
/**
|
|
41
97
|
* 验证十进制数字
|
|
42
98
|
*/
|
|
43
|
-
function number(value) {
|
|
44
|
-
|
|
99
|
+
export function number(value) {
|
|
100
|
+
return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value)
|
|
45
101
|
}
|
|
46
102
|
|
|
47
103
|
/**
|
|
48
104
|
* 验证字符串
|
|
49
105
|
*/
|
|
50
|
-
function string(value) {
|
|
51
|
-
|
|
106
|
+
export function string(value) {
|
|
107
|
+
return typeof value === 'string'
|
|
52
108
|
}
|
|
53
109
|
|
|
54
110
|
/**
|
|
55
111
|
* 验证整数
|
|
56
112
|
*/
|
|
57
|
-
function digits(value) {
|
|
58
|
-
|
|
113
|
+
export function digits(value) {
|
|
114
|
+
return /^\d+$/.test(value)
|
|
59
115
|
}
|
|
60
116
|
|
|
61
117
|
/**
|
|
62
118
|
* 验证身份证号码
|
|
63
119
|
*/
|
|
64
|
-
function idCard(value) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
120
|
+
export function idCard(value) {
|
|
121
|
+
return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
|
|
122
|
+
value
|
|
123
|
+
)
|
|
68
124
|
}
|
|
69
125
|
|
|
70
126
|
/**
|
|
71
127
|
* 是否车牌号
|
|
72
128
|
*/
|
|
73
|
-
function carNo(value) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
129
|
+
export function carNo(value) {
|
|
130
|
+
// 新能源车牌
|
|
131
|
+
const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
|
|
132
|
+
// 旧车牌
|
|
133
|
+
const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
|
|
134
|
+
if (value.length === 7) {
|
|
135
|
+
return creg.test(value)
|
|
136
|
+
} if (value.length === 8) {
|
|
137
|
+
return xreg.test(value)
|
|
138
|
+
}
|
|
139
|
+
return false
|
|
84
140
|
}
|
|
85
141
|
|
|
86
142
|
/**
|
|
87
143
|
* 金额,只允许2位小数
|
|
88
144
|
*/
|
|
89
|
-
function amount(value) {
|
|
90
|
-
|
|
91
|
-
|
|
145
|
+
export function amount(value) {
|
|
146
|
+
// 金额,只允许保留两位小数
|
|
147
|
+
return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value)
|
|
92
148
|
}
|
|
93
149
|
|
|
94
150
|
/**
|
|
95
151
|
* 中文
|
|
96
152
|
*/
|
|
97
|
-
function chinese(value) {
|
|
98
|
-
|
|
99
|
-
|
|
153
|
+
export function chinese(value) {
|
|
154
|
+
const reg = /^[\u4e00-\u9fa5]+$/gi
|
|
155
|
+
return reg.test(value)
|
|
100
156
|
}
|
|
101
157
|
|
|
102
158
|
/**
|
|
103
159
|
* 只能输入字母
|
|
104
160
|
*/
|
|
105
|
-
function letter(value) {
|
|
106
|
-
|
|
161
|
+
export function letter(value) {
|
|
162
|
+
return /^[a-zA-Z]*$/.test(value)
|
|
107
163
|
}
|
|
108
164
|
|
|
109
165
|
/**
|
|
110
166
|
* 只能是字母或者数字
|
|
111
167
|
*/
|
|
112
|
-
function enOrNum(value) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
168
|
+
export function enOrNum(value) {
|
|
169
|
+
// 英文或者数字
|
|
170
|
+
const reg = /^[0-9a-zA-Z]*$/g
|
|
171
|
+
return reg.test(value)
|
|
116
172
|
}
|
|
117
173
|
|
|
118
174
|
/**
|
|
119
175
|
* 验证是否包含某个值
|
|
120
176
|
*/
|
|
121
|
-
function contains(value, param) {
|
|
122
|
-
|
|
177
|
+
export function contains(value, param) {
|
|
178
|
+
return value.indexOf(param) >= 0
|
|
123
179
|
}
|
|
124
180
|
|
|
125
181
|
/**
|
|
126
182
|
* 验证一个值范围[min, max]
|
|
127
183
|
*/
|
|
128
|
-
function range(value, param) {
|
|
129
|
-
|
|
184
|
+
export function range(value, param) {
|
|
185
|
+
return value >= param[0] && value <= param[1]
|
|
130
186
|
}
|
|
131
187
|
|
|
132
188
|
/**
|
|
133
189
|
* 验证一个长度范围[min, max]
|
|
134
190
|
*/
|
|
135
|
-
function rangeLength(value, param) {
|
|
136
|
-
|
|
191
|
+
export function rangeLength(value, param) {
|
|
192
|
+
return value.length >= param[0] && value.length <= param[1]
|
|
137
193
|
}
|
|
138
194
|
|
|
139
195
|
/**
|
|
140
196
|
* 是否固定电话
|
|
141
197
|
*/
|
|
142
|
-
function landline(value) {
|
|
143
|
-
|
|
144
|
-
|
|
198
|
+
export function landline(value) {
|
|
199
|
+
const reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/
|
|
200
|
+
return reg.test(value)
|
|
145
201
|
}
|
|
146
202
|
|
|
147
203
|
/**
|
|
148
204
|
* 判断是否为空
|
|
149
205
|
*/
|
|
150
|
-
function empty(value) {
|
|
151
|
-
|
|
206
|
+
export function empty(value) {
|
|
207
|
+
console.log(' ', (isNaN(value)))
|
|
208
|
+
switch (typeof value) {
|
|
152
209
|
case 'undefined':
|
|
153
|
-
|
|
210
|
+
return true
|
|
154
211
|
case 'string':
|
|
155
|
-
|
|
156
|
-
|
|
212
|
+
if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true
|
|
213
|
+
break
|
|
157
214
|
case 'boolean':
|
|
158
|
-
|
|
159
|
-
|
|
215
|
+
if (!value) return true
|
|
216
|
+
break
|
|
160
217
|
case 'number':
|
|
161
|
-
|
|
162
|
-
|
|
218
|
+
if (value === 0 || isNaN(value)) return true
|
|
219
|
+
break
|
|
163
220
|
case 'object':
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
221
|
+
if (value === null || value.length === 0) return true
|
|
222
|
+
for (const i in value) {
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
return true
|
|
226
|
+
}
|
|
227
|
+
return false
|
|
171
228
|
}
|
|
172
229
|
|
|
173
230
|
/**
|
|
174
231
|
* 是否json字符串
|
|
175
232
|
*/
|
|
176
|
-
function jsonString(value) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
233
|
+
export function jsonString(value) {
|
|
234
|
+
if (typeof value === 'string') {
|
|
235
|
+
try {
|
|
236
|
+
const obj = JSON.parse(value)
|
|
237
|
+
if (typeof obj === 'object' && obj) {
|
|
238
|
+
return true
|
|
239
|
+
}
|
|
240
|
+
return false
|
|
241
|
+
} catch (e) {
|
|
242
|
+
return false
|
|
187
243
|
}
|
|
188
|
-
|
|
244
|
+
}
|
|
245
|
+
return false
|
|
189
246
|
}
|
|
190
247
|
|
|
191
248
|
/**
|
|
192
249
|
* 是否数组
|
|
193
250
|
*/
|
|
194
|
-
function array(value) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
251
|
+
export function array(value) {
|
|
252
|
+
if (typeof Array.isArray === 'function') {
|
|
253
|
+
return Array.isArray(value)
|
|
254
|
+
}
|
|
255
|
+
return Object.prototype.toString.call(value) === '[object Array]'
|
|
199
256
|
}
|
|
200
257
|
|
|
201
258
|
/**
|
|
202
259
|
* 是否对象
|
|
203
260
|
*/
|
|
204
|
-
function object(value) {
|
|
205
|
-
|
|
261
|
+
export function object(value) {
|
|
262
|
+
return Object.prototype.toString.call(value) === '[object Object]'
|
|
206
263
|
}
|
|
207
264
|
|
|
208
265
|
/**
|
|
209
266
|
* 是否短信验证码
|
|
210
267
|
*/
|
|
211
|
-
function code(value, len = 6) {
|
|
212
|
-
|
|
268
|
+
export function code(value, len = 6) {
|
|
269
|
+
return new RegExp(`^\\d{${len}}$`).test(value)
|
|
213
270
|
}
|
|
214
271
|
|
|
215
272
|
/**
|
|
216
273
|
* 是否函数方法
|
|
217
274
|
* @param {Object} value
|
|
218
275
|
*/
|
|
219
|
-
function func(value) {
|
|
220
|
-
|
|
276
|
+
export function func(value) {
|
|
277
|
+
return typeof value === 'function'
|
|
221
278
|
}
|
|
222
279
|
|
|
223
280
|
/**
|
|
224
281
|
* 是否promise对象
|
|
225
282
|
* @param {Object} value
|
|
226
283
|
*/
|
|
227
|
-
function promise(value) {
|
|
228
|
-
|
|
284
|
+
export function promise(value) {
|
|
285
|
+
return object(value) && func(value.then) && func(value.catch)
|
|
229
286
|
}
|
|
230
287
|
|
|
231
288
|
/** 是否图片格式
|
|
232
289
|
* @param {Object} value
|
|
233
290
|
*/
|
|
234
|
-
function image(value) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
291
|
+
export function image(value) {
|
|
292
|
+
const newValue = value.split('?')[0]
|
|
293
|
+
const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i
|
|
294
|
+
return IMAGE_REGEXP.test(newValue)
|
|
238
295
|
}
|
|
239
296
|
|
|
240
297
|
/**
|
|
241
298
|
* 是否视频格式
|
|
242
299
|
* @param {Object} value
|
|
243
300
|
*/
|
|
244
|
-
function video(value) {
|
|
245
|
-
|
|
246
|
-
|
|
301
|
+
export function video(value) {
|
|
302
|
+
const VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv|m3u8)/i
|
|
303
|
+
return VIDEO_REGEXP.test(value)
|
|
247
304
|
}
|
|
248
305
|
|
|
249
306
|
/**
|
|
@@ -251,38 +308,6 @@ function video(value) {
|
|
|
251
308
|
* @param {Object}
|
|
252
309
|
* @return {Boolean}
|
|
253
310
|
*/
|
|
254
|
-
function regExp(o) {
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
export default {
|
|
259
|
-
email,
|
|
260
|
-
mobile,
|
|
261
|
-
url,
|
|
262
|
-
date,
|
|
263
|
-
dateISO,
|
|
264
|
-
number,
|
|
265
|
-
digits,
|
|
266
|
-
idCard,
|
|
267
|
-
carNo,
|
|
268
|
-
amount,
|
|
269
|
-
chinese,
|
|
270
|
-
letter,
|
|
271
|
-
enOrNum,
|
|
272
|
-
contains,
|
|
273
|
-
range,
|
|
274
|
-
rangeLength,
|
|
275
|
-
empty,
|
|
276
|
-
isEmpty: empty,
|
|
277
|
-
jsonString,
|
|
278
|
-
landline,
|
|
279
|
-
object,
|
|
280
|
-
array,
|
|
281
|
-
code,
|
|
282
|
-
func,
|
|
283
|
-
promise,
|
|
284
|
-
video,
|
|
285
|
-
image,
|
|
286
|
-
regExp,
|
|
287
|
-
string
|
|
311
|
+
export function regExp(o) {
|
|
312
|
+
return o && Object.prototype.toString.call(o) === '[object RegExp]'
|
|
288
313
|
}
|
package/src/tool.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
let timeout = null
|
|
4
|
+
/**
|
|
5
|
+
* 函数防抖
|
|
6
|
+
* @param {number} time - 防抖间隔时间,单位毫秒
|
|
7
|
+
* @returns {Promise<boolean>} - 返回一个 Promise,在防抖时间内触发 resolve(true)
|
|
8
|
+
* @example
|
|
9
|
+
* debounce(time).then(res => {
|
|
10
|
+
* 执行方法
|
|
11
|
+
* })
|
|
12
|
+
*/
|
|
13
|
+
export function debounce(time = 500) {
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
if (timeout !== null) clearTimeout(timeout);
|
|
16
|
+
timeout = setTimeout(() => {
|
|
17
|
+
resolve(true)
|
|
18
|
+
}, time)
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
let wait = null
|
|
24
|
+
/**
|
|
25
|
+
* @description 节流
|
|
26
|
+
* @param {number} time 每time秒调用一次 默认0.5秒
|
|
27
|
+
* @example
|
|
28
|
+
* throttle(time).then(res => {
|
|
29
|
+
* 执行方法
|
|
30
|
+
* })
|
|
31
|
+
*/
|
|
32
|
+
export function throttle(time = 500) { //防抖 // 默认0.2秒后执行事件
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
if (wait) { return }
|
|
35
|
+
wait = setTimeout(() => {
|
|
36
|
+
resolve(true)
|
|
37
|
+
wait = null
|
|
38
|
+
}, time)
|
|
39
|
+
})
|
|
40
|
+
}
|