common-utils-kit 1.1.2 → 1.1.4

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
@@ -7,12 +7,49 @@
7
7
  ```
8
8
  # 安装
9
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 //工具函数
16
- files //文件下载
17
- ```
18
10
 
11
+ # 使用方式一(局部引入)
12
+ import commonUtilsKit from "common-utils-kit";
13
+ 例:验证是否为数字 commonUtilsKit.test.number(1)
14
+
15
+ # 使用方式二(全局引入)
16
+ import commonUtilsKit from "common-utils-kit";
17
+ Vue.prototype.$test = commonUtilsKit.test;
18
+ 例:验证是否为数字 this.$test.number(1)
19
+ ```
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.2",
3
+ "version": "1.1.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/format.js CHANGED
@@ -73,13 +73,13 @@ export function unrepeated(data, parameter) {
73
73
  * console.log(flatArray);
74
74
  * // 输出:[ { id: 1, label: 'parent 1' },{ id: 2, label: 'child 1' }, { id: 3, label: 'child 2' }, { id: 4, label: 'parent 2' },{ id: 5, label: 'child 3' },{ id: 6, label: 'child 4' }]
75
75
  */
76
- export function treeToFlat(previous, bind = 'children') {
76
+ export function treeToFlat(previous=[], bind = 'children') {
77
77
  const result = []
78
78
  previous.forEach(item => {
79
79
  const clonedItem = { ...item };
80
80
  if (clonedItem[bind]) {
81
81
  result.push(clonedItem)
82
- if (clonedItem[bind].length > 0) {
82
+ if (Array.isArray(previous) && clonedItem[bind].length > 0) {
83
83
  result.push(...treeToFlat(clonedItem[bind]))
84
84
  }
85
85
  } else {
@@ -159,13 +159,13 @@ export function flatToTree(flatArray, rootValue = null, bind = "pid") {
159
159
  * const processedTree = recursionFunction(treeData, processFunction);
160
160
  */
161
161
  export function recursionFunction(data, processFunction, bind = 'children') {
162
- function internalRecursion(previous, initial) {
162
+ function internalRecursion(previous=[], initial) {
163
163
  return previous.map((element, i) => {
164
164
  const menus = initial[i] ? processFunction(initial[i]) : {};
165
165
  const children = element[bind] && Array.isArray(element[bind]) && element[bind].length > 0
166
166
  ? internalRecursion(element[bind], initial[i][bind])
167
167
  : [];
168
- return { [bind]: children, ...menus };
168
+ return { ...menus,[bind]: children };
169
169
  });
170
170
  }
171
171
  return internalRecursion(data, data);
@@ -203,3 +203,46 @@ export function filterData(filterMethod) {
203
203
  }
204
204
  return list;
205
205
  }
206
+ /**
207
+ * @description 计算时间差(单位:天)
208
+ * @param {Date} startDate 开始时间 默认当前时间
209
+ * @param {Date} endDate 结束时间 默认当前时间
210
+ * @returns {number} 时间差(天)
211
+ * @example DateDiff(开始时间,结束时间)
212
+ */
213
+ export function dateDiff(startDate = new Date(), endDate = new Date()) {
214
+ const MS_PER_DAY = 24 * 60 * 60 * 1000; // 一天的毫秒数
215
+ const startTimestamp = new Date(startDate).setHours(0, 0, 0, 0);
216
+ const endTimestamp = new Date(endDate).setHours(0, 0, 0, 0);
217
+ const timeDiff = endTimestamp - startTimestamp;
218
+ const daysDiff = Math.floor(timeDiff / MS_PER_DAY);
219
+ return daysDiff;
220
+ }
221
+ /**
222
+ * @description 获取当前时间(年月日)
223
+ * @returns {string} 当前时间字符串,格式为 "YYYY-MM-DD"
224
+ * @example getNowDate()
225
+ */
226
+ export function getNowDate() {
227
+ const timeOne = new Date();
228
+ const year = timeOne.getFullYear();
229
+ const month = String(timeOne.getMonth() + 1).padStart(2, '0');
230
+ const day = String(timeOne.getDate()).padStart(2, '0');
231
+ return `${year}-${month}-${day}`;
232
+ }
233
+
234
+ /**
235
+ * @description form的下拉框 级联选择器等数据赋值
236
+ * @param {Array} columns - form的columns数据
237
+ * @param {String} targetLabel - 需要赋值的columns[i]的label
238
+ * @param {Array} newData - 想要赋值的数据
239
+ * @returns {Array} 包含data的Columns数据
240
+ * @example setFormColumnsData(this.columns, '下单医生', result.data)
241
+ */
242
+ export function setFormColumnsData(columns, targetLabel, newData) {
243
+ var targetIndex = columns.findIndex(item => item.label === targetLabel);
244
+ if (targetIndex !== -1) {
245
+ columns[targetIndex].data = newData;
246
+ }
247
+ return columns;
248
+ }
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
- }