@oeos-components/utils 0.0.3 → 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;
@@ -610,19 +610,26 @@ function processWidth(initValue, isBase = false) {
610
610
  }
611
611
  return { width: res };
612
612
  }
613
- function toFixed(value, digits = 2) {
613
+ function toFixed(value, options = {}) {
614
+ if (typeof options === "number") {
615
+ options = { digit: options };
616
+ }
617
+ let { digit = 2, prefix = "", suffix = "", unit = true } = options;
614
618
  let matches = ("" + value).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
615
619
  if (!matches) {
616
620
  return value;
617
621
  }
618
622
  let numericString = matches[1].replace(/\D/g, "");
619
623
  let decimalString = matches[3] ? `.${matches[3]}` : "";
620
- let unit = matches[4] || "";
624
+ let finalUnit = matches[4] || "";
621
625
  let res = numericString;
622
626
  if (isStringNumber(numericString) || isNumber(numericString)) {
623
- res = Number(numericString + decimalString).toFixed(digits);
627
+ res = Number(numericString + decimalString).toFixed(digit);
624
628
  }
625
- return `${res}${unit}`;
629
+ if (!unit) {
630
+ finalUnit = "";
631
+ }
632
+ return `${prefix}${res}${finalUnit}${suffix}`;
626
633
  }
627
634
  function formatBytes(bytes, { toFixed: toFixed2 = 2, thousands = true } = {}) {
628
635
  if (isStringNumber(bytes) || isNumber(bytes)) {
@@ -711,6 +718,23 @@ function throttle(fn, delay = 1e3) {
711
718
  }
712
719
  };
713
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
+ }
714
738
  function debounce(fn, delay = 1e3) {
715
739
  let timer = null;
716
740
  return function() {
@@ -783,6 +807,7 @@ exports.sleep = sleep;
783
807
  exports.throttle = throttle;
784
808
  exports.toFixed = toFixed;
785
809
  exports.toLine = toLine;
810
+ exports.tryCatch = tryCatch;
786
811
  exports.uuid = uuid;
787
812
  exports.validForm = validForm;
788
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>`,
@@ -241,11 +244,17 @@ declare function processWidth(initValue: any, isBase?: boolean): any;
241
244
  /**
242
245
  * 增加小数点
243
246
  * toFixed(22) -> '22.00'
244
- * proxy.toFixed('22') -> '22.00'
245
- * proxy.toFixed('22', 4) -> '22.0000'
246
- * proxy.toFixed('22', 2, true) -> 22
247
+ * toFixed('22') -> '22.00'
248
+ * toFixed('22', 4) -> '22.0000'
249
+ * toFixed('22', 2) -> 22
250
+ * toFixed('22 TB', {prefix: '$', suffix: '%', unit: false}) -> $22.00%
247
251
  */
248
- declare function toFixed(value: any, digits?: number): any;
252
+ declare function toFixed(value: any, options?: {
253
+ digit?: number;
254
+ prefix?: string;
255
+ suffix?: string;
256
+ unit?: boolean;
257
+ } | number): any;
249
258
  /**
250
259
  * 只有对正整数或者字符串正整数才进行单位的转换,
251
260
  * 否则返回原始数据
@@ -260,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
260
269
  toFixed?: number | undefined;
261
270
  }): any;
262
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
+ }>;
263
287
  declare function debounce(fn: any, delay?: number): () => void;
264
288
  /**
265
289
  * proxy.confirm('确定删除吗?')
@@ -306,4 +330,4 @@ declare function formatNewLines(str: any): any;
306
330
  * */
307
331
  declare function getVariable(propertyName: any): string;
308
332
 
309
- 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>`,
@@ -241,11 +244,17 @@ declare function processWidth(initValue: any, isBase?: boolean): any;
241
244
  /**
242
245
  * 增加小数点
243
246
  * toFixed(22) -> '22.00'
244
- * proxy.toFixed('22') -> '22.00'
245
- * proxy.toFixed('22', 4) -> '22.0000'
246
- * proxy.toFixed('22', 2, true) -> 22
247
+ * toFixed('22') -> '22.00'
248
+ * toFixed('22', 4) -> '22.0000'
249
+ * toFixed('22', 2) -> 22
250
+ * toFixed('22 TB', {prefix: '$', suffix: '%', unit: false}) -> $22.00%
247
251
  */
248
- declare function toFixed(value: any, digits?: number): any;
252
+ declare function toFixed(value: any, options?: {
253
+ digit?: number;
254
+ prefix?: string;
255
+ suffix?: string;
256
+ unit?: boolean;
257
+ } | number): any;
249
258
  /**
250
259
  * 只有对正整数或者字符串正整数才进行单位的转换,
251
260
  * 否则返回原始数据
@@ -260,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
260
269
  toFixed?: number | undefined;
261
270
  }): any;
262
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
+ }>;
263
287
  declare function debounce(fn: any, delay?: number): () => void;
264
288
  /**
265
289
  * proxy.confirm('确定删除吗?')
@@ -306,4 +330,4 @@ declare function formatNewLines(str: any): any;
306
330
  * */
307
331
  declare function getVariable(propertyName: any): string;
308
332
 
309
- 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>`,
@@ -241,11 +244,17 @@ declare function processWidth(initValue: any, isBase?: boolean): any;
241
244
  /**
242
245
  * 增加小数点
243
246
  * toFixed(22) -> '22.00'
244
- * proxy.toFixed('22') -> '22.00'
245
- * proxy.toFixed('22', 4) -> '22.0000'
246
- * proxy.toFixed('22', 2, true) -> 22
247
+ * toFixed('22') -> '22.00'
248
+ * toFixed('22', 4) -> '22.0000'
249
+ * toFixed('22', 2) -> 22
250
+ * toFixed('22 TB', {prefix: '$', suffix: '%', unit: false}) -> $22.00%
247
251
  */
248
- declare function toFixed(value: any, digits?: number): any;
252
+ declare function toFixed(value: any, options?: {
253
+ digit?: number;
254
+ prefix?: string;
255
+ suffix?: string;
256
+ unit?: boolean;
257
+ } | number): any;
249
258
  /**
250
259
  * 只有对正整数或者字符串正整数才进行单位的转换,
251
260
  * 否则返回原始数据
@@ -260,6 +269,21 @@ declare function formatBytesConvert(oBytes: any, { thounsand, toFixed }?: {
260
269
  toFixed?: number | undefined;
261
270
  }): any;
262
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
+ }>;
263
287
  declare function debounce(fn: any, delay?: number): () => void;
264
288
  /**
265
289
  * proxy.confirm('确定删除吗?')
@@ -306,4 +330,4 @@ declare function formatNewLines(str: any): any;
306
330
  * */
307
331
  declare function getVariable(propertyName: any): string;
308
332
 
309
- 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;
@@ -607,19 +607,26 @@ function processWidth(initValue, isBase = false) {
607
607
  }
608
608
  return { width: res };
609
609
  }
610
- function toFixed(value, digits = 2) {
610
+ function toFixed(value, options = {}) {
611
+ if (typeof options === "number") {
612
+ options = { digit: options };
613
+ }
614
+ let { digit = 2, prefix = "", suffix = "", unit = true } = options;
611
615
  let matches = ("" + value).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
612
616
  if (!matches) {
613
617
  return value;
614
618
  }
615
619
  let numericString = matches[1].replace(/\D/g, "");
616
620
  let decimalString = matches[3] ? `.${matches[3]}` : "";
617
- let unit = matches[4] || "";
621
+ let finalUnit = matches[4] || "";
618
622
  let res = numericString;
619
623
  if (isStringNumber(numericString) || isNumber(numericString)) {
620
- res = Number(numericString + decimalString).toFixed(digits);
624
+ res = Number(numericString + decimalString).toFixed(digit);
621
625
  }
622
- return `${res}${unit}`;
626
+ if (!unit) {
627
+ finalUnit = "";
628
+ }
629
+ return `${prefix}${res}${finalUnit}${suffix}`;
623
630
  }
624
631
  function formatBytes(bytes, { toFixed: toFixed2 = 2, thousands = true } = {}) {
625
632
  if (isStringNumber(bytes) || isNumber(bytes)) {
@@ -708,6 +715,23 @@ function throttle(fn, delay = 1e3) {
708
715
  }
709
716
  };
710
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
+ }
711
735
  function debounce(fn, delay = 1e3) {
712
736
  let timer = null;
713
737
  return function() {
@@ -749,4 +773,4 @@ function getVariable(propertyName) {
749
773
  return res;
750
774
  }
751
775
 
752
- 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.3",
3
+ "version": "0.0.5",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",