common-utils-kit 1.1.1 → 1.1.3
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 +18 -8
- package/package.json +1 -1
- package/src/directives.js +26 -3
- package/src/format.js +47 -4
package/README.md
CHANGED
|
@@ -7,12 +7,22 @@
|
|
|
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. 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
|
+
``` -->
|
package/package.json
CHANGED
package/src/directives.js
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
// 自定义指令基础配置
|
|
3
3
|
const directives = {
|
|
4
|
+
debounce: { // 按钮或者输入框防抖
|
|
5
|
+
// 例子 <el-input v-model="aa" v-debounce="[reset,`input`,1000]" placeholder="输入框数组防抖" />
|
|
6
|
+
// <el-button v-debounce="[reset,`click`,1000]">刷新</el-button>
|
|
7
|
+
inserted: function(el, binding) {
|
|
8
|
+
const [fn, event = "click", time = 1000] = binding.value;
|
|
9
|
+
let timer;
|
|
10
|
+
let flag = true; // 输入法标记
|
|
11
|
+
if (event === "input") {
|
|
12
|
+
el.addEventListener("compositionstart", () => { // 监听中文输入开始
|
|
13
|
+
flag = false;
|
|
14
|
+
});
|
|
15
|
+
el.addEventListener("compositionend", () => { // 监听中文输入结束
|
|
16
|
+
flag = true;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
el.addEventListener(event, () => {
|
|
20
|
+
timer && clearTimeout(timer);
|
|
21
|
+
timer = setTimeout(() => {
|
|
22
|
+
if (flag) fn();
|
|
23
|
+
}, time);
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
},
|
|
4
27
|
inputDebounce: { // 输入框防抖
|
|
5
28
|
inserted: function (el, binding) {
|
|
6
29
|
const fn = binding.value;
|
|
@@ -117,7 +140,7 @@ const directives = {
|
|
|
117
140
|
delete el.__timeout__
|
|
118
141
|
}
|
|
119
142
|
},
|
|
120
|
-
|
|
143
|
+
replace: {//限制input输入内容
|
|
121
144
|
inserted: function (el, binding, vnode) {
|
|
122
145
|
const element = el.tagName === "INPUT" ? el : el.querySelector("input");
|
|
123
146
|
let flag = true; // 输入法标记
|
|
@@ -134,7 +157,7 @@ const directives = {
|
|
|
134
157
|
case "price": // 正负的两位小数金额 -和.只能出现一次 并且-只能在首位
|
|
135
158
|
element.value = element.value.replace(/^([-+])?\D*(\d*(?:\.\d{0,2})?).*$/, "$1$2");
|
|
136
159
|
break;
|
|
137
|
-
case "
|
|
160
|
+
case "plusPrice": // 验证正的两位小数金额
|
|
138
161
|
element.value = element.value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, "$1");
|
|
139
162
|
break;
|
|
140
163
|
case "num": // 只能输入正整数
|
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
|
|
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
|
+
}
|