@oeos-components/utils 0.0.4 → 0.0.5

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
@@ -243,7 +243,7 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
243
243
  });
244
244
  return time_str;
245
245
  }
246
- function formatDurationTime(timestamp, cFormat = "{d} \u5929 {h} \u65F6 {i} \u5206 {s} \u79D2") {
246
+ function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
247
247
  const secondsPerMinute = 60;
248
248
  const minutesPerHour = 60;
249
249
  const hoursPerDay = 24;
@@ -718,6 +718,23 @@ function throttle(fn, delay = 1e3) {
718
718
  }
719
719
  };
720
720
  }
721
+ function tryCatch(promise, sendLoading) {
722
+ const updateLoading = (value) => {
723
+ if (reactivity.isRef(sendLoading)) {
724
+ sendLoading.value = value;
725
+ } else if (sendLoading !== null) {
726
+ console.warn("Cannot modify non-ref sendLoading directly!");
727
+ }
728
+ };
729
+ updateLoading(true);
730
+ return promise.then((data) => {
731
+ updateLoading(false);
732
+ return { data, error: null };
733
+ }).catch((error) => {
734
+ updateLoading(false);
735
+ return { data: null, error };
736
+ });
737
+ }
721
738
  function debounce(fn, delay = 1e3) {
722
739
  let timer = null;
723
740
  return function() {
@@ -790,6 +807,7 @@ exports.sleep = sleep;
790
807
  exports.throttle = throttle;
791
808
  exports.toFixed = toFixed;
792
809
  exports.toLine = toLine;
810
+ exports.tryCatch = tryCatch;
793
811
  exports.uuid = uuid;
794
812
  exports.validForm = validForm;
795
813
  exports.validate = validate;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as element_plus from 'element-plus';
2
+ import { Ref } from '@vue/reactivity';
2
3
 
3
4
  /**
4
5
  * 现有方法如下
@@ -37,15 +38,17 @@ declare const isString: (val: any) => val is string;
37
38
  declare const isStringNumber: (val: string) => boolean;
38
39
  declare const isNumber: (val: any) => val is number;
39
40
  /**
40
- * @example
41
+ * @example1
41
42
  proxy.$toast('保存成功')
42
43
  proxy.$toast('保存失败', 'e')
43
44
  proxy.$toast({
44
45
  message: 'andy',
45
46
  type: 'warning',
47
+ duration: 300,
48
+ closeAll: true,
46
49
  })
47
50
  * $toast.success('This is a success message')
48
- * @example 显示对象
51
+ * @example2 显示对象
49
52
  * $toast({
50
53
  dangerouslyUseHTMLString: true,
51
54
  message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
@@ -266,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
266
269
  toFixed?: number | undefined;
267
270
  }): any;
268
271
  declare function throttle(fn: any, delay?: number): () => void;
272
+ /**
273
+ * 封装 Promise 执行,提供自动 loading 状态管理
274
+ * @param promise 需要执行的 Promise
275
+ * @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
276
+ * @returns Promise<{ data: T | null; error: any }>
277
+ * @example1
278
+ * const loading = ref(false);
279
+ * const { data, error } = await tryCatch(fetchUserData(), loading);
280
+ * @example2 // 无视 loading 状态
281
+ * const { data, error } = await tryCatch(fetchUserData());
282
+ */
283
+ declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
284
+ data: T | null;
285
+ error: any;
286
+ }>;
269
287
  declare function debounce(fn: any, delay?: number): () => void;
270
288
  /**
271
289
  * proxy.confirm('确定删除吗?')
@@ -312,4 +330,4 @@ declare function formatNewLines(str: any): any;
312
330
  * */
313
331
  declare function getVariable(propertyName: any): string;
314
332
 
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 };
333
+ 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,5 @@
1
1
  import * as element_plus from 'element-plus';
2
+ import { Ref } from '@vue/reactivity';
2
3
 
3
4
  /**
4
5
  * 现有方法如下
@@ -37,15 +38,17 @@ declare const isString: (val: any) => val is string;
37
38
  declare const isStringNumber: (val: string) => boolean;
38
39
  declare const isNumber: (val: any) => val is number;
39
40
  /**
40
- * @example
41
+ * @example1
41
42
  proxy.$toast('保存成功')
42
43
  proxy.$toast('保存失败', 'e')
43
44
  proxy.$toast({
44
45
  message: 'andy',
45
46
  type: 'warning',
47
+ duration: 300,
48
+ closeAll: true,
46
49
  })
47
50
  * $toast.success('This is a success message')
48
- * @example 显示对象
51
+ * @example2 显示对象
49
52
  * $toast({
50
53
  dangerouslyUseHTMLString: true,
51
54
  message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
@@ -266,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
266
269
  toFixed?: number | undefined;
267
270
  }): any;
268
271
  declare function throttle(fn: any, delay?: number): () => void;
272
+ /**
273
+ * 封装 Promise 执行,提供自动 loading 状态管理
274
+ * @param promise 需要执行的 Promise
275
+ * @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
276
+ * @returns Promise<{ data: T | null; error: any }>
277
+ * @example1
278
+ * const loading = ref(false);
279
+ * const { data, error } = await tryCatch(fetchUserData(), loading);
280
+ * @example2 // 无视 loading 状态
281
+ * const { data, error } = await tryCatch(fetchUserData());
282
+ */
283
+ declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
284
+ data: T | null;
285
+ error: any;
286
+ }>;
269
287
  declare function debounce(fn: any, delay?: number): () => void;
