From 114114fc32ec8e1e42ff01bb013e20592d5e946a Mon Sep 17 00:00:00 2001 From: xingyu4j Date: Tue, 18 Nov 2025 17:22:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=20?= =?UTF-8?q?utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/adapter/vxe-table.ts | 8 ++------ apps/web-ele/src/adapter/vxe-table.ts | 8 ++------ apps/web-naive/src/adapter/vxe-table.ts | 8 ++------ apps/web-tdesign/src/adapter/vxe-table.ts | 8 ++------ packages/@core/base/shared/src/utils/upload.ts | 8 ++++---- 5 files changed, 12 insertions(+), 28 deletions(-) diff --git a/apps/web-antd/src/adapter/vxe-table.ts b/apps/web-antd/src/adapter/vxe-table.ts index 200ea61d5..d41f39113 100644 --- a/apps/web-antd/src/adapter/vxe-table.ts +++ b/apps/web-antd/src/adapter/vxe-table.ts @@ -16,6 +16,7 @@ import { erpCountInputFormatter, erpNumberFormatter, fenToYuan, + formatFileSize, formatPast2, isFunction, isString, @@ -354,12 +355,7 @@ setupVbenVxeTable({ // add by 星语:文件大小格式化 vxeUI.formats.add('formatFileSize', { tableCellFormatMethod({ cellValue }, digits = 2) { - if (!cellValue) return '0 B'; - const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const index = Math.floor(Math.log(cellValue) / Math.log(1024)); - const size = cellValue / 1024 ** index; - const formattedSize = size.toFixed(digits); - return `${formattedSize} ${unitArr[index]}`; + return formatFileSize(cellValue, digits); }, }); }, diff --git a/apps/web-ele/src/adapter/vxe-table.ts b/apps/web-ele/src/adapter/vxe-table.ts index aef2a9aa1..27917454f 100644 --- a/apps/web-ele/src/adapter/vxe-table.ts +++ b/apps/web-ele/src/adapter/vxe-table.ts @@ -16,6 +16,7 @@ import { erpCountInputFormatter, erpNumberFormatter, fenToYuan, + formatFileSize, formatPast2, isFunction, isString, @@ -344,12 +345,7 @@ setupVbenVxeTable({ // add by 星语:文件大小格式化 vxeUI.formats.add('formatFileSize', { tableCellFormatMethod({ cellValue }, digits = 2) { - if (!cellValue) return '0 B'; - const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const index = Math.floor(Math.log(cellValue) / Math.log(1024)); - const size = cellValue / 1024 ** index; - const formattedSize = size.toFixed(digits); - return `${formattedSize} ${unitArr[index]}`; + return formatFileSize(cellValue, digits); }, }); }, diff --git a/apps/web-naive/src/adapter/vxe-table.ts b/apps/web-naive/src/adapter/vxe-table.ts index 44d2f7bfc..db032db3b 100644 --- a/apps/web-naive/src/adapter/vxe-table.ts +++ b/apps/web-naive/src/adapter/vxe-table.ts @@ -7,6 +7,7 @@ import { erpCountInputFormatter, erpNumberFormatter, fenToYuan, + formatFileSize, formatPast2, } from '@vben/utils'; @@ -205,12 +206,7 @@ setupVbenVxeTable({ vxeUI.formats.add('formatFileSize', { tableCellFormatMethod({ cellValue }, digits = 2) { - if (!cellValue) return '0 B'; - const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const index = Math.floor(Math.log(cellValue) / Math.log(1024)); - const size = cellValue / 1024 ** index; - const formattedSize = size.toFixed(digits); - return `${formattedSize} ${unitArr[index]}`; + return formatFileSize(cellValue, digits); }, }); }, diff --git a/apps/web-tdesign/src/adapter/vxe-table.ts b/apps/web-tdesign/src/adapter/vxe-table.ts index 24caaefb4..d774885cf 100644 --- a/apps/web-tdesign/src/adapter/vxe-table.ts +++ b/apps/web-tdesign/src/adapter/vxe-table.ts @@ -13,6 +13,7 @@ import { erpCountInputFormatter, erpNumberFormatter, fenToYuan, + formatFileSize, formatPast2, } from '@vben/utils'; @@ -211,12 +212,7 @@ setupVbenVxeTable({ // add by 星语:文件大小格式化 vxeUI.formats.add('formatFileSize', { tableCellFormatMethod({ cellValue }, digits = 2) { - if (!cellValue) return '0 B'; - const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const index = Math.floor(Math.log(cellValue) / Math.log(1024)); - const size = cellValue / 1024 ** index; - const formattedSize = size.toFixed(digits); - return `${formattedSize} ${unitArr[index]}`; + return formatFileSize(cellValue, digits); }, }); }, diff --git a/packages/@core/base/shared/src/utils/upload.ts b/packages/@core/base/shared/src/utils/upload.ts index 04f6421ff..d7d3cb208 100644 --- a/packages/@core/base/shared/src/utils/upload.ts +++ b/packages/@core/base/shared/src/utils/upload.ts @@ -110,14 +110,14 @@ export function isImage(filename: null | string | undefined): boolean { * @param bytes 文件大小(字节) * @returns 格式化后的文件大小字符串 */ -export function formatFileSize(bytes: number): string { +export function formatFileSize(bytes: number, digits = 2): string { if (bytes === 0) { return '0 B'; } const k = 1024; - const sizes = ['B', 'KB', 'MB', 'GB']; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return `${Number.parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`; + const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const index = Math.floor(Math.log(bytes) / Math.log(k)); + return `${Number.parseFloat((bytes / k ** index).toFixed(digits))} ${unitArr[index]}`; } /**