@oeos-components/utils 0.0.13 → 0.0.14

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
@@ -645,7 +645,7 @@ function toFixed(value, options = {}) {
645
645
  return `${prefix}${res}${finalUnit}${suffix}`;
646
646
  }
647
647
  function formatBytes(bytes, options = {}) {
648
- let { digit = 2, thousands = true, prefix = "", suffix = "" } = options;
648
+ let { digit = 2, thousands = true, prefix = "", suffix = "", roundType = "floor" } = options;
649
649
  if (isStringNumber(bytes) || isNumber(bytes)) {
650
650
  bytes = Number(bytes);
651
651
  } else {
@@ -657,7 +657,21 @@ function formatBytes(bytes, options = {}) {
657
657
  const k = 1024;
658
658
  const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
659
659
  const i = Math.floor(Math.log(bytes) / Math.log(k));
660
- let res = (bytes / Math.pow(k, i)).toFixed(digit) + " " + sizes[i];
660
+ const power = Math.pow(k, i);
661
+ 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
+ }
674
+ let res = num.toFixed(digit) + " " + sizes[i];
661
675
  if (thousands) {
662
676
  res = formatThousands(res);
663
677
  }
package/dist/index.d.cts CHANGED
@@ -274,18 +274,22 @@ declare function toFixed(value: any, options?: {
274
274
  unit?: boolean;
275
275
  } | number): any;
276
276
  /**
277
- * 只有对正整数或者字符串正整数才进行单位的转换,
278
- * 否则返回原始数据
279
- * @example
280
- * proxy.formatBytes(536870912) // 512MB
281
- * proxy.formatBytes(536870912) // 512MB
277
+ * 格式化字节单位
278
+ * @param bytes - 字节数
279
+ * @param options - 配置项
280
+ * @param options.digit - 小数位数(默认2)
281
+ * @param options.thousands - 是否千分位分隔(默认true)
282
+ * @param options.prefix - 前缀(默认空)
283
+ * @param options.suffix - 后缀(默认空)
284
+ * @param options.roundType - 取整方式:'floor'(向下, 默认) | 'ceil'(向上) | 'round'(四舍五入)
282
285
  */
283
- declare function formatBytes(bytes: any, options?: {
286
+ declare function formatBytes(bytes: number | string, options?: {
284
287
  digit?: number;
285
288
  thousands?: boolean;
286
289
  prefix?: string;
287
290
  suffix?: string;
288
- }): any;
291
+ roundType?: 'floor' | 'ceil' | 'round';
292
+ }): string;
289
293
  /**
290
294
  * 字节转数字
291
295
  * @param oBytes
package/dist/index.d.mts CHANGED
@@ -274,18 +274,22 @@ declare function toFixed(value: any, options?: {
274
274
  unit?: boolean;
275
275
  } | number): any;
276
276
  /**
277
- * 只有对正整数或者字符串正整数才进行单位的转换,
278
- * 否则返回原始数据
279
- * @example
280
- * proxy.formatBytes(536870912) // 512MB
281
- * proxy.formatBytes(536870912) // 512MB
277
+ * 格式化字节单位
278
+ * @param bytes - 字节数
279
+ * @param options - 配置项
280
+ * @param options.digit - 小数位数(默认2)
281
+ * @param options.thousands - 是否千分位分隔(默认true)
282
+ * @param options.prefix - 前缀(默认空)
283
+ * @param options.suffix - 后缀(默认空)
284
+ * @param options.roundType - 取整方式:'floor'(向下, 默认) | 'ceil'(向上) | 'round'(四舍五入)
282
285
  */
283
- declare function formatBytes(bytes: any, options?: {
286
+ declare function formatBytes(bytes: number | string, options?: {
284
287
  digit?: number;
285
288
  thousands?: boolean;
286
289
  prefix?: string;
287
290
  suffix?: string;
288
- }): any;
291
+ roundType?: 'floor' | 'ceil' | 'round';
292
+ }): string;
289
293
  /**
290
294
  * 字节转数字
291
295
  * @param oBytes
package/dist/index.d.ts CHANGED
@@ -274,18 +274,22 @@ declare function toFixed(value: any, options?: {
274
274
  unit?: boolean;
275
275
  } | number): any;
276
276
  /**
277
- * 只有对正整数或者字符串正整数才进行单位的转换,
278
- * 否则返回原始数据
279
- * @example
280
- * proxy.formatBytes(536870912) // 512MB
281
- * proxy.formatBytes(536870912) // 512MB
277
+ * 格式化字节单位
278
+ * @param bytes - 字节数
279
+ * @param options - 配置项
280
+ * @param options.digit - 小数位数(默认2)
281
+ * @param options.thousands - 是否千分位分隔(默认true)
282
+ * @param options.prefix - 前缀(默认空)
283
+ * @param options.suffix - 后缀(默认空)
284
+ * @param options.roundType - 取整方式:'floor'(向下, 默认) | 'ceil'(向上) | 'round'(四舍五入)
282
285
  */
283
- declare function formatBytes(bytes: any, options?: {
286
+ declare function formatBytes(bytes: number | string, options?: {
284
287
  digit?: number;
285
288
  thousands?: boolean;
286
289
  prefix?: string;
287
290
  suffix?: string;
288
- }): any;
291
+ roundType?: 'floor' | 'ceil' | 'round';
292
+ }): string;
289
293
  /**
290
294
  * 字节转数字
291
295
  * @param oBytes
package/dist/index.mjs CHANGED
@@ -642,7 +642,7 @@ function toFixed(value, options = {}) {
642
642
  return `${prefix}${res}${finalUnit}${suffix}`;
643
643
  }
644
644
  function formatBytes(bytes, options = {}) {
645
- let { digit = 2, thousands = true, prefix = "", suffix = "" } = options;
645
+ let { digit = 2, thousands = true, prefix = "", suffix = "", roundType = "floor" } = options;
646
646
  if (isStringNumber(bytes) || isNumber(bytes)) {
647
647
  bytes = Number(bytes);
648
648
  } else {
@@ -654,7 +654,21 @@ function formatBytes(bytes, options = {}) {
654
654
  const k = 1024;
655
655
  const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
656
656
  const i = Math.floor(Math.log(bytes) / Math.log(k));
657
- let res = (bytes / Math.pow(k, i)).toFixed(digit) + " " + sizes[i];
657
+ const power = Math.pow(k, i);
658
+ 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
+ }
671
+ let res = num.toFixed(digit) + " " + sizes[i];
658
672
  if (thousands) {
659
673
  res = formatThousands(res);
660
674
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oeos-components/utils",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",