@oeos-components/utils 0.0.21 → 0.0.22
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/base.cjs +682 -0
- package/dist/base.d.cts +342 -0
- package/dist/base.d.mts +342 -0
- package/dist/base.d.ts +342 -0
- package/dist/base.mjs +655 -0
- package/dist/day.cjs +41 -0
- package/dist/day.d.cts +37 -0
- package/dist/day.d.mts +37 -0
- package/dist/day.d.ts +37 -0
- package/dist/day.mjs +31 -0
- package/dist/format.cjs +240 -0
- package/dist/format.d.cts +134 -0
- package/dist/format.d.mts +134 -0
- package/dist/format.d.ts +134 -0
- package/dist/format.mjs +230 -0
- package/dist/index.cjs +76 -866
- package/dist/index.d.cts +10 -396
- package/dist/index.d.mts +10 -396
- package/dist/index.d.ts +10 -396
- package/dist/index.mjs +12 -831
- package/dist/is.cjs +66 -0
- package/dist/is.d.cts +124 -0
- package/dist/is.d.mts +124 -0
- package/dist/is.d.ts +124 -0
- package/dist/is.mjs +43 -0
- package/dist/test.cjs +7 -0
- package/dist/test.d.cts +3 -0
- package/dist/test.d.mts +3 -0
- package/dist/test.d.ts +3 -0
- package/dist/test.mjs +5 -0
- package/dist/ws.cjs +120 -0
- package/dist/ws.d.cts +86 -0
- package/dist/ws.d.mts +86 -0
- package/dist/ws.d.ts +86 -0
- package/dist/ws.mjs +114 -0
- package/package.json +3 -1
package/dist/base.mjs
ADDED
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import { unref, isRef, toRaw } from '@vue/reactivity';
|
|
2
|
+
import { consola } from 'consola';
|
|
3
|
+
import { cloneDeep } from 'es-toolkit';
|
|
4
|
+
import { ElMessage, ElMessageBox } from 'element-plus';
|
|
5
|
+
|
|
6
|
+
function $toast(message, type = "success", otherParams = {}) {
|
|
7
|
+
const typeMap = {
|
|
8
|
+
s: "success",
|
|
9
|
+
i: "info",
|
|
10
|
+
e: "error",
|
|
11
|
+
w: "warning"
|
|
12
|
+
};
|
|
13
|
+
function isShortType(t) {
|
|
14
|
+
return ["s", "i", "e", "w"].includes(t);
|
|
15
|
+
}
|
|
16
|
+
function isToastOptions(obj) {
|
|
17
|
+
return typeof obj === "object" && obj !== null;
|
|
18
|
+
}
|
|
19
|
+
if (isToastOptions(message)) {
|
|
20
|
+
if (message.closeAll) {
|
|
21
|
+
ElMessage.closeAll();
|
|
22
|
+
}
|
|
23
|
+
message.customClass = message.customClass === "el" ? "" : "o-antd-message";
|
|
24
|
+
ElMessage(message);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (isToastOptions(type)) {
|
|
28
|
+
if (type.closeAll) {
|
|
29
|
+
ElMessage.closeAll();
|
|
30
|
+
}
|
|
31
|
+
type.customClass = type.customClass === "el" ? "" : "o-antd-message";
|
|
32
|
+
ElMessage({
|
|
33
|
+
message,
|
|
34
|
+
type: "success",
|
|
35
|
+
...type
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (otherParams.closeAll) {
|
|
40
|
+
ElMessage.closeAll();
|
|
41
|
+
}
|
|
42
|
+
const resolvedType = isShortType(type) ? typeMap[type] : type;
|
|
43
|
+
otherParams.customClass = otherParams.customClass === "el" ? "" : "o-antd-message";
|
|
44
|
+
ElMessage({
|
|
45
|
+
message,
|
|
46
|
+
type: resolvedType,
|
|
47
|
+
...otherParams
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
$toast.success = (message, otherParams = {}) => $toast(message, "success", otherParams);
|
|
51
|
+
$toast.info = (message, otherParams = {}) => $toast(message, "info", otherParams);
|
|
52
|
+
$toast.error = (message, otherParams = {}) => $toast(message, "error", otherParams);
|
|
53
|
+
$toast.warning = (message, otherParams = {}) => $toast(message, "warning", otherParams);
|
|
54
|
+
function setStorage(storageName, params, isSession = false) {
|
|
55
|
+
let handleParams;
|
|
56
|
+
if (typeof params === "number" || typeof params === "string") {
|
|
57
|
+
handleParams = params;
|
|
58
|
+
} else {
|
|
59
|
+
handleParams = JSON.stringify(params);
|
|
60
|
+
}
|
|
61
|
+
if (isSession) {
|
|
62
|
+
sessionStorage.setItem(storageName, handleParams);
|
|
63
|
+
} else {
|
|
64
|
+
localStorage.setItem(storageName, handleParams);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function getStorage(data, isSession = false) {
|
|
68
|
+
let getLocalData = "";
|
|
69
|
+
let getSessionData = "";
|
|
70
|
+
if (isSession) {
|
|
71
|
+
getSessionData = sessionStorage.getItem(data);
|
|
72
|
+
} else {
|
|
73
|
+
getLocalData = localStorage.getItem(data);
|
|
74
|
+
}
|
|
75
|
+
if (getLocalData) {
|
|
76
|
+
try {
|
|
77
|
+
if (typeof JSON.parse(getLocalData) !== "number") {
|
|
78
|
+
getLocalData = JSON.parse(getLocalData);
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
}
|
|
82
|
+
return getLocalData;
|
|
83
|
+
} else if (getSessionData) {
|
|
84
|
+
try {
|
|
85
|
+
if (typeof JSON.parse(getSessionData) !== "number") {
|
|
86
|
+
getSessionData = JSON.parse(getSessionData);
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
}
|
|
90
|
+
return getSessionData;
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
function clearStorage(str = "") {
|
|
95
|
+
if (isEmpty(str)) {
|
|
96
|
+
sessionStorage.clear();
|
|
97
|
+
localStorage.clear();
|
|
98
|
+
}
|
|
99
|
+
if (notEmpty(str) && getType(str) !== "object") {
|
|
100
|
+
let strArr = Array.isArray(str) ? str : [str];
|
|
101
|
+
for (let i = 0; i < strArr.length; i++) {
|
|
102
|
+
sessionStorage.removeItem(strArr[i]);
|
|
103
|
+
localStorage.removeItem(strArr[i]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (_isObjectWithExclude(str)) {
|
|
107
|
+
if (notEmpty(str.exclude) && getType(str) === "object") {
|
|
108
|
+
let sessionStorageObj = {};
|
|
109
|
+
let localStorageObj = {};
|
|
110
|
+
for (const key in str.exclude) {
|
|
111
|
+
if (Object.prototype.hasOwnProperty.call(str.exclude, key)) {
|
|
112
|
+
const name = str.exclude[key];
|
|
113
|
+
if (getStorage(name)) {
|
|
114
|
+
localStorageObj[name] = getStorage(name);
|
|
115
|
+
}
|
|
116
|
+
if (getStorage(name, true)) {
|
|
117
|
+
sessionStorageObj[name] = getStorage(name, true);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
sessionStorage.clear();
|
|
122
|
+
localStorage.clear();
|
|
123
|
+
for (const key in sessionStorageObj) {
|
|
124
|
+
setStorage(key, sessionStorageObj[key], true);
|
|
125
|
+
}
|
|
126
|
+
for (const key in localStorageObj) {
|
|
127
|
+
setStorage(key, localStorageObj[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function _isObjectWithExclude(obj) {
|
|
133
|
+
return typeof obj === "object" && obj !== null && "exclude" in obj && typeof obj.exclude === "object";
|
|
134
|
+
}
|
|
135
|
+
function validForm(ref, { message = "\u8868\u5355\u6821\u9A8C\u9519\u8BEF, \u8BF7\u68C0\u67E5", detail = false, showMessage = true } = {}) {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
unref(ref).validate((valid, status) => {
|
|
138
|
+
if (valid) {
|
|
139
|
+
resolve(status);
|
|
140
|
+
} else {
|
|
141
|
+
if (message && showMessage) {
|
|
142
|
+
let errorText = Object.keys(status);
|
|
143
|
+
let toastMessage = message;
|
|
144
|
+
if (detail) {
|
|
145
|
+
toastMessage = message + errorText.join(",");
|
|
146
|
+
}
|
|
147
|
+
$toast(toastMessage, "e");
|
|
148
|
+
}
|
|
149
|
+
reject(status);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function isEmpty(data, strict = false) {
|
|
155
|
+
if (isRef(data)) {
|
|
156
|
+
data = unref(data);
|
|
157
|
+
}
|
|
158
|
+
if (strict) {
|
|
159
|
+
if (data === false || data === 0 || data === BigInt(0)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (data == null) return true;
|
|
164
|
+
if (data instanceof Date) {
|
|
165
|
+
return isNaN(data.getTime());
|
|
166
|
+
}
|
|
167
|
+
switch (typeof data) {
|
|
168
|
+
case "string":
|
|
169
|
+
return data.trim().length === 0;
|
|
170
|
+
case "boolean":
|
|
171
|
+
return !data;
|
|
172
|
+
case "number":
|
|
173
|
+
return 0 === data || isNaN(data);
|
|
174
|
+
// ❗ `NaN`或者0 被认为是空
|
|
175
|
+
case "symbol":
|
|
176
|
+
return false;
|
|
177
|
+
case "bigint":
|
|
178
|
+
return data === BigInt(0);
|
|
179
|
+
}
|
|
180
|
+
if (data instanceof Map || data instanceof Set) return data.size === 0;
|
|
181
|
+
if (Array.isArray(data) || typeof data.length === "number" && Object.prototype.toString.call(data) === "[object Object]") {
|
|
182
|
+
return data.length === 0;
|
|
183
|
+
}
|
|
184
|
+
if (typeof data === "object") {
|
|
185
|
+
return Object.keys(data).length === 0;
|
|
186
|
+
}
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
function notEmpty(v) {
|
|
190
|
+
return !isEmpty(v);
|
|
191
|
+
}
|
|
192
|
+
function merge(obj1, obj2) {
|
|
193
|
+
let merged = { ...obj1, ...obj2 };
|
|
194
|
+
for (let key in merged) {
|
|
195
|
+
if (!isEmpty(obj1[key]) && !isEmpty(obj2[key])) {
|
|
196
|
+
merged[key] = obj2[key];
|
|
197
|
+
} else if (isEmpty(obj1[key]) && !isEmpty(obj2[key])) {
|
|
198
|
+
merged[key] = obj2[key];
|
|
199
|
+
} else if (!isEmpty(obj1[key]) && isEmpty(obj2[key])) {
|
|
200
|
+
merged[key] = obj1[key];
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return merged;
|
|
204
|
+
}
|
|
205
|
+
function clone(data, times = 1) {
|
|
206
|
+
if (isRef(data)) {
|
|
207
|
+
data = unref(data);
|
|
208
|
+
}
|
|
209
|
+
if (getType(data) !== "array") {
|
|
210
|
+
return cloneDeep(data);
|
|
211
|
+
}
|
|
212
|
+
const clonedData = cloneDeep(data);
|
|
213
|
+
const result = [];
|
|
214
|
+
for (let i = 0; i < times; i++) {
|
|
215
|
+
result.push(...clonedData);
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
function uuid(type = "", length = 4, options = {}) {
|
|
220
|
+
const { emailStr = "@qq.com", timeStr = "{y}-{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = options;
|
|
221
|
+
function isRef2(obj) {
|
|
222
|
+
return obj && typeof obj === "object" && obj._isRef === true;
|
|
223
|
+
}
|
|
224
|
+
function unref2(ref) {
|
|
225
|
+
return isRef2(ref) ? ref.value : ref;
|
|
226
|
+
}
|
|
227
|
+
function random2(min, max) {
|
|
228
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
229
|
+
}
|
|
230
|
+
type = unref2(type);
|
|
231
|
+
if (Array.isArray(type)) {
|
|
232
|
+
if (type.length === 0) return "";
|
|
233
|
+
const randIndex = optionsIndex ?? random2(0, type.length - 1);
|
|
234
|
+
const selectedItem = type[randIndex];
|
|
235
|
+
if (typeof selectedItem === "object" && selectedItem !== null && "value" in selectedItem) {
|
|
236
|
+
return selectedItem.value;
|
|
237
|
+
}
|
|
238
|
+
return selectedItem;
|
|
239
|
+
}
|
|
240
|
+
let randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
|
|
241
|
+
let result = startStr;
|
|
242
|
+
if (type === "phone") {
|
|
243
|
+
const prefixes = ["130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189"];
|
|
244
|
+
result = prefixes[random2(0, prefixes.length - 1)];
|
|
245
|
+
for (let i = 0; i < 8; i++) {
|
|
246
|
+
result += Math.floor(Math.random() * 10);
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
if (type === "email") {
|
|
251
|
+
result = uuid(startStr, length) + emailStr;
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
254
|
+
if (type === "time") {
|
|
255
|
+
return uuid(startStr, length, options) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
|
|
256
|
+
}
|
|
257
|
+
if (type === "number") {
|
|
258
|
+
const numChars = "123456789";
|
|
259
|
+
result = "";
|
|
260
|
+
for (let i = 0; i < length; i++) {
|
|
261
|
+
result += numChars[random2(0, numChars.length - 1)];
|
|
262
|
+
}
|
|
263
|
+
return Number(result);
|
|
264
|
+
}
|
|
265
|
+
if (type === "ip") {
|
|
266
|
+
const randomNum = random2(1, 99);
|
|
267
|
+
return `10.0.11.${randomNum}`;
|
|
268
|
+
}
|
|
269
|
+
if (type === "port") {
|
|
270
|
+
return random2(1, 65535);
|
|
271
|
+
}
|
|
272
|
+
for (let i = 0; i < length; i++) {
|
|
273
|
+
result += randomChars[random2(0, randomChars.length - 1)];
|
|
274
|
+
}
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
function getType(type) {
|
|
278
|
+
if (typeof type === "object") {
|
|
279
|
+
const objType = Object.prototype.toString.call(type).slice(8, -1).toLowerCase();
|
|
280
|
+
return objType;
|
|
281
|
+
} else {
|
|
282
|
+
return typeof type;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function sleep(delay = 0, fn) {
|
|
286
|
+
return new Promise(
|
|
287
|
+
(resolve) => setTimeout(() => {
|
|
288
|
+
fn?.();
|
|
289
|
+
resolve();
|
|
290
|
+
}, delay)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
function validateTrigger(type = "required", rules = {}, pureValid = false) {
|
|
294
|
+
let mergeRules = {
|
|
295
|
+
trigger: ["blur", "change"],
|
|
296
|
+
...rules
|
|
297
|
+
};
|
|
298
|
+
return validate(type, mergeRules, pureValid);
|
|
299
|
+
}
|
|
300
|
+
var ValidateType = /* @__PURE__ */ ((ValidateType2) => {
|
|
301
|
+
ValidateType2["REQUIRED"] = "required";
|
|
302
|
+
ValidateType2["PASSWORD"] = "password";
|
|
303
|
+
ValidateType2["NUMBER"] = "number";
|
|
304
|
+
ValidateType2["POSITIVE"] = "positive";
|
|
305
|
+
ValidateType2["ZERO_POSITIVE"] = "zeroPositive";
|
|
306
|
+
ValidateType2["INTEGER"] = "integer";
|
|
307
|
+
ValidateType2["DECIMAL"] = "decimal";
|
|
308
|
+
ValidateType2["MOBILE"] = "mobile";
|
|
309
|
+
ValidateType2["EMAIL"] = "email";
|
|
310
|
+
ValidateType2["IP"] = "ip";
|
|
311
|
+
ValidateType2["PORT"] = "port";
|
|
312
|
+
ValidateType2["BETWEEN"] = "between";
|
|
313
|
+
ValidateType2["LENGTH"] = "length";
|
|
314
|
+
ValidateType2["SAME"] = "same";
|
|
315
|
+
ValidateType2["CUSTOM"] = "custom";
|
|
316
|
+
return ValidateType2;
|
|
317
|
+
})(ValidateType || {});
|
|
318
|
+
function validate(type = "required", rules = {}, pureValid = false) {
|
|
319
|
+
let trigger = rules.trigger || [];
|
|
320
|
+
const typeMaps = Object.values(ValidateType);
|
|
321
|
+
let parseRequired = rules.required ?? true;
|
|
322
|
+
if (!typeMaps.includes(type)) {
|
|
323
|
+
return {
|
|
324
|
+
required: parseRequired,
|
|
325
|
+
message: type,
|
|
326
|
+
trigger
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
if (type === "required" /* REQUIRED */) {
|
|
330
|
+
return {
|
|
331
|
+
required: parseRequired,
|
|
332
|
+
message: rules.message ?? "\u8BF7\u8F93\u5165",
|
|
333
|
+
trigger
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
if (type === "password" /* PASSWORD */) {
|
|
337
|
+
const validateName = (rule, value, callback) => {
|
|
338
|
+
let validFlag = /^[a-zA-Z0-9_-]+$/.test(value);
|
|
339
|
+
if (!validFlag) {
|
|
340
|
+
callback(new Error(rules.message || "\u5BC6\u7801\u53EA\u80FD\u7531\u82F1\u6587\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u4E2D\u5212\u7EBF\u7EC4\u6210"));
|
|
341
|
+
} else {
|
|
342
|
+
callback();
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
return {
|
|
346
|
+
validator: validateName,
|
|
347
|
+
trigger
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
if (type === "positive" /* POSITIVE */ || type === "number" /* NUMBER */) {
|
|
351
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u6574\u6570", pureValid, /^[1-9]+\d*$/);
|
|
352
|
+
}
|
|
353
|
+
if (type === "zeroPositive" /* ZERO_POSITIVE */) {
|
|
354
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u975E\u8D1F\u6574\u6570", pureValid, /^(0|[1-9]+\d*)$/);
|
|
355
|
+
}
|
|
356
|
+
if (type === "integer" /* INTEGER */) {
|
|
357
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6574\u6570", pureValid, /^(0|[-]?[1-9]\d*)$/);
|
|
358
|
+
}
|
|
359
|
+
if (type === "decimal" /* DECIMAL */) {
|
|
360
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u975E\u8D1F\u6570\u5B57, \u5305\u542B\u5C0F\u6570\u4E14\u6700\u591A2\u4F4D", pureValid, /(0|[1-9]\d*)(\.\d{1, 2})?|0\.\d{1,2}/);
|
|
361
|
+
}
|
|
362
|
+
if (type === "mobile" /* MOBILE */) {
|
|
363
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7", pureValid, /^[1][0-9]{10}$/);
|
|
364
|
+
}
|
|
365
|
+
if (type === "email" /* EMAIL */) {
|
|
366
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684email", pureValid, /^[^\s@]+@[^\s@]+\.[^\s@]+$/);
|
|
367
|
+
}
|
|
368
|
+
if (type === "ip" /* IP */) {
|
|
369
|
+
return _validValue(
|
|
370
|
+
rules,
|
|
371
|
+
"\u8BF7\u8F93\u5165\u6B63\u786E\u7684ip\u5730\u5740",
|
|
372
|
+
pureValid,
|
|
373
|
+
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
if (type === "port" /* PORT */) {
|
|
377
|
+
return _validValue(
|
|
378
|
+
rules,
|
|
379
|
+
"\u8BF7\u8F93\u51651-65535\u7684\u7AEF\u53E3\u53F7",
|
|
380
|
+
pureValid,
|
|
381
|
+
/^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-5][0-5][0-3][0-5])$/
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
if (type === "between" /* BETWEEN */) {
|
|
385
|
+
let min = rules.min;
|
|
386
|
+
let max = rules.max;
|
|
387
|
+
const validateBetween = (rule, value, callback) => {
|
|
388
|
+
let validFlag = /^-?[0-9]+$/.test(value);
|
|
389
|
+
if (!validFlag) {
|
|
390
|
+
callback(new Error("\u8BF7\u8F93\u5165\u6570\u5B57"));
|
|
391
|
+
}
|
|
392
|
+
if (value < min && min !== void 0) {
|
|
393
|
+
callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5C0F\u4E8E${min}`));
|
|
394
|
+
}
|
|
395
|
+
if (value > max && max !== void 0) {
|
|
396
|
+
callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5927\u4E8E${max}`));
|
|
397
|
+
}
|
|
398
|
+
callback();
|
|
399
|
+
};
|
|
400
|
+
return {
|
|
401
|
+
validator: validateBetween,
|
|
402
|
+
trigger,
|
|
403
|
+
required: parseRequired
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
if (type === "length" /* LENGTH */) {
|
|
407
|
+
return {
|
|
408
|
+
min: rules.min,
|
|
409
|
+
max: rules.max,
|
|
410
|
+
message: rules.message ?? `\u8BF7\u8F93\u5165${rules.min}\u5230${rules.max}\u4E2A\u5B57\u7B26`,
|
|
411
|
+
trigger,
|
|
412
|
+
required: parseRequired
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
if (type === "same" /* SAME */) {
|
|
416
|
+
const validateSame = (rule, value, callback) => {
|
|
417
|
+
let isSame = value === rules.value;
|
|
418
|
+
if (!isSame) {
|
|
419
|
+
const errMessage = rules.message || "\u5BC6\u7801\u548C\u786E\u8BA4\u5BC6\u7801\u8981\u4E00\u81F4";
|
|
420
|
+
callback(new Error(errMessage));
|
|
421
|
+
}
|
|
422
|
+
if (parseRequired && !value) {
|
|
423
|
+
callback(new Error(rules.message || "\u8BF7\u8F93\u5165"));
|
|
424
|
+
}
|
|
425
|
+
callback();
|
|
426
|
+
};
|
|
427
|
+
let res = {
|
|
428
|
+
validator: validateSame,
|
|
429
|
+
trigger,
|
|
430
|
+
required: parseRequired
|
|
431
|
+
};
|
|
432
|
+
return res;
|
|
433
|
+
}
|
|
434
|
+
if (type === "custom" /* CUSTOM */) {
|
|
435
|
+
if (pureValid) {
|
|
436
|
+
return _validValue(rules.value, rules.message, pureValid, rules.reg);
|
|
437
|
+
} else {
|
|
438
|
+
return _validValue(rules, rules.message, pureValid, rules.reg);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function _validValue(rules2, msg, pureValid2, reg) {
|
|
442
|
+
if (pureValid2 === true) {
|
|
443
|
+
return reg.test(rules2);
|
|
444
|
+
}
|
|
445
|
+
const validatePhone = (rule, value, callback) => {
|
|
446
|
+
let validFlag = reg.test(value);
|
|
447
|
+
if (!validFlag) {
|
|
448
|
+
callback(new Error(rules2.message ?? msg));
|
|
449
|
+
} else {
|
|
450
|
+
callback();
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
return {
|
|
454
|
+
validator: validatePhone,
|
|
455
|
+
required: rules2.required ?? true,
|
|
456
|
+
trigger
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async function asyncWrapper(func, ...args) {
|
|
461
|
+
try {
|
|
462
|
+
const res = await func(...args);
|
|
463
|
+
return { res };
|
|
464
|
+
} catch (err) {
|
|
465
|
+
return { err };
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const copy = (text, toastParams = {}) => {
|
|
469
|
+
const textarea = document.createElement("textarea");
|
|
470
|
+
textarea.value = text;
|
|
471
|
+
textarea.style.position = "fixed";
|
|
472
|
+
document.body.appendChild(textarea);
|
|
473
|
+
textarea.select();
|
|
474
|
+
document.execCommand("copy");
|
|
475
|
+
document.body.removeChild(textarea);
|
|
476
|
+
if (!toastParams.hideToast) {
|
|
477
|
+
const { hideToast, ...toastOptions } = toastParams;
|
|
478
|
+
$toast(text + "\u590D\u5236\u6210\u529F", toastOptions);
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
return true;
|
|
482
|
+
};
|
|
483
|
+
function log(variableStr, variable, otherInfo = "") {
|
|
484
|
+
const stack = new Error().stack.split("\n")[2].trim();
|
|
485
|
+
const matchResult = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
486
|
+
let fileInfo = "";
|
|
487
|
+
try {
|
|
488
|
+
if (matchResult && otherInfo) {
|
|
489
|
+
const lineNumber = matchResult[2];
|
|
490
|
+
fileInfo = `vscode://file${JSON.parse(otherInfo)}:${lineNumber}`;
|
|
491
|
+
}
|
|
492
|
+
} catch (error) {
|
|
493
|
+
fileInfo = otherInfo;
|
|
494
|
+
}
|
|
495
|
+
if (isRef(variable)) {
|
|
496
|
+
let unrefVariable = unref(variable);
|
|
497
|
+
_log(toRaw(unrefVariable));
|
|
498
|
+
} else {
|
|
499
|
+
_log(variable);
|
|
500
|
+
}
|
|
501
|
+
function _log(consoleData) {
|
|
502
|
+
if (getType2(consoleData) === "object" || getType2(consoleData) === "array") {
|
|
503
|
+
consola.log(
|
|
504
|
+
`%c${variableStr} `,
|
|
505
|
+
"background:#fff; color: blue;font-size: 0.8em",
|
|
506
|
+
JSON.stringify(consoleData, null, " "),
|
|
507
|
+
`${fileInfo}`
|
|
508
|
+
);
|
|
509
|
+
} else {
|
|
510
|
+
consola.log(`%c${variableStr} `, "background:#fff; color: blue;font-size: 0.8em", consoleData, `${fileInfo}`);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
function getType2(type) {
|
|
514
|
+
if (typeof type === "object") {
|
|
515
|
+
const objType = Object.prototype.toString.call(type).slice(8, -1).toLowerCase();
|
|
516
|
+
return objType;
|
|
517
|
+
} else {
|
|
518
|
+
return typeof type;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
function random(min = 0, max = 10) {
|
|
523
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
524
|
+
}
|
|
525
|
+
function toLine(text, connect = "-") {
|
|
526
|
+
let translateText = text.replace(/([A-Z])/g, (match, p1, offset, origin) => {
|
|
527
|
+
if (offset === 0) {
|
|
528
|
+
return `${match.toLocaleLowerCase()}`;
|
|
529
|
+
} else {
|
|
530
|
+
return `${connect}${match.toLocaleLowerCase()}`;
|
|
531
|
+
}
|
|
532
|
+
}).toLocaleLowerCase();
|
|
533
|
+
return translateText;
|
|
534
|
+
}
|
|
535
|
+
function processWidth(initValue, isBase = false) {
|
|
536
|
+
let value = unref(initValue);
|
|
537
|
+
let res = "";
|
|
538
|
+
if (!value) {
|
|
539
|
+
return isBase ? value : {};
|
|
540
|
+
} else if (typeof value === "number") {
|
|
541
|
+
value = String(value);
|
|
542
|
+
}
|
|
543
|
+
if (value === "") {
|
|
544
|
+
return isBase ? value : {};
|
|
545
|
+
} else if (typeof value === "string" && !isNaN(value)) {
|
|
546
|
+
res = value + "px";
|
|
547
|
+
} else if (typeof value === "string" && /^[0-9]+(\.[0-9]+)?(px|%|em|rem|vw|vh|ch)*$/.test(value)) {
|
|
548
|
+
res = value;
|
|
549
|
+
} else {
|
|
550
|
+
console.warn(`${value} is Invalid unit provided`);
|
|
551
|
+
return value;
|
|
552
|
+
}
|
|
553
|
+
if (isBase) {
|
|
554
|
+
return res;
|
|
555
|
+
}
|
|
556
|
+
return { width: res };
|
|
557
|
+
}
|
|
558
|
+
function throttle(fn, delay = 1e3) {
|
|
559
|
+
let last = 0;
|
|
560
|
+
let timer = null;
|
|
561
|
+
return function() {
|
|
562
|
+
let context = this;
|
|
563
|
+
let args = arguments;
|
|
564
|
+
let now = +/* @__PURE__ */ new Date();
|
|
565
|
+
if (now - last < delay) {
|
|
566
|
+
clearTimeout(timer);
|
|
567
|
+
timer = setTimeout(function() {
|
|
568
|
+
last = now;
|
|
569
|
+
fn.apply(context, args);
|
|
570
|
+
}, delay);
|
|
571
|
+
} else {
|
|
572
|
+
last = now;
|
|
573
|
+
fn.apply(context, args);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
function tryCatch(promise, sendLoading) {
|
|
578
|
+
const updateLoading = (value) => {
|
|
579
|
+
if (isRef(sendLoading)) {
|
|
580
|
+
sendLoading.value = value;
|
|
581
|
+
} else if (sendLoading !== null) {
|
|
582
|
+
console.warn("Cannot modify non-ref sendLoading directly!");
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
updateLoading(true);
|
|
586
|
+
return promise.then((data) => {
|
|
587
|
+
updateLoading(false);
|
|
588
|
+
return { data, error: null };
|
|
589
|
+
}).catch((error) => {
|
|
590
|
+
updateLoading(false);
|
|
591
|
+
return { data: null, error };
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
function debounce(func, delay = 500, immediate, resultCallback) {
|
|
595
|
+
let timer = null;
|
|
596
|
+
let isInvoke = false;
|
|
597
|
+
const _debounce = function(...args) {
|
|
598
|
+
return new Promise((resolve, reject) => {
|
|
599
|
+
if (timer) clearTimeout(timer);
|
|
600
|
+
if (immediate && !isInvoke) {
|
|
601
|
+
try {
|
|
602
|
+
const result = func.apply(this, args);
|
|
603
|
+
if (resultCallback) resultCallback(result);
|
|
604
|
+
resolve(result);
|
|
605
|
+
} catch (e) {
|
|
606
|
+
reject(e);
|
|
607
|
+
}
|
|
608
|
+
isInvoke = true;
|
|
609
|
+
} else {
|
|
610
|
+
timer = setTimeout(() => {
|
|
611
|
+
try {
|
|
612
|
+
const result = func.apply(this, args);
|
|
613
|
+
if (resultCallback) resultCallback(result);
|
|
614
|
+
resolve(result);
|
|
615
|
+
} catch (e) {
|
|
616
|
+
reject(e);
|
|
617
|
+
}
|
|
618
|
+
isInvoke = false;
|
|
619
|
+
timer = null;
|
|
620
|
+
}, delay);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
};
|
|
624
|
+
_debounce.cancel = function() {
|
|
625
|
+
if (timer) clearTimeout(timer);
|
|
626
|
+
isInvoke = false;
|
|
627
|
+
timer = null;
|
|
628
|
+
};
|
|
629
|
+
return _debounce;
|
|
630
|
+
}
|
|
631
|
+
function confirm(message, options) {
|
|
632
|
+
const resolvedMessage = typeof message === "function" ? message() : message;
|
|
633
|
+
const elContext = ElMessageBox.install?.context || ElMessageBox._context || document.querySelector("#app")?._vue_app?._context;
|
|
634
|
+
const mergeOptions = {
|
|
635
|
+
title: "\u63D0\u793A",
|
|
636
|
+
draggable: true,
|
|
637
|
+
showCancelButton: false,
|
|
638
|
+
confirmButtonText: "\u786E\u5B9A",
|
|
639
|
+
dangerouslyUseHTMLString: true,
|
|
640
|
+
// 允许 HTML
|
|
641
|
+
appContext: elContext,
|
|
642
|
+
// 强制注入 Element Plus 的上下文
|
|
643
|
+
...options
|
|
644
|
+
};
|
|
645
|
+
return ElMessageBox.confirm(resolvedMessage, mergeOptions);
|
|
646
|
+
}
|
|
647
|
+
function getVariable(propertyName) {
|
|
648
|
+
let res = getComputedStyle(document.documentElement).getPropertyValue(propertyName).trim();
|
|
649
|
+
return res;
|
|
650
|
+
}
|
|
651
|
+
function test() {
|
|
652
|
+
return "\u54C8\u54C8\u54C8" + /* @__PURE__ */ new Date();
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, getStorage, getType, getVariable, isEmpty, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
|
package/dist/day.cjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const dayjs = require('dayjs');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
6
|
+
|
|
7
|
+
const dayjs__default = /*#__PURE__*/_interopDefaultCompat(dayjs);
|
|
8
|
+
|
|
9
|
+
function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
|
|
10
|
+
return dayjs__default(date || /* @__PURE__ */ new Date()).format(format);
|
|
11
|
+
}
|
|
12
|
+
const formatDateToDay = (date) => formatDate(date, "YYYY-MM-DD");
|
|
13
|
+
const formatDateToMinute = (date) => formatDate(date, "YYYY-MM-DD HH:mm");
|
|
14
|
+
function diffDate(date1, date2 = dayjs__default(), format = "second") {
|
|
15
|
+
if (!date1) return;
|
|
16
|
+
return dayjs__default(date1).diff(dayjs__default(date2), format);
|
|
17
|
+
}
|
|
18
|
+
function diffDateFromCurrent(second) {
|
|
19
|
+
if (second >= 60 * 60 * 24 * 365) {
|
|
20
|
+
return `${parseInt(String(second / (60 * 60 * 24 * 365)))}\u5E74\u524D`;
|
|
21
|
+
}
|
|
22
|
+
if (second >= 60 * 60 * 24 * 30) {
|
|
23
|
+
return `${parseInt(String(second / (60 * 60 * 24 * 30)))}\u6708\u524D`;
|
|
24
|
+
}
|
|
25
|
+
if (second >= 60 * 60 * 24) {
|
|
26
|
+
return `${parseInt(String(second / (60 * 60 * 24)))}\u5929\u524D`;
|
|
27
|
+
}
|
|
28
|
+
if (second >= 60 * 60) {
|
|
29
|
+
return `${parseInt(String(second / (60 * 60)))}\u5C0F\u65F6\u524D`;
|
|
30
|
+
}
|
|
31
|
+
if (second >= 60) {
|
|
32
|
+
return `${parseInt(String(second / 60))}\u5206\u949F\u524D`;
|
|
33
|
+
}
|
|
34
|
+
return `${parseInt(String(second))}\u79D2\u524D`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.diffDate = diffDate;
|
|
38
|
+
exports.diffDateFromCurrent = diffDateFromCurrent;
|
|
39
|
+
exports.formatDate = formatDate;
|
|
40
|
+
exports.formatDateToDay = formatDateToDay;
|
|
41
|
+
exports.formatDateToMinute = formatDateToMinute;
|