common-utils-kit 1.1.3 → 1.1.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 CHANGED
@@ -17,12 +17,39 @@ import commonUtilsKit from "common-utils-kit";
17
17
  Vue.prototype.$test = commonUtilsKit.test;
18
18
  例:验证是否为数字 this.$test.number(1)
19
19
  ```
20
- <!-- ### directives(指令)
21
- 1. v-debounce(按钮或输入框防抖)
22
- * function:需要防抖的函数
23
- * event:事件类型
24
- * time:防抖时间
25
- ```
26
- <el-input v-debounce="[function,`input`,1000]" placeholder="输入框数组防抖" />
27
- <el-button v-debounce="[function,`click`,1000]">按钮防抖</el-button>
28
- ``` -->
20
+ ### directives(指令)
21
+ 1. debounce(按钮或输入框防抖)
22
+ 2. inputDebounce(输入框防抖)
23
+ 3. btnDebounce(按钮防抖)
24
+ 4. btnThrottle(按钮节流)
25
+ 5. relativeTime(相对时间)
26
+ 6. replace(输入框字符串替换)
27
+ 7. elDragDialog(拖拽dialog)
28
+ ### files(文件)
29
+ 1. downloadBase64File(base64文件下载)
30
+ 2. downloadFiles(文件http地址下载)
31
+ ### format(格式化数据)
32
+ 1. unrepeated(数组对象去重)
33
+ 2. treeToFlat(树形结构拍平为一维数组)
34
+ 3. flatToTree(一维数组递归成为树形结构)
35
+ 4. recursionFunction(递归重构子集数据)
36
+ 5. filterData(将switch case语句转为数组对象)
37
+ 6. dateDiff(计算日期差值(单位:天))
38
+ 7. getNowDate(获取当前日期)
39
+ 8. setFormColumnsData(设置自定义表单列数据)
40
+ ### test(验证)
41
+ 1. hasValue(是否有值,可验证指定类型)
42
+ 2. valueType(返回数据类型)
43
+ 3. emai(是否为邮箱)
44
+ 4. mobile(是否为手机号)
45
+ 5. url(是否为url)
46
+ 6. idCard(是否为身份证号)
47
+ 7. carNo(是否为车牌号)
48
+ 8. chinese(是否为中文)
49
+ 9. letter(是否为字母)
50
+ 10. landline(是否为座机)
51
+ 11. code(是否为6位数短信验证码)
52
+ ### tool(工具函数)
53
+ 1. debounce(防抖)
54
+ 2. throttle(节流)
55
+ 3. sleep(睡眠阻塞延时)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "common-utils-kit",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/files.js CHANGED
@@ -107,3 +107,131 @@ export function downloadFiles(url, filename) {
107
107
  xhr.send();
108
108
  });
109
109
  }
110
+ /**
111
+ * File转Base64
112
+ * @param {File} file - File对象格式文件数据
113
+ * @example
114
+ * fileToBase64(file).then((base64) => {
115
+ * console.log(base64); // base64格式的文件
116
+ * }).catch((error) => {
117
+ * console.error(error); // 转换失败或其他错误信息
118
+ * });
119
+ */
120
+ export function fileToBase64(file) {
121
+ return new Promise((resolve, reject) => {
122
+ const reader = new FileReader();
123
+ reader.readAsDataURL(file);
124
+ this.aa = reader.result;
125
+ reader.onload = () => resolve(reader.result);
126
+ reader.onerror = (error) => reject(error);
127
+ });
128
+ }
129
+ /**
130
+ * Base64转File
131
+ * @param {String} base64 - base64格式文件数据
132
+ * @example
133
+ * base64ToFile(base64).then((file) => {
134
+ * console.log(file); // File格式的文件
135
+ * }).catch((error) => {
136
+ * console.error(error); // 转换失败或其他错误信息
137
+ * });
138
+ */
139
+ export function base64ToFile(base64, filename = "file") { //将base64转换为文件
140
+ return new Promise((resolve, reject) => {
141
+ const arr = base64.split(',')
142
+ const mime = arr[0].match(/:(.*?);/)[1]
143
+ const suffix = mime.split('/')[1]
144
+ const bstr = atob(arr[1])
145
+ let n = bstr.length
146
+ const u8arr = new Uint8Array(n)
147
+ while (n--) {
148
+ u8arr[n] = bstr.charCodeAt(n)
149
+ }
150
+ const fileData = new File([u8arr], `${filename}.${suffix}`, { type: mime })
151
+ resolve(fileData)
152
+ })
153
+ }
154
+ /**
155
+ * Base64转Blob
156
+ * @param {String} base64 - base64格式文件数据
157
+ * @example
158
+ * base64ToFile(base64).then((blob) => {
159
+ * console.log(blob); // blob格式的文件
160
+ * }).catch((error) => {
161
+ * console.error(error); // 转换失败或其他错误信息
162
+ * });
163
+ */
164
+ export function base64ToFileBlob(dataurl) {
165
+ return new Promise((resolve, reject) => {
166
+ var arr = dataurl.split(','),
167
+ mime = arr[0].match(/:(.*?);/)[1],
168
+ bstr = atob(arr[1]),
169
+ n = bstr.length,
170
+ u8arr = new Uint8Array(n)
171
+ while (n--) {
172
+ u8arr[n] = bstr.charCodeAt(n)
173
+ }
174
+ const blobData = new Blob([u8arr], { type: mime })
175
+ resolve(blobData)
176
+ })
177
+ }
178
+ // 使用canvas压缩图片
179
+ export function uploadCompress(file, config) {
180
+ return new Promise((resolve, reject) => {
181
+ const image = new Image();
182
+ image.src = URL.createObjectURL(file);
183
+ image.onload = () => {
184
+ let resultBlob = "";
185
+ // 压缩图片
186
+ resultBlob = compressUpload(image, file, config);
187
+ resolve(resultBlob);
188
+ };
189
+ image.onerror = (err) => { reject(err) };
190
+ /* 图片压缩-canvas压缩 */
191
+ function compressUpload(image, file, compressConfig={}) {
192
+ const canvas = document.createElement("canvas");
193
+ const ctx = canvas.getContext("2d");
194
+ let width = compressConfig.width || image.width;
195
+ let height = compressConfig.height || image.height;
196
+
197
+ // 只设置宽度时,等比计算高度
198
+ if (compressConfig.width && !compressConfig.height) {
199
+ height = (compressConfig.width / image.width) * image.height;
200
+ }
201
+ // 只设置高度时,等比计算宽度
202
+ if (compressConfig.height && !compressConfig.width) {
203
+ width = (compressConfig.height / image.height) * image.width;
204
+ }
205
+
206
+ canvas.width = width;
207
+ canvas.height = height;
208
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
209
+ ctx.drawImage(image, 0, 0, width, height);
210
+
211
+ // 进行最小压缩0.1
212
+ const compressData = canvas.toDataURL(
213
+ file.type || "image/jpeg",
214
+ compressConfig.rate || 0.5
215
+ );
216
+ // base64转Blob
217
+ const blobImg = dataURItoBlob(compressData);
218
+ console.log(" ", (blobImg.size / 1024 / 1024).toFixed(2));
219
+ return blobImg;
220
+ }
221
+ /* 图片格式转换——base64转Blob对象 */
222
+ function dataURItoBlob(data) {
223
+ let byteString;
224
+ if (data.split(",")[0].indexOf("base64") >= 0) {
225
+ byteString = data.split(",")[1];
226
+ } else {
227
+ byteString = unescape(data.split(",")[1]);
228
+ }
229
+ const mimeString = data.split(",")[0].split(":")[1].split(";")[0];
230
+ const ia = new Uint8Array(byteString.length);
231
+ for (let i = 0; i < byteString.length; i += 1) {
232
+ ia[i] = byteString.charCodeAt(i);
233
+ }
234
+ return new Blob([ia], { type: mimeString });
235
+ }
236
+ });
237
+ }
package/src/format.js CHANGED
@@ -126,9 +126,7 @@ export function flatToTree(flatArray, rootValue = null, bind = "pid") {
126
126
  flatArray.forEach(item => {
127
127
  if (item[bind] === rootValue) {
128
128
  const children = flatToTree(flatArray, item.id);
129
- if (children.length) {
130
- item.children = children;
131
- }
129
+ item.children = children && children.length ? children : []
132
130
  tree.push(item);
133
131
  }
134
132
  });
package/src/test.js CHANGED
@@ -19,17 +19,15 @@ export function hasValue(value, type) {
19
19
  case 'undefined':
20
20
  return false;
21
21
  case 'string':
22
- return value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length > 0;
22
+ return (type === 'string' || !type) && value.trim().length > 0;
23
23
  case 'boolean':
24
- return !!value;
24
+ return (type === 'boolean' || !type) && !!value;
25
25
  case 'number':
26
- return (type === 'number' || value !== 0) && !isNaN(value);
26
+ return (type === 'number' || !type) && ((type === 'number' || value !== 0) && !isNaN(value));
27
27
  case 'object':
28
- if (value === null || value.length === 0) return false;
29
- for (const i in value) {
30
- return true; // 只要对象有属性就认为有值
31
- }
32
- return false;
28
+ if (value === null) return false;
29
+ if (Array.isArray(value)) return (type === 'array' || !type) && value.length > 0;
30
+ return (type === 'object' || !type) && Object.keys(value).length > 0;
33
31
  }
34
32
  return false;
35
33
  }
@@ -49,21 +47,17 @@ export function hasValue(value, type) {
49
47
  * console.log(valueType(null)); // 输出: 'null'
50
48
  */
51
49
  export function valueType(value) {
52
- switch (typeof value) {
50
+ const type = typeof value;
51
+ switch (type) {
53
52
  case 'string':
54
- return 'string';
55
53
  case 'number':
56
- return 'number';
57
54
  case 'boolean':
58
- return 'boolean';
59
55
  case 'function':
60
- return 'function';
56
+ return type;
61
57
  case 'object':
62
58
  if (value === null) {
63
59
  return 'null';
64
- } else if (typeof Array.isArray === 'function' && Array.isArray(value)) {
65
- return 'array';
66
- } else if (Object.prototype.toString.call(value) === '[object Array]') {
60
+ } else if (Array.isArray(value)) {
67
61
  return 'array';
68
62
  } else if (Object.prototype.toString.call(value) === '[object Object]') {
69
63
  return 'object';
@@ -94,44 +88,6 @@ export function url(value) {
94
88
  .test(value)
95
89
  }
96
90
 
97
- /**
98
- * 验证日期格式
99
- */
100
- export function date(value) {
101
- if (!value) return false
102
- // 判断是否数值或者字符串数值(意味着为时间戳),转为数值,否则new Date无法识别字符串时间戳
103
- if (number(value)) value = +value
104
- return !/Invalid|NaN/.test(new Date(value).toString())
105
- }
106
-
107
- /**
108
- * 验证ISO类型的日期格式
109
- */
110
- export function dateISO(value) {
111
- return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value)
112
- }
113
-
114
- /**
115
- * 验证十进制数字
116
- */
117
- export function number(value) {
118
- return /^[\+-]?(\d+\.?\d*|\.\d+|\d\.\d+e\+\d+)$/.test(value)
119
- }
120
-
121
- /**
122
- * 验证字符串
123
- */
124
- export function string(value) {
125
- return typeof value === 'string'
126
- }
127
-
128
- /**
129
- * 验证整数
130
- */
131
- export function digits(value) {
132
- return /^\d+$/.test(value)
133
- }
134
-
135
91
  /**
136
92
  * 验证身份证号码
137
93
  */
@@ -180,36 +136,6 @@ export function letter(value) {
180
136
  return /^[a-zA-Z]*$/.test(value)
181
137
  }
182
138
 
183
- /**
184
- * 只能是字母或者数字
185
- */
186
- export function enOrNum(value) {
187
- // 英文或者数字
188
- const reg = /^[0-9a-zA-Z]*$/g
189
- return reg.test(value)
190
- }
191
-
192
- /**
193
- * 验证是否包含某个值
194
- */
195
- export function contains(value, param) {
196
- return value.indexOf(param) >= 0
197
- }
198
-
199
- /**
200
- * 验证一个值范围[min, max]
201
- */
202
- export function range(value, param) {
203
- return value >= param[0] && value <= param[1]
204
- }
205
-
206
- /**
207
- * 验证一个长度范围[min, max]
208
- */
209
- export function rangeLength(value, param) {
210
- return value.length >= param[0] && value.length <= param[1]
211
- }
212
-
213
139
  /**
214
140
  * 是否固定电话
215
141
  */
@@ -218,68 +144,6 @@ export function landline(value) {
218
144
  return reg.test(value)
219
145
  }
220
146
 
221
- /**
222
- * 判断是否为空
223
- */
224
- export function empty(value) {
225
- console.log(' ', (isNaN(value)))
226
- switch (typeof value) {
227
- case 'undefined':
228
- return true
229
- case 'string':
230
- if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true
231
- break
232
- case 'boolean':
233
- if (!value) return true
234
- break
235
- case 'number':
236
- if (value === 0 || isNaN(value)) return true
237
- break
238
- case 'object':
239
- if (value === null || value.length === 0) return true
240
- for (const i in value) {
241
- return false
242
- }
243
- return true
244
- }
245
- return false
246
- }
247
-
248
- /**
249
- * 是否json字符串
250
- */
251
- export function jsonString(value) {
252
- if (typeof value === 'string') {
253
- try {
254
- const obj = JSON.parse(value)
255
- if (typeof obj === 'object' && obj) {
256
- return true
257
- }
258
- return false
259
- } catch (e) {
260
- return false
261
- }
262
- }
263
- return false
264
- }
265
-
266
- /**
267
- * 是否数组
268
- */
269
- export function array(value) {
270
- if (typeof Array.isArray === 'function') {
271
- return Array.isArray(value)
272
- }
273
- return Object.prototype.toString.call(value) === '[object Array]'
274
- }
275
-
276
- /**
277
- * 是否对象
278
- */
279
- export function object(value) {
280
- return Object.prototype.toString.call(value) === '[object Object]'
281
- }
282
-
283
147
  /**
284
148
  * 是否短信验证码
285
149
  */
@@ -287,45 +151,3 @@ export function code(value, len = 6) {
287
151
  return new RegExp(`^\\d{${len}}$`).test(value)
288
152
  }
289
153
 
290
- /**
291
- * 是否函数方法
292
- * @param {Object} value
293
- */
294
- export function func(value) {
295
- return typeof value === 'function'
296
- }
297
-
298
- /**
299
- * 是否promise对象
300
- * @param {Object} value
301
- */
302
- export function promise(value) {
303
- return object(value) && func(value.then) && func(value.catch)
304
- }
305
-
306
- /** 是否图片格式
307
- * @param {Object} value
308
- */
309
- export function image(value) {
310
- const newValue = value.split('?')[0]
311
- const IMAGE_REGEXP = /\.(jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg)/i
312
- return IMAGE_REGEXP.test(newValue)
313
- }
314
-
315
- /**
316
- * 是否视频格式
317
- * @param {Object} value
318
- */
319
- export function video(value) {
320
- const VIDEO_REGEXP = /\.(mp4|mpg|mpeg|dat|asf|avi|rm|rmvb|mov|wmv|flv|mkv|m3u8)/i
321
- return VIDEO_REGEXP.test(value)
322
- }
323
-
324
- /**
325
- * 是否为正则对象
326
- * @param {Object}
327
- * @return {Boolean}
328
- */
329
- export function regExp(o) {
330
- return o && Object.prototype.toString.call(o) === '[object RegExp]'
331
- }
package/src/tool.js CHANGED
@@ -19,26 +19,55 @@ export function debounce(time = 500) {
19
19
  })
20
20
  }
21
21
 
22
-
23
- let wait = null
24
22
  /**
25
23
  * @description 节流
26
24
  * @param {number} time 每time秒调用一次 默认0.5秒
25
+ * @param {boolean} immediately 是否立即执行一次 默认true
27
26
  * @example
28
- * throttle(time).then(res => {
27
+ * throttle(time,immediately).then(res => {
29
28
  * 执行方法
30
29
  * })
31
30
  */
32
- export function throttle(time = 500) { //防抖 // 默认0.2秒后执行事件
31
+
32
+ let wait = null;
33
+ export function throttle(time = 500, immediately=true) {
33
34
  return new Promise((resolve) => {
34
- if (wait) { return }
35
- wait = setTimeout(() => {
36
- resolve(true)
37
- wait = null
38
- }, time)
39
- })
35
+ if (immediately && (!wait || Date.now() - wait >= time)) {
36
+ resolve(true);
37
+ wait = Date.now();
38
+ setTimeout(() => {
39
+ wait = null;
40
+ }, time);
41
+ }
42
+ if (!immediately && !wait) {
43
+ wait = setTimeout(() => {
44
+ resolve(true);
45
+ wait = null;
46
+ }, time);
47
+ }
48
+ });
40
49
  }
41
50
 
51
+
52
+ // /**
53
+ // * @description 节流
54
+ // * @param {number} time 每time秒调用一次 默认0.5秒
55
+ // * @example
56
+ // * throttle(time).then(res => {
57
+ // * 执行方法
58
+ // * })
59
+ // */
60
+ // let wait = null
61
+ // export function throttle(time = 500) { //防抖 // 默认0.2秒后执行事件
62
+ // return new Promise((resolve) => {
63
+ // if (wait) { return }
64
+ // wait = setTimeout(() => {
65
+ // resolve(true)
66
+ // wait = null
67
+ // }, time)
68
+ // })
69
+ // }
70
+
42
71
  /**
43
72
  * @description 进行阻塞延时,以达到可以简写代码的目的
44
73
  * @param {number} value 堵塞时间 单位ms 毫秒