@oeos-components/utils 0.0.4 → 0.0.6
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/dist/index.cjs +55 -24
- package/dist/index.d.cts +55 -7
- package/dist/index.d.mts +55 -7
- package/dist/index.d.ts +55 -7
- package/dist/index.mjs +55 -25
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -15,21 +15,27 @@ const isStringNumber = (val) => {
|
|
|
15
15
|
};
|
|
16
16
|
const isNumber = (val) => typeof val === "number";
|
|
17
17
|
function $toast(message, type = "success", otherParams = {}) {
|
|
18
|
-
const
|
|
18
|
+
const typeMap = {
|
|
19
19
|
s: "success",
|
|
20
20
|
i: "info",
|
|
21
21
|
e: "error",
|
|
22
22
|
w: "warning"
|
|
23
23
|
};
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
function isShortType(t) {
|
|
25
|
+
return ["s", "i", "e", "w"].includes(t);
|
|
26
|
+
}
|
|
27
|
+
function isToastOptions(obj) {
|
|
28
|
+
return typeof obj === "object" && obj !== null;
|
|
29
|
+
}
|
|
30
|
+
if (isToastOptions(message)) {
|
|
31
|
+
if (message.closeAll) {
|
|
26
32
|
elementPlus.ElMessage.closeAll();
|
|
27
33
|
}
|
|
28
34
|
elementPlus.ElMessage(message);
|
|
29
35
|
return;
|
|
30
36
|
}
|
|
31
|
-
if (
|
|
32
|
-
if (type.
|
|
37
|
+
if (isToastOptions(type)) {
|
|
38
|
+
if (type.closeAll) {
|
|
33
39
|
elementPlus.ElMessage.closeAll();
|
|
34
40
|
}
|
|
35
41
|
elementPlus.ElMessage({
|
|
@@ -42,9 +48,10 @@ function $toast(message, type = "success", otherParams = {}) {
|
|
|
42
48
|
if (otherParams.closeAll) {
|
|
43
49
|
elementPlus.ElMessage.closeAll();
|
|
44
50
|
}
|
|
51
|
+
const resolvedType = isShortType(type) ? typeMap[type] : type;
|
|
45
52
|
elementPlus.ElMessage({
|
|
46
53
|
message,
|
|
47
|
-
type:
|
|
54
|
+
type: resolvedType,
|
|
48
55
|
...otherParams
|
|
49
56
|
});
|
|
50
57
|
}
|
|
@@ -152,32 +159,38 @@ function validForm(ref, { message = "\u8868\u5355\u6821\u9A8C\u9519\u8BEF, \u8BF
|
|
|
152
159
|
});
|
|
153
160
|
});
|
|
154
161
|
}
|
|
155
|
-
function isEmpty(data) {
|
|
162
|
+
function isEmpty(data, strict = false) {
|
|
156
163
|
if (reactivity.isRef(data)) {
|
|
157
164
|
data = reactivity.unref(data);
|
|
158
165
|
}
|
|
166
|
+
if (strict) {
|
|
167
|
+
if (data === false || data === 0 || data === BigInt(0)) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (data == null) return true;
|
|
159
172
|
if (data instanceof Date) {
|
|
160
173
|
return isNaN(data.getTime());
|
|
161
174
|
}
|
|
162
175
|
switch (typeof data) {
|
|
163
|
-
case "undefined":
|
|
164
|
-
return true;
|
|
165
176
|
case "string":
|
|
166
|
-
|
|
167
|
-
break;
|
|
177
|
+
return data.trim().length === 0;
|
|
168
178
|
case "boolean":
|
|
169
|
-
|
|
170
|
-
break;
|
|
179
|
+
return !data;
|
|
171
180
|
case "number":
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
case "
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
return 0 === data || isNaN(data);
|
|
182
|
+
// ❗ `NaN`或者0 被认为是空
|
|
183
|
+
case "symbol":
|
|
184
|
+
return false;
|
|
185
|
+
case "bigint":
|
|
186
|
+
return data === BigInt(0);
|
|
187
|
+
}
|
|
188
|
+
if (data instanceof Map || data instanceof Set) return data.size === 0;
|
|
189
|
+
if (Array.isArray(data) || typeof data.length === "number" && Object.prototype.toString.call(data) === "[object Object]") {
|
|
190
|
+
return data.length === 0;
|
|
191
|
+
}
|
|
192
|
+
if (typeof data === "object") {
|
|
193
|
+
return Object.keys(data).length === 0;
|
|
181
194
|
}
|
|
182
195
|
return false;
|
|
183
196
|
}
|
|
@@ -243,7 +256,7 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
|
243
256
|
});
|
|
244
257
|
return time_str;
|
|
245
258
|
}
|
|
246
|
-
function formatDurationTime(timestamp, cFormat = "{d}
|
|
259
|
+
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
247
260
|
const secondsPerMinute = 60;
|
|
248
261
|
const minutesPerHour = 60;
|
|
249
262
|
const hoursPerDay = 24;
|
|
@@ -692,7 +705,7 @@ function formatBytesConvert(oBytes, { thounsand = false, toFixed: toFixed2 = 0 }
|
|
|
692
705
|
}
|
|
693
706
|
let finalRes = size * units[unit];
|
|
694
707
|
if (toFixed2) {
|
|
695
|
-
finalRes =
|
|
708
|
+
finalRes = Number(finalRes).toFixed(toFixed2);
|
|
696
709
|
}
|
|
697
710
|
if (thounsand) {
|
|
698
711
|
finalRes = formatThousands(finalRes);
|
|
@@ -718,6 +731,23 @@ function throttle(fn, delay = 1e3) {
|
|
|
718
731
|
}
|
|
719
732
|
};
|
|
720
733
|
}
|
|
734
|
+
function tryCatch(promise, sendLoading) {
|
|
735
|
+
const updateLoading = (value) => {
|
|
736
|
+
if (reactivity.isRef(sendLoading)) {
|
|
737
|
+
sendLoading.value = value;
|
|
738
|
+
} else if (sendLoading !== null) {
|
|
739
|
+
console.warn("Cannot modify non-ref sendLoading directly!");
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
updateLoading(true);
|
|
743
|
+
return promise.then((data) => {
|
|
744
|
+
updateLoading(false);
|
|
745
|
+
return { data, error: null };
|
|
746
|
+
}).catch((error) => {
|
|
747
|
+
updateLoading(false);
|
|
748
|
+
return { data: null, error };
|
|
749
|
+
});
|
|
750
|
+
}
|
|
721
751
|
function debounce(fn, delay = 1e3) {
|
|
722
752
|
let timer = null;
|
|
723
753
|
return function() {
|
|
@@ -790,6 +820,7 @@ exports.sleep = sleep;
|
|
|
790
820
|
exports.throttle = throttle;
|
|
791
821
|
exports.toFixed = toFixed;
|
|
792
822
|
exports.toLine = toLine;
|
|
823
|
+
exports.tryCatch = tryCatch;
|
|
793
824
|
exports.uuid = uuid;
|
|
794
825
|
exports.validForm = validForm;
|
|
795
826
|
exports.validate = validate;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import * as element_plus from 'element-plus';
|
|
2
|
+
import { ElMessageOptions } from 'element-plus';
|
|
3
|
+
import { Ref } from '@vue/reactivity';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* 现有方法如下
|
|
@@ -37,15 +39,18 @@ declare const isString: (val: any) => val is string;
|
|
|
37
39
|
declare const isStringNumber: (val: string) => boolean;
|
|
38
40
|
declare const isNumber: (val: any) => val is number;
|
|
39
41
|
/**
|
|
40
|
-
* @
|
|
41
|
-
proxy.$toast('保存成功')
|
|
42
|
+
* @example1
|
|
43
|
+
proxy.$toast('保存成功') // s:success; i: info; w: warning; e: error;
|
|
42
44
|
proxy.$toast('保存失败', 'e')
|
|
45
|
+
proxy.$toast('永不关闭', {duration: 0})
|
|
43
46
|
proxy.$toast({
|
|
44
47
|
message: 'andy',
|
|
45
48
|
type: 'warning',
|
|
49
|
+
duration: 300,
|
|
50
|
+
closeAll: true,
|
|
46
51
|
})
|
|
47
52
|
* $toast.success('This is a success message')
|
|
48
|
-
* @
|
|
53
|
+
* @example2 显示对象
|
|
49
54
|
* $toast({
|
|
50
55
|
dangerouslyUseHTMLString: true,
|
|
51
56
|
message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
|
|
@@ -53,7 +58,13 @@ declare const isNumber: (val: any) => val is number;
|
|
|
53
58
|
duration: 5000,
|
|
54
59
|
})
|
|
55
60
|
*/
|
|
56
|
-
|
|
61
|
+
type MessageType = 'success' | 'info' | 'error' | 'warning';
|
|
62
|
+
type ShortType = 's' | 'i' | 'e' | 'w';
|
|
63
|
+
type ToastType = MessageType | ShortType;
|
|
64
|
+
type ToastOptions = Partial<ElMessageOptions> & {
|
|
65
|
+
closeAll?: boolean;
|
|
66
|
+
};
|
|
67
|
+
declare function $toast(message: string | ToastOptions, type?: ToastType | ToastOptions, otherParams?: ToastOptions): void;
|
|
57
68
|
declare namespace $toast {
|
|
58
69
|
var success: (message: any, otherParams?: {}) => void;
|
|
59
70
|
var info: (message: any, otherParams?: {}) => void;
|
|
@@ -73,6 +84,16 @@ declare function getStorage(data: any, isSession?: boolean): string | null;
|
|
|
73
84
|
* clearStorage({ exclude: ['loginId', 'token'] })
|
|
74
85
|
*/
|
|
75
86
|
declare function clearStorage(str?: string | [] | object): void;
|
|
87
|
+
/**
|
|
88
|
+
* element-plus的form表单使用promise进行封装
|
|
89
|
+
* @param ref
|
|
90
|
+
* @param param1
|
|
91
|
+
* @returns Promise
|
|
92
|
+
* await proxy.validForm(formRef);
|
|
93
|
+
* await proxy.validForm(formRef, {message: '自定义错误'});
|
|
94
|
+
* await proxy.validForm(formRef, {showMessage: false});
|
|
95
|
+
* await proxy.validForm(formRef, {detail: true});
|
|
96
|
+
*/
|
|
76
97
|
declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
77
98
|
message?: string | undefined;
|
|
78
99
|
detail?: boolean | undefined;
|
|
@@ -80,9 +101,9 @@ declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
|
80
101
|
}): Promise<unknown>;
|
|
81
102
|
/**
|
|
82
103
|
* 判断变量是否空值
|
|
83
|
-
* undefined, null, '', ' ', false, 0, [], {} 均返回true,否则返回false
|
|
104
|
+
* undefined, null, '', ' ', false, 0, [], {}, NaN, new Set(), new Map(), BigInt(0), new Date('无效日期') 均返回true,否则返回false
|
|
84
105
|
*/
|
|
85
|
-
declare function isEmpty(data: any): boolean;
|
|
106
|
+
declare function isEmpty(data: any, strict?: boolean): boolean;
|
|
86
107
|
declare function notEmpty(v: any): boolean;
|
|
87
108
|
/**
|
|
88
109
|
* 将两个对象合并, 以第二个对象为准, 如果两个对象, 一个属性有值, 一个没值, 合并后有值; 如果两个属性都有值, 以第二个属性为准
|
|
@@ -255,17 +276,44 @@ declare function toFixed(value: any, options?: {
|
|
|
255
276
|
/**
|
|
256
277
|
* 只有对正整数或者字符串正整数才进行单位的转换,
|
|
257
278
|
* 否则返回原始数据
|
|
279
|
+
* @example
|
|
280
|
+
* proxy.formatBytes(536870912) // 512MB
|
|
258
281
|
* proxy.formatBytes(536870912) // 512MB
|
|
259
282
|
*/
|
|
260
283
|
declare function formatBytes(bytes: any, { toFixed, thousands }?: {
|
|
261
284
|
toFixed?: number | undefined;
|
|
262
285
|
thousands?: boolean | undefined;
|
|
263
286
|
}): any;
|
|
287
|
+
/**
|
|
288
|
+
* 字节转数字
|
|
289
|
+
* @param oBytes
|
|
290
|
+
* @param param1
|
|
291
|
+
* @returns number
|
|
292
|
+
* formatBytesConvert('0.5GB') 536870912
|
|
293
|
+
* formatBytesConvert('1,234 GB') 1324997410816
|
|
294
|
+
* formatBytesConvert('1,234 GB', {thousand: true}) 1,324,997,410,816
|
|
295
|
+
* formatBytesConvert('1,234 GB', {toFixed: 2}) 1324997410816.00
|
|
296
|
+
*/
|
|
264
297
|
declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
|
|
265
298
|
thounsand?: boolean | undefined;
|
|
266
299
|
toFixed?: number | undefined;
|
|
267
300
|
}): any;
|
|
268
301
|
declare function throttle(fn: any, delay?: number): () => void;
|
|
302
|
+
/**
|
|
303
|
+
* 封装 Promise 执行,提供自动 loading 状态管理
|
|
304
|
+
* @param promise 需要执行的 Promise
|
|
305
|
+
* @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
|
|
306
|
+
* @returns Promise<{ data: T | null; error: any }>
|
|
307
|
+
* @example1
|
|
308
|
+
* const loading = ref(false);
|
|
309
|
+
* const { data, error } = await tryCatch(fetchUserData(), loading);
|
|
310
|
+
* @example2 // 无视 loading 状态
|
|
311
|
+
* const { data, error } = await tryCatch(fetchUserData());
|
|
312
|
+
*/
|
|
313
|
+
declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
|
|
314
|
+
data: T | null;
|
|
315
|
+
error: any;
|
|
316
|
+
}>;
|
|
269
317
|
declare function debounce(fn: any, delay?: number): () => void;
|
|
270
318
|
/**
|
|
271
319
|
* proxy.confirm('确定删除吗?')
|
|
@@ -312,4 +360,4 @@ declare function formatNewLines(str: any): any;
|
|
|
312
360
|
* */
|
|
313
361
|
declare function getVariable(propertyName: any): string;
|
|
314
362
|
|
|
315
|
-
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, uuid, validForm, validate };
|
|
363
|
+
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import * as element_plus from 'element-plus';
|
|
2
|
+
import { ElMessageOptions } from 'element-plus';
|
|
3
|
+
import { Ref } from '@vue/reactivity';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* 现有方法如下
|
|
@@ -37,15 +39,18 @@ declare const isString: (val: any) => val is string;
|
|
|
37
39
|
declare const isStringNumber: (val: string) => boolean;
|
|
38
40
|
declare const isNumber: (val: any) => val is number;
|
|
39
41
|
/**
|
|
40
|
-
* @
|
|
41
|
-
proxy.$toast('保存成功')
|
|
42
|
+
* @example1
|
|
43
|
+
proxy.$toast('保存成功') // s:success; i: info; w: warning; e: error;
|
|
42
44
|
proxy.$toast('保存失败', 'e')
|
|
45
|
+
proxy.$toast('永不关闭', {duration: 0})
|
|
43
46
|
proxy.$toast({
|
|
44
47
|
message: 'andy',
|
|
45
48
|
type: 'warning',
|
|
49
|
+
duration: 300,
|
|
50
|
+
closeAll: true,
|
|
46
51
|
})
|
|
47
52
|
* $toast.success('This is a success message')
|
|
48
|
-
* @
|
|
53
|
+
* @example2 显示对象
|
|
49
54
|
* $toast({
|
|
50
55
|
dangerouslyUseHTMLString: true,
|
|
51
56
|
message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
|
|
@@ -53,7 +58,13 @@ declare const isNumber: (val: any) => val is number;
|
|
|
53
58
|
duration: 5000,
|
|
54
59
|
})
|
|
55
60
|
*/
|
|
56
|
-
|
|
61
|
+
type MessageType = 'success' | 'info' | 'error' | 'warning';
|
|
62
|
+
type ShortType = 's' | 'i' | 'e' | 'w';
|
|
63
|
+
type ToastType = MessageType | ShortType;
|
|
64
|
+
type ToastOptions = Partial<ElMessageOptions> & {
|
|
65
|
+
closeAll?: boolean;
|
|
66
|
+
};
|
|
67
|
+
declare function $toast(message: string | ToastOptions, type?: ToastType | ToastOptions, otherParams?: ToastOptions): void;
|
|
57
68
|
declare namespace $toast {
|
|
58
69
|
var success: (message: any, otherParams?: {}) => void;
|
|
59
70
|
var info: (message: any, otherParams?: {}) => void;
|
|
@@ -73,6 +84,16 @@ declare function getStorage(data: any, isSession?: boolean): string | null;
|
|
|
73
84
|
* clearStorage({ exclude: ['loginId', 'token'] })
|
|
74
85
|
*/
|
|
75
86
|
declare function clearStorage(str?: string | [] | object): void;
|
|
87
|
+
/**
|
|
88
|
+
* element-plus的form表单使用promise进行封装
|
|
89
|
+
* @param ref
|
|
90
|
+
* @param param1
|
|
91
|
+
* @returns Promise
|
|
92
|
+
* await proxy.validForm(formRef);
|
|
93
|
+
* await proxy.validForm(formRef, {message: '自定义错误'});
|
|
94
|
+
* await proxy.validForm(formRef, {showMessage: false});
|
|
95
|
+
* await proxy.validForm(formRef, {detail: true});
|
|
96
|
+
*/
|
|
76
97
|
declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
77
98
|
message?: string | undefined;
|
|
78
99
|
detail?: boolean | undefined;
|
|
@@ -80,9 +101,9 @@ declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
|
80
101
|
}): Promise<unknown>;
|
|
81
102
|
/**
|
|
82
103
|
* 判断变量是否空值
|
|
83
|
-
* undefined, null, '', ' ', false, 0, [], {} 均返回true,否则返回false
|
|
104
|
+
* undefined, null, '', ' ', false, 0, [], {}, NaN, new Set(), new Map(), BigInt(0), new Date('无效日期') 均返回true,否则返回false
|
|
84
105
|
*/
|
|
85
|
-
declare function isEmpty(data: any): boolean;
|
|
106
|
+
declare function isEmpty(data: any, strict?: boolean): boolean;
|
|
86
107
|
declare function notEmpty(v: any): boolean;
|
|
87
108
|
/**
|
|
88
109
|
* 将两个对象合并, 以第二个对象为准, 如果两个对象, 一个属性有值, 一个没值, 合并后有值; 如果两个属性都有值, 以第二个属性为准
|
|
@@ -255,17 +276,44 @@ declare function toFixed(value: any, options?: {
|
|
|
255
276
|
/**
|
|
256
277
|
* 只有对正整数或者字符串正整数才进行单位的转换,
|
|
257
278
|
* 否则返回原始数据
|
|
279
|
+
* @example
|
|
280
|
+
* proxy.formatBytes(536870912) // 512MB
|
|
258
281
|
* proxy.formatBytes(536870912) // 512MB
|
|
259
282
|
*/
|
|
260
283
|
declare function formatBytes(bytes: any, { toFixed, thousands }?: {
|
|
261
284
|
toFixed?: number | undefined;
|
|
262
285
|
thousands?: boolean | undefined;
|
|
263
286
|
}): any;
|
|
287
|
+
/**
|
|
288
|
+
* 字节转数字
|
|
289
|
+
* @param oBytes
|
|
290
|
+
* @param param1
|
|
291
|
+
* @returns number
|
|
292
|
+
* formatBytesConvert('0.5GB') 536870912
|
|
293
|
+
* formatBytesConvert('1,234 GB') 1324997410816
|
|
294
|
+
* formatBytesConvert('1,234 GB', {thousand: true}) 1,324,997,410,816
|
|
295
|
+
* formatBytesConvert('1,234 GB', {toFixed: 2}) 1324997410816.00
|
|
296
|
+
*/
|
|
264
297
|
declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
|
|
265
298
|
thounsand?: boolean | undefined;
|
|
266
299
|
toFixed?: number | undefined;
|
|
267
300
|
}): any;
|
|
268
301
|
declare function throttle(fn: any, delay?: number): () => void;
|
|
302
|
+
/**
|
|
303
|
+
* 封装 Promise 执行,提供自动 loading 状态管理
|
|
304
|
+
* @param promise 需要执行的 Promise
|
|
305
|
+
* @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
|
|
306
|
+
* @returns Promise<{ data: T | null; error: any }>
|
|
307
|
+
* @example1
|
|
308
|
+
* const loading = ref(false);
|
|
309
|
+
* const { data, error } = await tryCatch(fetchUserData(), loading);
|
|
310
|
+
* @example2 // 无视 loading 状态
|
|
311
|
+
* const { data, error } = await tryCatch(fetchUserData());
|
|
312
|
+
*/
|
|
313
|
+
declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
|
|
314
|
+
data: T | null;
|
|
315
|
+
error: any;
|
|
316
|
+
}>;
|
|
269
317
|
declare function debounce(fn: any, delay?: number): () => void;
|
|
270
318
|
/**
|
|
271
319
|
* proxy.confirm('确定删除吗?')
|
|
@@ -312,4 +360,4 @@ declare function formatNewLines(str: any): any;
|
|
|
312
360
|
* */
|
|
313
361
|
declare function getVariable(propertyName: any): string;
|
|
314
362
|
|
|
315
|
-
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, uuid, validForm, validate };
|
|
363
|
+
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import * as element_plus from 'element-plus';
|
|
2
|
+
import { ElMessageOptions } from 'element-plus';
|
|
3
|
+
import { Ref } from '@vue/reactivity';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* 现有方法如下
|
|
@@ -37,15 +39,18 @@ declare const isString: (val: any) => val is string;
|
|
|
37
39
|
declare const isStringNumber: (val: string) => boolean;
|
|
38
40
|
declare const isNumber: (val: any) => val is number;
|
|
39
41
|
/**
|
|
40
|
-
* @
|
|
41
|
-
proxy.$toast('保存成功')
|
|
42
|
+
* @example1
|
|
43
|
+
proxy.$toast('保存成功') // s:success; i: info; w: warning; e: error;
|
|
42
44
|
proxy.$toast('保存失败', 'e')
|
|
45
|
+
proxy.$toast('永不关闭', {duration: 0})
|
|
43
46
|
proxy.$toast({
|
|
44
47
|
message: 'andy',
|
|
45
48
|
type: 'warning',
|
|
49
|
+
duration: 300,
|
|
50
|
+
closeAll: true,
|
|
46
51
|
})
|
|
47
52
|
* $toast.success('This is a success message')
|
|
48
|
-
* @
|
|
53
|
+
* @example2 显示对象
|
|
49
54
|
* $toast({
|
|
50
55
|
dangerouslyUseHTMLString: true,
|
|
51
56
|
message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
|
|
@@ -53,7 +58,13 @@ declare const isNumber: (val: any) => val is number;
|
|
|
53
58
|
duration: 5000,
|
|
54
59
|
})
|
|
55
60
|
*/
|
|
56
|
-
|
|
61
|
+
type MessageType = 'success' | 'info' | 'error' | 'warning';
|
|
62
|
+
type ShortType = 's' | 'i' | 'e' | 'w';
|
|
63
|
+
type ToastType = MessageType | ShortType;
|
|
64
|
+
type ToastOptions = Partial<ElMessageOptions> & {
|
|
65
|
+
closeAll?: boolean;
|
|
66
|
+
};
|
|
67
|
+
declare function $toast(message: string | ToastOptions, type?: ToastType | ToastOptions, otherParams?: ToastOptions): void;
|
|
57
68
|
declare namespace $toast {
|
|
58
69
|
var success: (message: any, otherParams?: {}) => void;
|
|
59
70
|
var info: (message: any, otherParams?: {}) => void;
|
|
@@ -73,6 +84,16 @@ declare function getStorage(data: any, isSession?: boolean): string | null;
|
|
|
73
84
|
* clearStorage({ exclude: ['loginId', 'token'] })
|
|
74
85
|
*/
|
|
75
86
|
declare function clearStorage(str?: string | [] | object): void;
|
|
87
|
+
/**
|
|
88
|
+
* element-plus的form表单使用promise进行封装
|
|
89
|
+
* @param ref
|
|
90
|
+
* @param param1
|
|
91
|
+
* @returns Promise
|
|
92
|
+
* await proxy.validForm(formRef);
|
|
93
|
+
* await proxy.validForm(formRef, {message: '自定义错误'});
|
|
94
|
+
* await proxy.validForm(formRef, {showMessage: false});
|
|
95
|
+
* await proxy.validForm(formRef, {detail: true});
|
|
96
|
+
*/
|
|
76
97
|
declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
77
98
|
message?: string | undefined;
|
|
78
99
|
detail?: boolean | undefined;
|
|
@@ -80,9 +101,9 @@ declare function validForm(ref: any, { message, detail, showMessage }?: {
|
|
|
80
101
|
}): Promise<unknown>;
|
|
81
102
|
/**
|
|
82
103
|
* 判断变量是否空值
|
|
83
|
-
* undefined, null, '', ' ', false, 0, [], {} 均返回true,否则返回false
|
|
104
|
+
* undefined, null, '', ' ', false, 0, [], {}, NaN, new Set(), new Map(), BigInt(0), new Date('无效日期') 均返回true,否则返回false
|
|
84
105
|
*/
|
|
85
|
-
declare function isEmpty(data: any): boolean;
|
|
106
|
+
declare function isEmpty(data: any, strict?: boolean): boolean;
|
|
86
107
|
declare function notEmpty(v: any): boolean;
|
|
87
108
|
/**
|
|
88
109
|
* 将两个对象合并, 以第二个对象为准, 如果两个对象, 一个属性有值, 一个没值, 合并后有值; 如果两个属性都有值, 以第二个属性为准
|
|
@@ -255,17 +276,44 @@ declare function toFixed(value: any, options?: {
|
|
|
255
276
|
/**
|
|
256
277
|
* 只有对正整数或者字符串正整数才进行单位的转换,
|
|
257
278
|
* 否则返回原始数据
|
|
279
|
+
* @example
|
|
280
|
+
* proxy.formatBytes(536870912) // 512MB
|
|
258
281
|
* proxy.formatBytes(536870912) // 512MB
|
|
259
282
|
*/
|
|
260
283
|
declare function formatBytes(bytes: any, { toFixed, thousands }?: {
|
|
261
284
|
toFixed?: number | undefined;
|
|
262
285
|
thousands?: boolean | undefined;
|
|
263
286
|
}): any;
|
|
287
|
+
/**
|
|
288
|
+
* 字节转数字
|
|
289
|
+
* @param oBytes
|
|
290
|
+
* @param param1
|
|
291
|
+
* @returns number
|
|
292
|
+
* formatBytesConvert('0.5GB') 536870912
|
|
293
|
+
* formatBytesConvert('1,234 GB') 1324997410816
|
|
294
|
+
* formatBytesConvert('1,234 GB', {thousand: true}) 1,324,997,410,816
|
|
295
|
+
* formatBytesConvert('1,234 GB', {toFixed: 2}) 1324997410816.00
|
|
296
|
+
*/
|
|
264
297
|
declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
|
|
265
298
|
thounsand?: boolean | undefined;
|
|
266
299
|
toFixed?: number | undefined;
|
|
267
300
|
}): any;
|
|
268
301
|
declare function throttle(fn: any, delay?: number): () => void;
|
|
302
|
+
/**
|
|
303
|
+
* 封装 Promise 执行,提供自动 loading 状态管理
|
|
304
|
+
* @param promise 需要执行的 Promise
|
|
305
|
+
* @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
|
|
306
|
+
* @returns Promise<{ data: T | null; error: any }>
|
|
307
|
+
* @example1
|
|
308
|
+
* const loading = ref(false);
|
|
309
|
+
* const { data, error } = await tryCatch(fetchUserData(), loading);
|
|
310
|
+
* @example2 // 无视 loading 状态
|
|
311
|
+
* const { data, error } = await tryCatch(fetchUserData());
|
|
312
|
+
*/
|
|
313
|
+
declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
|
|
314
|
+
data: T | null;
|
|
315
|
+
error: any;
|
|
316
|
+
}>;
|
|
269
317
|
declare function debounce(fn: any, delay?: number): () => void;
|
|
270
318
|
/**
|
|
271
319
|
* proxy.confirm('确定删除吗?')
|
|
@@ -312,4 +360,4 @@ declare function formatNewLines(str: any): any;
|
|
|
312
360
|
* */
|
|
313
361
|
declare function getVariable(propertyName: any): string;
|
|
314
362
|
|
|
315
|
-
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, uuid, validForm, validate };
|
|
363
|
+
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate };
|
package/dist/index.mjs
CHANGED
|
@@ -12,21 +12,27 @@ const isStringNumber = (val) => {
|
|
|
12
12
|
};
|
|
13
13
|
const isNumber = (val) => typeof val === "number";
|
|
14
14
|
function $toast(message, type = "success", otherParams = {}) {
|
|
15
|
-
const
|
|
15
|
+
const typeMap = {
|
|
16
16
|
s: "success",
|
|
17
17
|
i: "info",
|
|
18
18
|
e: "error",
|
|
19
19
|
w: "warning"
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
function isShortType(t) {
|
|
22
|
+
return ["s", "i", "e", "w"].includes(t);
|
|
23
|
+
}
|
|
24
|
+
function isToastOptions(obj) {
|
|
25
|
+
return typeof obj === "object" && obj !== null;
|
|
26
|
+
}
|
|
27
|
+
if (isToastOptions(message)) {
|
|
28
|
+
if (message.closeAll) {
|
|
23
29
|
ElMessage.closeAll();
|
|
24
30
|
}
|
|
25
31
|
ElMessage(message);
|
|
26
32
|
return;
|
|
27
33
|
}
|
|
28
|
-
if (
|
|
29
|
-
if (type.
|
|
34
|
+
if (isToastOptions(type)) {
|
|
35
|
+
if (type.closeAll) {
|
|
30
36
|
ElMessage.closeAll();
|
|
31
37
|
}
|
|
32
38
|
ElMessage({
|
|
@@ -39,9 +45,10 @@ function $toast(message, type = "success", otherParams = {}) {
|
|
|
39
45
|
if (otherParams.closeAll) {
|
|
40
46
|
ElMessage.closeAll();
|
|
41
47
|
}
|
|
48
|
+
const resolvedType = isShortType(type) ? typeMap[type] : type;
|
|
42
49
|
ElMessage({
|
|
43
50
|
message,
|
|
44
|
-
type:
|
|
51
|
+
type: resolvedType,
|
|
45
52
|
...otherParams
|
|
46
53
|
});
|
|
47
54
|
}
|
|
@@ -149,32 +156,38 @@ function validForm(ref, { message = "\u8868\u5355\u6821\u9A8C\u9519\u8BEF, \u8BF
|
|
|
149
156
|
});
|
|
150
157
|
});
|
|
151
158
|
}
|
|
152
|
-
function isEmpty(data) {
|
|
159
|
+
function isEmpty(data, strict = false) {
|
|
153
160
|
if (isRef(data)) {
|
|
154
161
|
data = unref(data);
|
|
155
162
|
}
|
|
163
|
+
if (strict) {
|
|
164
|
+
if (data === false || data === 0 || data === BigInt(0)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (data == null) return true;
|
|
156
169
|
if (data instanceof Date) {
|
|
157
170
|
return isNaN(data.getTime());
|
|
158
171
|
}
|
|
159
172
|
switch (typeof data) {
|
|
160
|
-
case "undefined":
|
|
161
|
-
return true;
|
|
162
173
|
case "string":
|
|
163
|
-
|
|
164
|
-
break;
|
|
174
|
+
return data.trim().length === 0;
|
|
165
175
|
case "boolean":
|
|
166
|
-
|
|
167
|
-
break;
|
|
176
|
+
return !data;
|
|
168
177
|
case "number":
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
case "
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
+
return 0 === data || isNaN(data);
|
|
179
|
+
// ❗ `NaN`或者0 被认为是空
|
|
180
|
+
case "symbol":
|
|
181
|
+
return false;
|
|
182
|
+
case "bigint":
|
|
183
|
+
return data === BigInt(0);
|
|
184
|
+
}
|
|
185
|
+
if (data instanceof Map || data instanceof Set) return data.size === 0;
|
|
186
|
+
if (Array.isArray(data) || typeof data.length === "number" && Object.prototype.toString.call(data) === "[object Object]") {
|
|
187
|
+
return data.length === 0;
|
|
188
|
+
}
|
|
189
|
+
if (typeof data === "object") {
|
|
190
|
+
return Object.keys(data).length === 0;
|
|
178
191
|
}
|
|
179
192
|
return false;
|
|
180
193
|
}
|
|
@@ -240,7 +253,7 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
|
240
253
|
});
|
|
241
254
|
return time_str;
|
|
242
255
|
}
|
|
243
|
-
function formatDurationTime(timestamp, cFormat = "{d}
|
|
256
|
+
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
244
257
|
const secondsPerMinute = 60;
|
|
245
258
|
const minutesPerHour = 60;
|
|
246
259
|
const hoursPerDay = 24;
|
|
@@ -689,7 +702,7 @@ function formatBytesConvert(oBytes, { thounsand = false, toFixed: toFixed2 = 0 }
|
|
|
689
702
|
}
|
|
690
703
|
let finalRes = size * units[unit];
|
|
691
704
|
if (toFixed2) {
|
|
692
|
-
finalRes =
|
|
705
|
+
finalRes = Number(finalRes).toFixed(toFixed2);
|
|
693
706
|
}
|
|
694
707
|
if (thounsand) {
|
|
695
708
|
finalRes = formatThousands(finalRes);
|
|
@@ -715,6 +728,23 @@ function throttle(fn, delay = 1e3) {
|
|
|
715
728
|
}
|
|
716
729
|
};
|
|
717
730
|
}
|
|
731
|
+
function tryCatch(promise, sendLoading) {
|
|
732
|
+
const updateLoading = (value) => {
|
|
733
|
+
if (isRef(sendLoading)) {
|
|
734
|
+
sendLoading.value = value;
|
|
735
|
+
} else if (sendLoading !== null) {
|
|
736
|
+
console.warn("Cannot modify non-ref sendLoading directly!");
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
updateLoading(true);
|
|
740
|
+
return promise.then((data) => {
|
|
741
|
+
updateLoading(false);
|
|
742
|
+
return { data, error: null };
|
|
743
|
+
}).catch((error) => {
|
|
744
|
+
updateLoading(false);
|
|
745
|
+
return { data: null, error };
|
|
746
|
+
});
|
|
747
|
+
}
|
|
718
748
|
function debounce(fn, delay = 1e3) {
|
|
719
749
|
let timer = null;
|
|
720
750
|
return function() {
|
|
@@ -756,4 +786,4 @@ function getVariable(propertyName) {
|
|
|
756
786
|
return res;
|
|
757
787
|
}
|
|
758
788
|
|
|
759
|
-
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, uuid, validForm, validate };
|
|
789
|
+
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate };
|