270
288
  /**
271
289
  * proxy.confirm('确定删除吗?')
@@ -312,4 +330,4 @@ declare function formatNewLines(str: any): any;
312
330
  * */
313
331
  declare function getVariable(propertyName: any): string;
314
332
 
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 };
333
+ 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,5 @@
1
1
  import * as element_plus from 'element-plus';
2
+ import { Ref } from '@vue/reactivity';
2
3
 
3
4
  /**
4
5
  * 现有方法如下
@@ -37,15 +38,17 @@ declare const isString: (val: any) => val is string;
37
38
  declare const isStringNumber: (val: string) => boolean;
38
39
  declare const isNumber: (val: any) => val is number;
39
40
  /**
40
- * @example
41
+ * @example1
41
42
  proxy.$toast('保存成功')
42
43
  proxy.$toast('保存失败', 'e')
43
44
  proxy.$toast({
44
45
  message: 'andy',
45
46
  type: 'warning',
47
+ duration: 300,
48
+ closeAll: true,
46
49
  })
47
50
  * $toast.success('This is a success message')
48
- * @example 显示对象
51
+ * @example2 显示对象
49
52
  * $toast({
50
53
  dangerouslyUseHTMLString: true,
51
54
  message: `<h6>复制成功</h6><pre>${JSON.stringify(obj, null, 2)}</pre>`,
@@ -266,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
266
269
  toFixed?: number | undefined;
267
270
  }): any;
268
271
  declare function throttle(fn: any, delay?: number): () => void;
272
+ /**
273
+ * 封装 Promise 执行,提供自动 loading 状态管理
274
+ * @param promise 需要执行的 Promise
275
+ * @param sendLoading 可选的 loading 状态(支持 Ref<boolean> 或 boolean)
276
+ * @returns Promise<{ data: T | null; error: any }>
277
+ * @example1
278
+ * const loading = ref(false);
279
+ * const { data, error } = await tryCatch(fetchUserData(), loading);
280
+ * @example2 // 无视 loading 状态
281
+ * const { data, error } = await tryCatch(fetchUserData());
282
+ */
283
+ declare function tryCatch<T>(promise: Promise<T>, sendLoading?: Ref<boolean> | null): Promise<{
284
+ data: T | null;
285
+ error: any;
286
+ }>;
269
287
  declare function debounce(fn: any, delay?: number): () => void;
270
288
  /**
271
289
  * proxy.confirm('确定删除吗?')
@@ -312,4 +330,4 @@ declare function formatNewLines(str: any): any;
312
330
  * */
313
331
  declare function getVariable(propertyName: any): string;
314
332
 
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 };
333
+ 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
@@ -240,7 +240,7 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
240
240
  });
241
241
  return time_str;
242
242
  }
243
- function formatDurationTime(timestamp, cFormat = "{d} \u5929 {h} \u65F6 {i} \u5206 {s} \u79D2") {
243
+ function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
244
244
  const secondsPerMinute = 60;
245
245
  const minutesPerHour = 60;
246
246
  const hoursPerDay = 24;
@@ -715,6 +715,23 @@ function throttle(fn, delay = 1e3) {
715
715
  }
716
716
  };
717
717
  }
718
+ function tryCatch(promise, sendLoading) {
719
+ const updateLoading = (value) => {
720
+ if (isRef(sendLoading)) {
721
+ sendLoading.value = value;
722
+ } else if (sendLoading !== null) {
723
+ console.warn("Cannot modify non-ref sendLoading directly!");
724
+ }
725
+ };
726
+ updateLoading(true);
727
+ return promise.then((data) => {
728
+ updateLoading(false);
729
+ return { data, error: null };
730
+ }).catch((error) => {
731
+ updateLoading(false);
732
+ return { data: null, error };
733
+ });
734
+ }
718
735
  function debounce(fn, delay = 1e3) {
719
736
  let timer = null;
720
737
  return function() {
@@ -756,4 +773,4 @@ function getVariable(propertyName) {
756
773
  return res;
757
774
  }
758
775
 
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 };
776
+ 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oeos-components/utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",