common-utils-kit 1.1.5 → 1.1.19

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/src/format.js DELETED
@@ -1,246 +0,0 @@
1
- /**
2
- * 获取对象属性值,支持可选链操作符的形式
3
- * @param {Object} obj - 要获取属性值的对象
4
- * @param {string} key - 属性路径,可以包含点号('.')表示深层嵌套属性
5
- * @returns {*} - 返回属性值,如果属性不存在则返回 undefined
6
- * @example
7
- * const data = {
8
- * parent: {
9
- * child: {
10
- * value: 'Hello, World!'
11
- * }
12
- * }
13
- * };
14
- * const nestedValue = getProperty(data, 'parent.child.value');
15
- * console.log(nestedValue); // 输出: Hello, World!
16
- */
17
- export function getProperty(obj, key) {
18
- if (!obj) { return }
19
- if (typeof key !== 'string' || key === '') {
20
- return ''
21
- }
22
- if (key.indexOf('.') !== -1) {
23
- const keys = key.split('.')
24
- let firstObj = obj[keys[0]] || {}
25
- for (let i = 1; i < keys.length; i++) {
26
- if (firstObj) {
27
- firstObj = firstObj[keys[i]]
28
- }
29
- }
30
- return firstObj
31
- }
32
- return obj[key]
33
- }
34
-
35
- /**
36
- * @description 数组对象去重
37
- * @param {array} data 需要去重的原数组对象
38
- * @param {string} parameter 去重数组的唯一码(关键字)
39
- * @returns {Array} - 返回去重后的数组对象
40
- * @example
41
- * // 使用示例
42
- * const data = [{ id: 1, name: 'John' },{ id: 2, name: 'Jane' },{ id: 1, name: 'John' },{ id: 3, name: 'Doe' }];
43
- * const uniqueData = unrepeated(data, 'id');
44
- * console.log(uniqueData); // 输出: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Doe' }]
45
- */
46
- export function unrepeated(data, parameter) {
47
- var temp = {};
48
- var tempData = JSON.parse(JSON.stringify(data)); // 深拷贝原来的数据
49
- var newData = tempData.reduce((pre, cur) => {
50
- if (!temp[cur[parameter]] && cur[parameter]) {
51
- temp[cur[parameter]] = true;
52
- pre.push(cur);
53
- }
54
- return pre;
55
- }, []);
56
- return newData;
57
- }
58
-
59
- /**
60
- * @description 将树形数据转化为一维数组
61
- * @param {Array} previous - 树形结构数据
62
- * @param {string} bind - 树形结构中下级节点的绑定属性,默认为 'children'
63
- * @returns {Array} - 一维数组
64
- * @example
65
- * const treeData = [{ id: 1,
66
- * label: 'parent 1',
67
- * children: [{ id: 2, label: 'child 1' },{ id: 3, label: 'child 2' }]
68
- * },{id: 4,
69
- * label: 'parent 2',
70
- * children: [{ id: 5, label: 'child 3' },{ id: 6, label: 'child 4' }]
71
- * }];
72
- * const flatArray = treeToFlat(treeData,'children');
73
- * console.log(flatArray);
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
- */
76
- export function treeToFlat(previous=[], bind = 'children') {
77
- const result = []
78
- previous.forEach(item => {
79
- const clonedItem = { ...item };
80
- if (clonedItem[bind]) {
81
- result.push(clonedItem)
82
- if (Array.isArray(previous) && clonedItem[bind].length > 0) {
83
- result.push(...treeToFlat(clonedItem[bind]))
84
- }
85
- } else {
86
- result.push(clonedItem)
87
- }
88
- })
89
- return result
90
- }
91
-
92
- /**
93
- * @description 将一维数组转树形结构
94
- * @param {Array} flatArray - 树形结构数据
95
- * @param {string} [rootValue=null] - 树形结构一级父 id,默认为 null
96
- * @param {string} [bind="pid"] - 父 id 的属性,默认为 "pid"
97
- * @returns {Array} - 树形结构
98
- * @example
99
- * const flatArray = [
100
- * { id: 1, label: 'Node 1', pid: null },
101
- * { id: 2, label: 'Node 2', pid: 1 },
102
- * { id: 3, label: 'Node 3', pid: 1 },
103
- * { id: 4, label: 'Node 4', pid: 2 },
104
- * { id: 5, label: 'Node 5', pid: 2 },
105
- * { id: 6, label: 'Node 6', pid: 3 },
106
- * { id: 7, label: 'Node 7', pid: null },
107
- * { id: 8, label: 'Node 8', pid: 3 },
108
- * ];
109
- * const treeData = flatToTree(flatArray);
110
- * console.log(treeData);
111
- * // 输出: [
112
- * // {
113
- * // id: 1,
114
- * // label: 'Node 1',
115
- * // pid: null,
116
- * // children: [
117
- * // { id: 2, label: 'Node 2', pid: 1, children: [ { id: 4, label: 'Node 4', pid: 2 }, { id: 5, label: 'Node 5', pid: 2 } ] },
118
- * // { id: 3, label: 'Node 3', pid: 1, children: [ { id: 6, label: 'Node 6', pid: 3, children: [ { id: 8, label: 'Node 8', pid: 6 } ] } ] },
119
- * // ],
120
- * // },
121
- * // { id: 7, label: 'Node 7', pid: null },
122
- * // ]
123
- */
124
- export function flatToTree(flatArray, rootValue = null, bind = "pid") {
125
- const tree = [];
126
- flatArray.forEach(item => {
127
- if (item[bind] === rootValue) {
128
- const children = flatToTree(flatArray, item.id);
129
- item.children = children && children.length ? children : []
130
- tree.push(item);
131
- }
132
- });
133
- return tree.length > 0 ? tree : null; // 处理不存在的根节点
134
- }
135
-
136
- /**
137
- * 递归封装
138
- * @param {Array} data - 待处理的数据
139
- * @param {Function} funName - 处理每个节点的方法
140
- * @param {string} [bind="children"] - 子节点绑定属性的名称,默认为 "children"
141
- * @returns {Array} - 处理后的树形结构数据
142
- * @example
143
- * // 数据结构
144
- * const treeData = [{ id: 1,
145
- * label: 'parent 1',
146
- * children: [{ id: 2, label: 'child 1' },{ id: 3, label: 'child 2' }]
147
- * },{id: 4,
148
- * label: 'parent 2',
149
- * children: [{ id: 5, label: 'child 3' },{ id: 6, label: 'child 4' }]
150
- * }];
151
- *
152
- * // 处理逻辑函数,
153
- * function processFunction(node) {
154
- * return { label: node.label.toUpperCase() };
155
- * }
156
- * // 使用递归封装方法
157
- * const processedTree = recursionFunction(treeData, processFunction);
158
- */
159
- export function recursionFunction(data, processFunction, bind = 'children') {
160
- function internalRecursion(previous=[], initial) {
161
- return previous.map((element, i) => {
162
- const menus = initial[i] ? processFunction(initial[i]) : {};
163
- const children = element[bind] && Array.isArray(element[bind]) && element[bind].length > 0
164
- ? internalRecursion(element[bind], initial[i][bind])
165
- : [];
166
- return { ...menus,[bind]: children };
167
- });
168
- }
169
- return internalRecursion(data, data);
170
- }
171
-
172
- /**
173
- * 解析给定函数中switch case语句中的数据并返回提取出值的数组对象。
174
- * @param {function} filterMethod - 包含带有条件和标签的 case 的函数。
175
- * @returns {Array} - 包含提取出的值和标签的对象数组。
176
- * @example
177
- * const filterMethod = (item) => {
178
- * switch (item) {
179
- * case 1://若是字符串请使用单引号
180
- * return "是";
181
- * case 2://若是字符串请使用单引号
182
- * return "否";
183
- * default:
184
- * return "数据异常";
185
- * }
186
- * };
187
- * 使用
188
- * filterData(filterMethod)//[{label:"是",value:1},{label:"否",value:2}]
189
- */
190
- export function filterData(filterMethod) {
191
- const funStrArr = filterMethod.toString().split("case");
192
- if (funStrArr.length < 1) return []
193
- const list = [];
194
- for (let i = 1; i < funStrArr.length; i++) {
195
- const condition = funStrArr[i].split(":")[0].replace(/\s*/g, "").replace(/(\"*)/g, "");
196
- const value = JSON.parse(condition.replace(/'/g, '"'))
197
- const label = funStrArr[i].split("return")[1].split(";")[0].replace(/\s*/g, "").replace(/(\"*)/g, "");
198
- if (value || value === 0 || value === false) {
199
- list.push({ value, label });
200
- }
201
- }
202
- return list;
203
- }
204
- /**
205
- * @description 计算时间差(单位:天)
206
- * @param {Date} startDate 开始时间 默认当前时间
207
- * @param {Date} endDate 结束时间 默认当前时间
208
- * @returns {number} 时间差(天)
209
- * @example DateDiff(开始时间,结束时间)
210
- */
211
- export function dateDiff(startDate = new Date(), endDate = new Date()) {
212
- const MS_PER_DAY = 24 * 60 * 60 * 1000; // 一天的毫秒数
213
- const startTimestamp = new Date(startDate).setHours(0, 0, 0, 0);
214
- const endTimestamp = new Date(endDate).setHours(0, 0, 0, 0);
215
- const timeDiff = endTimestamp - startTimestamp;
216
- const daysDiff = Math.floor(timeDiff / MS_PER_DAY);
217
- return daysDiff;
218
- }
219
- /**
220
- * @description 获取当前时间(年月日)
221
- * @returns {string} 当前时间字符串,格式为 "YYYY-MM-DD"
222
- * @example getNowDate()
223
- */
224
- export function getNowDate() {
225
- const timeOne = new Date();
226
- const year = timeOne.getFullYear();
227
- const month = String(timeOne.getMonth() + 1).padStart(2, '0');
228
- const day = String(timeOne.getDate()).padStart(2, '0');
229
- return `${year}-${month}-${day}`;
230
- }
231
-
232
- /**
233
- * @description form的下拉框 级联选择器等数据赋值
234
- * @param {Array} columns - form的columns数据
235
- * @param {String} targetLabel - 需要赋值的columns[i]的label
236
- * @param {Array} newData - 想要赋值的数据
237
- * @returns {Array} 包含data的Columns数据
238
- * @example setFormColumnsData(this.columns, '下单医生', result.data)
239
- */
240
- export function setFormColumnsData(columns, targetLabel, newData) {
241
- var targetIndex = columns.findIndex(item => item.label === targetLabel);
242
- if (targetIndex !== -1) {
243
- columns[targetIndex].data = newData;
244
- }
245
- return columns;
246
- }
package/src/test.js DELETED
@@ -1,153 +0,0 @@
1
-
2
- /**
3
- * 验证是否有值,除了 0、undefined、''、false、null、{}、[] 为空,其他都属于有值
4
- * @param {*} value - 要验证的值
5
- * @param {string} type - 类型检查(可选),当类型为 'number' 时会将 0 视为有值
6
- * @returns {boolean} - 如果有值返回 true,否则返回 false
7
- * @example
8
- * // 使用示例
9
- * console.log(hasValue(' Hello ')); // 输出: true
10
- * console.log(hasValue(0, 'number'),hasValue(0)); // 传入类型number,0也是有值 输出: true false
11
- * console.log(hasValue(true)); // 输出: true
12
- * console.log(hasValue([1, 2, 3]),hasValue([])); // 输出: true false
13
- * console.log(hasValue({ key: 'value' }),hasValue({})); // 输出: true false
14
- * console.log(hasValue(undefined)); // 输出: false
15
- * console.log(hasValue(null)); // 输出: false
16
- */
17
- export function hasValue(value, type) {
18
- switch (typeof value) {
19
- case 'undefined':
20
- return false;
21
- case 'string':
22
- return (type === 'string' || !type) && value.trim().length > 0;
23
- case 'boolean':
24
- return (type === 'boolean' || !type) && !!value;
25
- case 'number':
26
- return (type === 'number' || !type) && ((type === 'number' || value !== 0) && !isNaN(value));
27
- case 'object':
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;
31
- }
32
- return false;
33
- }
34
-
35
- /**
36
- * 获取值的数据类型
37
- * @param {*} value - 要获取类型的值
38
- * @returns {string} - 返回值的数据类型,包括 'string'、'number'、'boolean'、'function'、'array'、'object'、'null' 等
39
- * @example
40
- * // 使用示例
41
- * console.log(valueType('Hello')); // 输出: 'string'
42
- * console.log(valueType(42)); // 输出: 'number'
43
- * console.log(valueType(true)); // 输出: 'boolean'
44
- * console.log(valueType(function () {})); // 输出: 'function'
45
- * console.log(valueType([1, 2, 3])); // 输出: 'array'
46
- * console.log(valueType({ key: 'value' })); // 输出: 'object'
47
- * console.log(valueType(null)); // 输出: 'null'
48
- */
49
- export function valueType(value) {
50
- const type = typeof value;
51
- switch (type) {
52
- case 'string':
53
- case 'number':
54
- case 'boolean':
55
- case 'function':
56
- return type;
57
- case 'object':
58
- if (value === null) {
59
- return 'null';
60
- } else if (Array.isArray(value)) {
61
- return 'array';
62
- } else if (Object.prototype.toString.call(value) === '[object Object]') {
63
- return 'object';
64
- }
65
- break;
66
- }
67
- }
68
-
69
- /**
70
- * 验证电子邮箱格式
71
- */
72
- export function email(value) {
73
- return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value)
74
- }
75
-
76
- /**
77
- * 验证手机格式
78
- */
79
- export function mobile(value) {
80
- return /^1[23456789]\d{9}$/.test(value)
81
- }
82
-
83
- /**
84
- * 验证URL格式
85
- */
86
- export function url(value) {
87
- 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_!~*'().;?:@&=+$,%#-]+)+\/?)$/
88
- .test(value)
89
- }
90
-
91
- /**
92
- * 验证身份证号码
93
- */
94
- export function idCard(value) {
95
- 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(
96
- value
97
- )
98
- }
99
-
100
- /**
101
- * 是否车牌号
102
- */
103
- export function carNo(value) {
104
- // 新能源车牌
105
- const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
106
- // 旧车牌
107
- const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
108
- if (value.length === 7) {
109
- return creg.test(value)
110
- } if (value.length === 8) {
111
- return xreg.test(value)
112
- }
113
- return false
114
- }
115
-
116
- /**
117
- * 金额,只允许2位小数
118
- */
119
- export function amount(value) {
120
- // 金额,只允许保留两位小数
121
- return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value)
122
- }
123
-
124
- /**
125
- * 中文
126
- */
127
- export function chinese(value) {
128
- const reg = /^[\u4e00-\u9fa5]+$/gi
129
- return reg.test(value)
130
- }
131
-
132
- /**
133
- * 只能输入字母
134
- */
135
- export function letter(value) {
136
- return /^[a-zA-Z]*$/.test(value)
137
- }
138
-
139
- /**
140
- * 是否固定电话
141
- */
142
- export function landline(value) {
143
- const reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/
144
- return reg.test(value)
145
- }
146
-
147
- /**
148
- * 是否短信验证码
149
- */
150
- export function code(value, len = 6) {
151
- return new RegExp(`^\\d{${len}}$`).test(value)
152
- }
153
-
package/src/tool.js DELETED
@@ -1,84 +0,0 @@
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
- * @description 节流
24
- * @param {number} time 每time秒调用一次 默认0.5秒
25
- * @param {boolean} immediately 是否立即执行一次 默认true
26
- * @example
27
- * throttle(time,immediately).then(res => {
28
- * 执行方法
29
- * })
30
- */
31
-
32
- let wait = null;
33
- export function throttle(time = 500, immediately=true) {
34
- return new Promise((resolve) => {
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
- });
49
- }
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
-
71
- /**
72
- * @description 进行阻塞延时,以达到可以简写代码的目的
73
- * @param {number} value 堵塞时间 单位ms 毫秒
74
- * @returns {Promise} 返回promise
75
- * @example
76
- * await sleep(1000);
77
- */
78
- export function sleep(value = 100) {
79
- return new Promise((resolve) => {
80
- setTimeout(() => {
81
- resolve()
82
- }, value)
83
- })
84
- }