@oeos-components/utils 0.0.15 → 0.0.17

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 CHANGED
@@ -232,7 +232,12 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
232
232
  if (typeof time === "object") {
233
233
  date = time;
234
234
  } else {
235
- if (("" + time).length === 10) time = parseInt(time) * 1e3;
235
+ const timeStr = "" + time;
236
+ if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
237
+ time = parseFloat(time) * 1e3;
238
+ } else if (timeStr.length === 10) {
239
+ time = parseInt(time) * 1e3;
240
+ }
236
241
  date = new Date(time);
237
242
  }
238
243
  const formatObj = {
@@ -295,54 +300,63 @@ function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}
295
300
  });
296
301
  return time_str;
297
302
  }
298
- function uuid(type = "", length = 4, { emailStr = "@qq.com", timeStr = "{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = {}) {
299
- let randomStr = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
300
- let res = type;
301
- if (reactivity.isRef(type)) {
302
- type = reactivity.unref(type);
303
+ function uuid(type = "", length = 4, options = {}) {
304
+ const { emailStr = "@qq.com", timeStr = "{y}-{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = options;
305
+ function isRef2(obj) {
306
+ return obj && typeof obj === "object" && obj._isRef === true;
307
+ }
308
+ function unref2(ref) {
309
+ return isRef2(ref) ? ref.value : ref;
310
+ }
311
+ function random2(min, max) {
312
+ return Math.floor(Math.random() * (max - min + 1) + min);
303
313
  }
304
- if (getType(type) === "array" && type.length > 0) {
305
- let randNum = random(0, type.length - 1);
306
- if (!length) {
307
- return type[optionsIndex ?? randNum];
314
+ type = unref2(type);
315
+ if (Array.isArray(type)) {
316
+ if (type.length === 0) return "";
317
+ const randIndex = optionsIndex ?? random2(0, type.length - 1);
318
+ const selectedItem = type[randIndex];
319
+ if (typeof selectedItem === "object" && selectedItem !== null && "value" in selectedItem) {
320
+ return selectedItem.value;
308
321
  }
309
- return type[optionsIndex ?? randNum][length === 4 ? "value" : length];
322
+ return selectedItem;
310
323
  }
324
+ let randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
325
+ let result = startStr;
311
326
  if (type === "phone") {
312
- let prefixArray = new Array("130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189");
313
- let i = parseInt(Math.random() * 10);
314
- let res2 = prefixArray[i];
315
- for (var j = 0; j < 8; j++) {
316
- res2 += Math.floor(Math.random() * 10);
327
+ const prefixes = ["130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189"];
328
+ result = prefixes[random2(0, prefixes.length - 1)];
329
+ for (let i = 0; i < 8; i++) {
330
+ result += Math.floor(Math.random() * 10);
317
331
  }
318
- return res2;
332
+ return result;
319
333
  }
320
334
  if (type === "email") {
321
- return uuid(startStr, length) + emailStr;
335
+ result = uuid(startStr, length) + emailStr;
336
+ return result;
322
337
  }
323
338
  if (type === "time") {
324
- return uuid(startStr, length) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
339
+ return uuid(startStr, length, options) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
325
340
  }
326
341
  if (type === "number") {
327
- let randomStr2 = "123456789";
328
- let res2 = "";
329
- for (let i = length; i > 0; --i) {
330
- res2 += randomStr2[Math.floor(Math.random() * randomStr2.length)];
342
+ const numChars = "123456789";
343
+ result = "";
344
+ for (let i = 0; i < length; i++) {
345
+ result += numChars[random2(0, numChars.length - 1)];
331
346
  }
332
- return Number(res2);
347
+ return Number(result);
333
348
  }
334
349
  if (type === "ip") {
335
- let randomNum = random(1, 99);
336
- return `10.0.11.` + randomNum;
350
+ const randomNum = random2(1, 99);
351
+ return `10.0.11.${randomNum}`;
337
352
  }
338
353
  if (type === "port") {
339
- let randomNum = random(1, 65535);
340
- return randomNum;
354
+ return random2(1, 65535);
341
355
  }
342
- for (let i = length; i > 0; --i) {
343
- res += randomStr[Math.floor(Math.random() * randomStr.length)];
356
+ for (let i = 0; i < length; i++) {
357
+ result += randomChars[random2(0, randomChars.length - 1)];
344
358
  }
345
- return res;
359
+ return result;
346
360
  }
347
361
  function getType(type) {
348
362
  if (typeof type === "object") {
@@ -360,6 +374,13 @@ function sleep(delay = 0, fn) {
360
374
  }, delay)
361
375
  );
362
376
  }
377
+ function validateTrigger(type = "required", rules = {}, pureValid = false) {
378
+ let mergeRules = {
379
+ trigger: ["blur", "change"],
380
+ ...rules
381
+ };
382
+ return validate(type, mergeRules, pureValid);
383
+ }
363
384
  function validate(type = "required", rules = {}, pureValid = false) {
364
385
  if (getType(type) === "object") {
365
386
  pureValid = rules || false;
@@ -367,7 +388,7 @@ function validate(type = "required", rules = {}, pureValid = false) {
367
388
  type = "required";
368
389
  }
369
390
  let trigger = rules.trigger || [];
370
- const typeMaps = ["required", "pwd", "number", "mobile", "between", "length", "same", "ip", "port", "custom"];
391
+ const typeMaps = ["required", "pwd", "number", "mobile", "email", "between", "length", "same", "ip", "port", "custom"];
371
392
  let parseRequired = rules.required ?? true;
372
393
  if (!typeMaps.includes(type)) {
373
394
  return {
@@ -412,6 +433,9 @@ function validate(type = "required", rules = {}, pureValid = false) {
412
433
  if (type === "mobile") {
413
434
  return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7", pureValid, /^[1][0-9]{10}$/);
414
435
  }
436
+ if (type === "email") {
437
+ return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684email", pureValid, /^[^\s@]+@[^\s@]+\.[^\s@]+$/);
438
+ }
415
439
  if (type === "ip") {
416
440
  return _validValue(
417
441
  rules,
@@ -421,17 +445,17 @@ function validate(type = "required", rules = {}, pureValid = false) {
421
445
  );
422
446
  }
423
447
  if (type === "between") {
424
- let min = rules.min || 1;
425
- let max = rules.max || 10;
448
+ let min = rules.min;
449
+ let max = rules.max;
426
450
  const validateBetween = (rule, value, callback) => {
427
- let validFlag = /^[0-9]+$/.test(value);
451
+ let validFlag = /^-?[0-9]+$/.test(value);
428
452
  if (!validFlag) {
429
453
  callback(new Error("\u8BF7\u8F93\u5165\u6570\u5B57"));
430
454
  }
431
- if (value < min) {
455
+ if (value < min && min !== void 0) {
432
456
  callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5C0F\u4E8E${min}`));
433
457
  }
434
- if (value > max) {
458
+ if (value > max && max !== void 0) {
435
459
  callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5927\u4E8E${max}`));
436
460
  }
437
461
  callback();
@@ -651,26 +675,15 @@ function formatBytes(bytes, options = {}) {
651
675
  } else {
652
676
  return bytes;
653
677
  }
654
- if (bytes <= 0) {
655
- return bytes.toFixed(digit) + " B";
678
+ if (bytes <= 1) {
679
+ return Math[roundType](bytes * Math.pow(10, digit)) / Math.pow(10, digit) + " B";
656
680
  }
657
681
  const k = 1024;
658
682
  const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
659
683
  const i = Math.floor(Math.log(bytes) / Math.log(k));
660
684
  const power = Math.pow(k, i);
661
685
  let num = bytes / power;
662
- switch (roundType) {
663
- case "ceil":
664
- num = Math.ceil(num * Math.pow(10, digit)) / Math.pow(10, digit);
665
- break;
666
- case "round":
667
- num = Math.round(num * Math.pow(10, digit)) / Math.pow(10, digit);
668
- break;
669
- case "floor":
670
- default:
671
- num = Math.floor(num * Math.pow(10, digit)) / Math.pow(10, digit);
672
- break;
673
- }
686
+ num = Math[roundType](num * Math.pow(10, digit)) / Math.pow(10, digit);
674
687
  let res = num.toFixed(digit) + " " + sizes[i];
675
688
  if (thousands) {
676
689
  res = formatThousands(res);
@@ -842,3 +855,4 @@ exports.tryCatch = tryCatch;
842
855
  exports.uuid = uuid;
843
856
  exports.validForm = validForm;
844
857
  exports.validate = validate;
858
+ exports.validateTrigger = validateTrigger;
package/dist/index.d.cts CHANGED
@@ -130,8 +130,9 @@ declare function clone(data: any, times?: number): any;
130
130
  * formatTime(1541927611000); //2018-11-11 17:13:21
131
131
  * formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
132
132
  * formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
133
- * formatTime(new Date()); //2018-11-11 17:13:21
134
- * formatTime(new Date().getTime()); //2018-11-11 17:13:21
133
+ * formatTime(new Date()); // 2018-11-11 17:13:21
134
+ * formatTime(new Date().getTime()); // 2018-11-11 17:13:21
135
+ * formatTime('1764128798.456'); // 2025/11/26 11:11:38
135
136
  */
136
137
  declare function formatTime(time: any, cFormat?: string): any;
137
138
  /**
@@ -145,15 +146,15 @@ declare function formatTime(time: any, cFormat?: string): any;
145
146
  declare function formatDurationTime(timestamp: any, cFormat?: string): string;
146
147
  /**
147
148
  * 生成 UUID
148
- * @param {string} [type=''] - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number' 或空字符串
149
- * @param {number} [length=4] - 生成字符串的长度
150
- * @param {object} [options={}] - 额外的选项
151
- * @param {string} [options.emailStr='@qq.com'] - 生成 email 时使用的后缀
152
- * @param {string} [options.timeStr='{m}-{d} {h}:{i}:{s}'] - 生成时间字符串的格式
153
- * @param {string} [options.startStr=''] - 起始字符串
154
- * @param {number|null} [options.optionsIndex=null] - 数组索引
155
- * @returns {string|number} - 生成的 UUID
156
- * uuid("名字") => 名字hc8f
149
+ * @param type - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number', 'ip', 'port' 或空字符串
150
+ * @param length - 生成字符串的长度(默认为4)
151
+ * @param options - 额外的选项
152
+ * @param options.emailStr - 生成 email 时使用的后缀(默认为 '@qq.com')
153
+ * @param options.timeStr - 生成时间字符串的格式(默认为 '{m}-{d} {h}:{i}:{s}'
154
+ * @param options.startStr - 起始字符串(默认为空)
155
+ * @param options.optionsIndex - 数组索引(默认为随机)
156
+ * @returns 生成的 UUID (字符串或数字)
157
+ * * uuid("名字") => 名字hc8f
157
158
  * uuid() => abcd
158
159
  * uuid('time') => 25MR 10-27 17:34:01
159
160
  * uuid('time', 0, {startStr:'andy', timeStr:"{h}:{i}:{s}"}) => andy 17:38:23
@@ -162,12 +163,15 @@ declare function formatDurationTime(timestamp: any, cFormat?: string): string;
162
163
  * uuid('number') => 2319
163
164
  * uuid([ { label: "小泽泽", value: "xzz" },{ label: "小月月", value: "xyy" }]) => xzz
164
165
  */
165
- declare function uuid(type?: string, length?: number, { emailStr, timeStr, startStr, optionsIndex }?: {
166
- emailStr?: string | undefined;
167
- timeStr?: string | undefined;
168
- startStr?: string | undefined;
169
- optionsIndex?: null | undefined;
170
- }): any;
166
+ declare function uuid(type?: string | Array<{
167
+ label: string;
168
+ value: any;
169
+ }>, length?: number, options?: {
170
+ emailStr?: string;
171
+ timeStr?: string;
172
+ startStr?: string;
173
+ optionsIndex?: number | null;
174
+ }): string | number;
171
175
  /**
172
176
  * 判断传入参数的类型
173
177
  * @param {*} type
@@ -179,7 +183,36 @@ declare function uuid(type?: string, length?: number, { emailStr, timeStr, start
179
183
  * getType(123) number
180
184
  */
181
185
  declare function getType(type: any): string;
182
- declare function sleep(delay?: number, fn?: () => void): Promise<unknown>;
186
+ /**
187
+ * 一个辅助函数,用于在代码中创建一个暂停(延迟)。
188
+ * 它返回一个 Promise,你可以在 `await` 后使用它来实现类似 "sleep" 的效果。
189
+ *
190
+ * @param delay - 等待的毫秒数。默认值为 0,表示不延迟。
191
+ * @param fn - (可选) 一个在延迟结束后立即执行的函数。
192
+ *
193
+ * @returns 一个 Promise,当延迟结束后解析(resolve)。
194
+ *
195
+ * @example
196
+ * // 基本用法:延迟 2 秒后打印消息
197
+ * console.log('开始');
198
+ * await sleep(2000);
199
+ * console.log('2秒后执行');
200
+ *
201
+ * @example
202
+ * // 带回调函数的用法:延迟 1 秒后执行清理工作
203
+ * sleep(1000, () => {
204
+ * console.log('执行清理操作...');
205
+ * // 清理代码...
206
+ * });
207
+ *
208
+ * @example
209
+ * // 在循环中使用:每次迭代后延迟 500 毫秒
210
+ * for (let i = 0; i < 5; i++) {
211
+ * console.log(`当前值: ${i}`);
212
+ * await sleep(500);
213
+ * }
214
+ */
215
+ declare function sleep(delay?: number, fn?: () => void): Promise<void>;
183
216
  /** @使用方式
184
217
  * 1. 在el-form中使用
185
218
  name: [ proxy.validate('name', { message: '你干嘛哈哈' })],
@@ -199,6 +232,7 @@ confirmRegPwd: [
199
232
  let ip = proxy.validate('ip', 122322, true)
200
233
  let custom = proxy.validate('custom', { value: -123, reg: /^-\d+\.?\d{0,2}$/ }, true)
201
234
  */
235
+ declare function validateTrigger(type?: string, rules?: {}, pureValid?: boolean): any;
202
236
  declare function validate(type?: string, rules?: {}, pureValid?: boolean): any;
203
237
  /**
204
238
  *
@@ -366,4 +400,4 @@ declare function formatNewLines(str: any): any;
366
400
  * */
367
401
  declare function getVariable(propertyName: any): string;
368
402
 
369
- 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 };
403
+ 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, validateTrigger };
package/dist/index.d.mts CHANGED
@@ -130,8 +130,9 @@ declare function clone(data: any, times?: number): any;
130
130
  * formatTime(1541927611000); //2018-11-11 17:13:21
131
131
  * formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
132
132
  * formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
133
- * formatTime(new Date()); //2018-11-11 17:13:21
134
- * formatTime(new Date().getTime()); //2018-11-11 17:13:21
133
+ * formatTime(new Date()); // 2018-11-11 17:13:21
134
+ * formatTime(new Date().getTime()); // 2018-11-11 17:13:21
135
+ * formatTime('1764128798.456'); // 2025/11/26 11:11:38
135
136
  */
136
137
  declare function formatTime(time: any, cFormat?: string): any;
137
138
  /**
@@ -145,15 +146,15 @@ declare function formatTime(time: any, cFormat?: string): any;
145
146
  declare function formatDurationTime(timestamp: any, cFormat?: string): string;
146
147
  /**
147
148
  * 生成 UUID
148
- * @param {string} [type=''] - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number' 或空字符串
149
- * @param {number} [length=4] - 生成字符串的长度
150
- * @param {object} [options={}] - 额外的选项
151
- * @param {string} [options.emailStr='@qq.com'] - 生成 email 时使用的后缀
152
- * @param {string} [options.timeStr='{m}-{d} {h}:{i}:{s}'] - 生成时间字符串的格式
153
- * @param {string} [options.startStr=''] - 起始字符串
154
- * @param {number|null} [options.optionsIndex=null] - 数组索引
155
- * @returns {string|number} - 生成的 UUID
156
- * uuid("名字") => 名字hc8f
149
+ * @param type - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number', 'ip', 'port' 或空字符串
150
+ * @param length - 生成字符串的长度(默认为4)
151
+ * @param options - 额外的选项
152
+ * @param options.emailStr - 生成 email 时使用的后缀(默认为 '@qq.com')
153
+ * @param options.timeStr - 生成时间字符串的格式(默认为 '{m}-{d} {h}:{i}:{s}'
154
+ * @param options.startStr - 起始字符串(默认为空)
155
+ * @param options.optionsIndex - 数组索引(默认为随机)
156
+ * @returns 生成的 UUID (字符串或数字)
157
+ * * uuid("名字") => 名字hc8f
157
158
  * uuid() => abcd
158
159
  * uuid('time') => 25MR 10-27 17:34:01
159
160
  * uuid('time', 0, {startStr:'andy', timeStr:"{h}:{i}:{s}"}) => andy 17:38:23
@@ -162,12 +163,15 @@ declare function formatDurationTime(timestamp: any, cFormat?: string): string;
162
163
  * uuid('number') => 2319
163
164
  * uuid([ { label: "小泽泽", value: "xzz" },{ label: "小月月", value: "xyy" }]) => xzz
164
165
  */
165
- declare function uuid(type?: string, length?: number, { emailStr, timeStr, startStr, optionsIndex }?: {
166
- emailStr?: string | undefined;
167
- timeStr?: string | undefined;
168
- startStr?: string | undefined;
169
- optionsIndex?: null | undefined;
170
- }): any;
166
+ declare function uuid(type?: string | Array<{
167
+ label: string;
168
+ value: any;
169
+ }>, length?: number, options?: {
170
+ emailStr?: string;
171
+ timeStr?: string;
172
+ startStr?: string;
173
+ optionsIndex?: number | null;
174
+ }): string | number;
171
175
  /**
172
176
  * 判断传入参数的类型
173
177
  * @param {*} type
@@ -179,7 +183,36 @@ declare function uuid(type?: string, length?: number, { emailStr, timeStr, start
179
183
  * getType(123) number
180
184
  */
181
185
  declare function getType(type: any): string;
182
- declare function sleep(delay?: number, fn?: () => void): Promise<unknown>;
186
+ /**
187
+ * 一个辅助函数,用于在代码中创建一个暂停(延迟)。
188
+ * 它返回一个 Promise,你可以在 `await` 后使用它来实现类似 "sleep" 的效果。
189
+ *
190
+ * @param delay - 等待的毫秒数。默认值为 0,表示不延迟。
191
+ * @param fn - (可选) 一个在延迟结束后立即执行的函数。
192
+ *
193
+ * @returns 一个 Promise,当延迟结束后解析(resolve)。
194
+ *
195
+ * @example
196
+ * // 基本用法:延迟 2 秒后打印消息
197
+ * console.log('开始');
198
+ * await sleep(2000);
199
+ * console.log('2秒后执行');
200
+ *
201
+ * @example
202
+ * // 带回调函数的用法:延迟 1 秒后执行清理工作
203
+ * sleep(1000, () => {
204
+ * console.log('执行清理操作...');
205
+ * // 清理代码...
206
+ * });
207
+ *
208
+ * @example
209
+ * // 在循环中使用:每次迭代后延迟 500 毫秒
210
+ * for (let i = 0; i < 5; i++) {
211
+ * console.log(`当前值: ${i}`);
212
+ * await sleep(500);
213
+ * }
214
+ */
215
+ declare function sleep(delay?: number, fn?: () => void): Promise<void>;
183
216
  /** @使用方式
184
217
  * 1. 在el-form中使用
185
218
  name: [ proxy.validate('name', { message: '你干嘛哈哈' })],
@@ -199,6 +232,7 @@ confirmRegPwd: [
199
232
  let ip = proxy.validate('ip', 122322, true)
200
233
  let custom = proxy.validate('custom', { value: -123, reg: /^-\d+\.?\d{0,2}$/ }, true)
201
234
  */
235
+ declare function validateTrigger(type?: string, rules?: {}, pureValid?: boolean): any;
202
236
  declare function validate(type?: string, rules?: {}, pureValid?: boolean): any;
203
237
  /**
204
238
  *
@@ -366,4 +400,4 @@ declare function formatNewLines(str: any): any;
366
400
  * */
367
401
  declare function getVariable(propertyName: any): string;
368
402
 
369
- 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 };
403
+ 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, validateTrigger };
package/dist/index.d.ts CHANGED
@@ -130,8 +130,9 @@ declare function clone(data: any, times?: number): any;
130
130
  * formatTime(1541927611000); //2018-11-11 17:13:21
131
131
  * formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
132
132
  * formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
133
- * formatTime(new Date()); //2018-11-11 17:13:21
134
- * formatTime(new Date().getTime()); //2018-11-11 17:13:21
133
+ * formatTime(new Date()); // 2018-11-11 17:13:21
134
+ * formatTime(new Date().getTime()); // 2018-11-11 17:13:21
135
+ * formatTime('1764128798.456'); // 2025/11/26 11:11:38
135
136
  */
136
137
  declare function formatTime(time: any, cFormat?: string): any;
137
138
  /**
@@ -145,15 +146,15 @@ declare function formatTime(time: any, cFormat?: string): any;
145
146
  declare function formatDurationTime(timestamp: any, cFormat?: string): string;
146
147
  /**
147
148
  * 生成 UUID
148
- * @param {string} [type=''] - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number' 或空字符串
149
- * @param {number} [length=4] - 生成字符串的长度
150
- * @param {object} [options={}] - 额外的选项
151
- * @param {string} [options.emailStr='@qq.com'] - 生成 email 时使用的后缀
152
- * @param {string} [options.timeStr='{m}-{d} {h}:{i}:{s}'] - 生成时间字符串的格式
153
- * @param {string} [options.startStr=''] - 起始字符串
154
- * @param {number|null} [options.optionsIndex=null] - 数组索引
155
- * @returns {string|number} - 生成的 UUID
156
- * uuid("名字") => 名字hc8f
149
+ * @param type - 生成 UUID 的类型,可以是 'phone', 'email', 'time', 'number', 'ip', 'port' 或空字符串
150
+ * @param length - 生成字符串的长度(默认为4)
151
+ * @param options - 额外的选项
152
+ * @param options.emailStr - 生成 email 时使用的后缀(默认为 '@qq.com')
153
+ * @param options.timeStr - 生成时间字符串的格式(默认为 '{m}-{d} {h}:{i}:{s}'
154
+ * @param options.startStr - 起始字符串(默认为空)
155
+ * @param options.optionsIndex - 数组索引(默认为随机)
156
+ * @returns 生成的 UUID (字符串或数字)
157
+ * * uuid("名字") => 名字hc8f
157
158
  * uuid() => abcd
158
159
  * uuid('time') => 25MR 10-27 17:34:01
159
160
  * uuid('time', 0, {startStr:'andy', timeStr:"{h}:{i}:{s}"}) => andy 17:38:23
@@ -162,12 +163,15 @@ declare function formatDurationTime(timestamp: any, cFormat?: string): string;
162
163
  * uuid('number') => 2319
163
164
  * uuid([ { label: "小泽泽", value: "xzz" },{ label: "小月月", value: "xyy" }]) => xzz
164
165
  */
165
- declare function uuid(type?: string, length?: number, { emailStr, timeStr, startStr, optionsIndex }?: {
166
- emailStr?: string | undefined;
167
- timeStr?: string | undefined;
168
- startStr?: string | undefined;
169
- optionsIndex?: null | undefined;
170
- }): any;
166
+ declare function uuid(type?: string | Array<{
167
+ label: string;
168
+ value: any;
169
+ }>, length?: number, options?: {
170
+ emailStr?: string;
171
+ timeStr?: string;
172
+ startStr?: string;
173
+ optionsIndex?: number | null;
174
+ }): string | number;
171
175
  /**
172
176
  * 判断传入参数的类型
173
177
  * @param {*} type
@@ -179,7 +183,36 @@ declare function uuid(type?: string, length?: number, { emailStr, timeStr, start
179
183
  * getType(123) number
180
184
  */
181
185
  declare function getType(type: any): string;
182
- declare function sleep(delay?: number, fn?: () => void): Promise<unknown>;
186
+ /**
187
+ * 一个辅助函数,用于在代码中创建一个暂停(延迟)。
188
+ * 它返回一个 Promise,你可以在 `await` 后使用它来实现类似 "sleep" 的效果。
189
+ *
190
+ * @param delay - 等待的毫秒数。默认值为 0,表示不延迟。
191
+ * @param fn - (可选) 一个在延迟结束后立即执行的函数。
192
+ *
193
+ * @returns 一个 Promise,当延迟结束后解析(resolve)。
194
+ *
195
+ * @example
196
+ * // 基本用法:延迟 2 秒后打印消息
197
+ * console.log('开始');
198
+ * await sleep(2000);
199
+ * console.log('2秒后执行');
200
+ *
201
+ * @example
202
+ * // 带回调函数的用法:延迟 1 秒后执行清理工作
203
+ * sleep(1000, () => {
204
+ * console.log('执行清理操作...');
205
+ * // 清理代码...
206
+ * });
207
+ *
208
+ * @example
209
+ * // 在循环中使用:每次迭代后延迟 500 毫秒
210
+ * for (let i = 0; i < 5; i++) {
211
+ * console.log(`当前值: ${i}`);
212
+ * await sleep(500);
213
+ * }
214
+ */
215
+ declare function sleep(delay?: number, fn?: () => void): Promise<void>;
183
216
  /** @使用方式
184
217
  * 1. 在el-form中使用
185
218
  name: [ proxy.validate('name', { message: '你干嘛哈哈' })],
@@ -199,6 +232,7 @@ confirmRegPwd: [
199
232
  let ip = proxy.validate('ip', 122322, true)
200
233
  let custom = proxy.validate('custom', { value: -123, reg: /^-\d+\.?\d{0,2}$/ }, true)
201
234
  */
235
+ declare function validateTrigger(type?: string, rules?: {}, pureValid?: boolean): any;
202
236
  declare function validate(type?: string, rules?: {}, pureValid?: boolean): any;
203
237
  /**
204
238
  *
@@ -366,4 +400,4 @@ declare function formatNewLines(str: any): any;
366
400
  * */
367
401
  declare function getVariable(propertyName: any): string;
368
402
 
369
- 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 };
403
+ 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, validateTrigger };
package/dist/index.mjs CHANGED
@@ -229,7 +229,12 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
229
229
  if (typeof time === "object") {
230
230
  date = time;
231
231
  } else {
232
- if (("" + time).length === 10) time = parseInt(time) * 1e3;
232
+ const timeStr = "" + time;
233
+ if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
234
+ time = parseFloat(time) * 1e3;
235
+ } else if (timeStr.length === 10) {
236
+ time = parseInt(time) * 1e3;
237
+ }
233
238
  date = new Date(time);
234
239
  }
235
240
  const formatObj = {
@@ -292,54 +297,63 @@ function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}
292
297
  });
293
298
  return time_str;
294
299
  }
295
- function uuid(type = "", length = 4, { emailStr = "@qq.com", timeStr = "{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = {}) {
296
- let randomStr = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
297
- let res = type;
298
- if (isRef(type)) {
299
- type = unref(type);
300
+ function uuid(type = "", length = 4, options = {}) {
301
+ const { emailStr = "@qq.com", timeStr = "{y}-{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = options;
302
+ function isRef2(obj) {
303
+ return obj && typeof obj === "object" && obj._isRef === true;
304
+ }
305
+ function unref2(ref) {
306
+ return isRef2(ref) ? ref.value : ref;
307
+ }
308
+ function random2(min, max) {
309
+ return Math.floor(Math.random() * (max - min + 1) + min);
300
310
  }
301
- if (getType(type) === "array" && type.length > 0) {
302
- let randNum = random(0, type.length - 1);
303
- if (!length) {
304
- return type[optionsIndex ?? randNum];
311
+ type = unref2(type);
312
+ if (Array.isArray(type)) {
313
+ if (type.length === 0) return "";
314
+ const randIndex = optionsIndex ?? random2(0, type.length - 1);
315
+ const selectedItem = type[randIndex];
316
+ if (typeof selectedItem === "object" && selectedItem !== null && "value" in selectedItem) {
317
+ return selectedItem.value;
305
318
  }
306
- return type[optionsIndex ?? randNum][length === 4 ? "value" : length];
319
+ return selectedItem;
307
320
  }
321
+ let randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
322
+ let result = startStr;
308
323
  if (type === "phone") {
309
- let prefixArray = new Array("130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189");
310
- let i = parseInt(Math.random() * 10);
311
- let res2 = prefixArray[i];
312
- for (var j = 0; j < 8; j++) {
313
- res2 += Math.floor(Math.random() * 10);
324
+ const prefixes = ["130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189"];
325
+ result = prefixes[random2(0, prefixes.length - 1)];
326
+ for (let i = 0; i < 8; i++) {
327
+ result += Math.floor(Math.random() * 10);
314
328
  }
315
- return res2;
329
+ return result;
316
330
  }
317
331
  if (type === "email") {
318
- return uuid(startStr, length) + emailStr;
332
+ result = uuid(startStr, length) + emailStr;
333
+ return result;
319
334
  }
320
335
  if (type === "time") {
321
- return uuid(startStr, length) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
336
+ return uuid(startStr, length, options) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
322
337
  }
323
338
  if (type === "number") {
324
- let randomStr2 = "123456789";
325
- let res2 = "";
326
- for (let i = length; i > 0; --i) {
327
- res2 += randomStr2[Math.floor(Math.random() * randomStr2.length)];
339
+ const numChars = "123456789";
340
+ result = "";
341
+ for (let i = 0; i < length; i++) {
342
+ result += numChars[random2(0, numChars.length - 1)];
328
343
  }
329
- return Number(res2);
344
+ return Number(result);
330
345
  }
331
346
  if (type === "ip") {
332
- let randomNum = random(1, 99);
333
- return `10.0.11.` + randomNum;
347
+ const randomNum = random2(1, 99);
348
+ return `10.0.11.${randomNum}`;
334
349
  }
335
350
  if (type === "port") {
336
- let randomNum = random(1, 65535);
337
- return randomNum;
351
+ return random2(1, 65535);
338
352
  }
339
- for (let i = length; i > 0; --i) {
340
- res += randomStr[Math.floor(Math.random() * randomStr.length)];
353
+ for (let i = 0; i < length; i++) {
354
+ result += randomChars[random2(0, randomChars.length - 1)];
341
355
  }
342
- return res;
356
+ return result;
343
357
  }
344
358
  function getType(type) {
345
359
  if (typeof type === "object") {
@@ -357,6 +371,13 @@ function sleep(delay = 0, fn) {
357
371
  }, delay)
358
372
  );
359
373
  }
374
+ function validateTrigger(type = "required", rules = {}, pureValid = false) {
375
+ let mergeRules = {
376
+ trigger: ["blur", "change"],
377
+ ...rules
378
+ };
379
+ return validate(type, mergeRules, pureValid);
380
+ }
360
381
  function validate(type = "required", rules = {}, pureValid = false) {
361
382
  if (getType(type) === "object") {
362
383
  pureValid = rules || false;
@@ -364,7 +385,7 @@ function validate(type = "required", rules = {}, pureValid = false) {
364
385
  type = "required";
365
386
  }
366
387
  let trigger = rules.trigger || [];
367
- const typeMaps = ["required", "pwd", "number", "mobile", "between", "length", "same", "ip", "port", "custom"];
388
+ const typeMaps = ["required", "pwd", "number", "mobile", "email", "between", "length", "same", "ip", "port", "custom"];
368
389
  let parseRequired = rules.required ?? true;
369
390
  if (!typeMaps.includes(type)) {
370
391
  return {
@@ -409,6 +430,9 @@ function validate(type = "required", rules = {}, pureValid = false) {
409
430
  if (type === "mobile") {
410
431
  return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7", pureValid, /^[1][0-9]{10}$/);
411
432
  }
433
+ if (type === "email") {
434
+ return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684email", pureValid, /^[^\s@]+@[^\s@]+\.[^\s@]+$/);
435
+ }
412
436
  if (type === "ip") {
413
437
  return _validValue(
414
438
  rules,
@@ -418,17 +442,17 @@ function validate(type = "required", rules = {}, pureValid = false) {
418
442
  );
419
443
  }
420
444
  if (type === "between") {
421
- let min = rules.min || 1;
422
- let max = rules.max || 10;
445
+ let min = rules.min;
446
+ let max = rules.max;
423
447
  const validateBetween = (rule, value, callback) => {
424
- let validFlag = /^[0-9]+$/.test(value);
448
+ let validFlag = /^-?[0-9]+$/.test(value);
425
449
  if (!validFlag) {
426
450
  callback(new Error("\u8BF7\u8F93\u5165\u6570\u5B57"));
427
451
  }
428
- if (value < min) {
452
+ if (value < min && min !== void 0) {
429
453
  callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5C0F\u4E8E${min}`));
430
454
  }
431
- if (value > max) {
455
+ if (value > max && max !== void 0) {
432
456
  callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5927\u4E8E${max}`));
433
457
  }
434
458
  callback();
@@ -648,26 +672,15 @@ function formatBytes(bytes, options = {}) {
648
672
  } else {
649
673
  return bytes;
650
674
  }
651
- if (bytes <= 0) {
652
- return bytes.toFixed(digit) + " B";
675
+ if (bytes <= 1) {
676
+ return Math[roundType](bytes * Math.pow(10, digit)) / Math.pow(10, digit) + " B";
653
677
  }
654
678
  const k = 1024;
655
679
  const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
656
680
  const i = Math.floor(Math.log(bytes) / Math.log(k));
657
681
  const power = Math.pow(k, i);
658
682
  let num = bytes / power;
659
- switch (roundType) {
660
- case "ceil":
661
- num = Math.ceil(num * Math.pow(10, digit)) / Math.pow(10, digit);
662
- break;
663
- case "round":
664
- num = Math.round(num * Math.pow(10, digit)) / Math.pow(10, digit);
665
- break;
666
- case "floor":
667
- default:
668
- num = Math.floor(num * Math.pow(10, digit)) / Math.pow(10, digit);
669
- break;
670
- }
683
+ num = Math[roundType](num * Math.pow(10, digit)) / Math.pow(10, digit);
671
684
  let res = num.toFixed(digit) + " " + sizes[i];
672
685
  if (thousands) {
673
686
  res = formatThousands(res);
@@ -804,4 +817,4 @@ function getVariable(propertyName) {
804
817
  return res;
805
818
  }
806
819
 
807
- 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 };
820
+ 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, validateTrigger };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oeos-components/utils",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -18,14 +18,14 @@
18
18
  "stub": "npx unbuild --stub",
19
19
  "version": "git add . && git commit -am \"chore: release v$npm_package_version\"",
20
20
  "postversion": "git push && git push origin --tags && npm publish",
21
- "release": "npx unbuild && npm version patch && gg \"chore: release v$(npm pkg get version | sed 's/\"//g')\" && npm publish"
21
+ "release": "npx unbuild && npm version patch && gg \"chore: release @oeos-components/utils v$(npm pkg get version | sed 's/\"//g')\" && npm publish"
22
22
  },
23
23
  "author": "",
24
24
  "dependencies": {
25
25
  "@vue/reactivity": "^3.5.18",
26
26
  "@vue/shared": "^3.5.18",
27
27
  "consola": "^3.4.2",
28
- "element-plus": "^2.10.4",
28
+ "element-plus": "^2.11.5",
29
29
  "es-toolkit": "^1.39.10",
30
30
  "lodash-es": "^4.17.21",
31
31
  "vue": "3.4.15"