feat: 统一使用 utils
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
|||||||
erpCountInputFormatter,
|
erpCountInputFormatter,
|
||||||
erpNumberFormatter,
|
erpNumberFormatter,
|
||||||
fenToYuan,
|
fenToYuan,
|
||||||
|
formatFileSize,
|
||||||
formatPast2,
|
formatPast2,
|
||||||
isFunction,
|
isFunction,
|
||||||
isString,
|
isString,
|
||||||
@@ -354,12 +355,7 @@ setupVbenVxeTable({
|
|||||||
// add by 星语:文件大小格式化
|
// add by 星语:文件大小格式化
|
||||||
vxeUI.formats.add('formatFileSize', {
|
vxeUI.formats.add('formatFileSize', {
|
||||||
tableCellFormatMethod({ cellValue }, digits = 2) {
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
||||||
if (!cellValue) return '0 B';
|
return formatFileSize(cellValue, digits);
|
||||||
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]}`;
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
erpCountInputFormatter,
|
erpCountInputFormatter,
|
||||||
erpNumberFormatter,
|
erpNumberFormatter,
|
||||||
fenToYuan,
|
fenToYuan,
|
||||||
|
formatFileSize,
|
||||||
formatPast2,
|
formatPast2,
|
||||||
isFunction,
|
isFunction,
|
||||||
isString,
|
isString,
|
||||||
@@ -344,12 +345,7 @@ setupVbenVxeTable({
|
|||||||
// add by 星语:文件大小格式化
|
// add by 星语:文件大小格式化
|
||||||
vxeUI.formats.add('formatFileSize', {
|
vxeUI.formats.add('formatFileSize', {
|
||||||
tableCellFormatMethod({ cellValue }, digits = 2) {
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
||||||
if (!cellValue) return '0 B';
|
return formatFileSize(cellValue, digits);
|
||||||
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]}`;
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
erpCountInputFormatter,
|
erpCountInputFormatter,
|
||||||
erpNumberFormatter,
|
erpNumberFormatter,
|
||||||
fenToYuan,
|
fenToYuan,
|
||||||
|
formatFileSize,
|
||||||
formatPast2,
|
formatPast2,
|
||||||
} from '@vben/utils';
|
} from '@vben/utils';
|
||||||
|
|
||||||
@@ -205,12 +206,7 @@ setupVbenVxeTable({
|
|||||||
|
|
||||||
vxeUI.formats.add('formatFileSize', {
|
vxeUI.formats.add('formatFileSize', {
|
||||||
tableCellFormatMethod({ cellValue }, digits = 2) {
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
||||||
if (!cellValue) return '0 B';
|
return formatFileSize(cellValue, digits);
|
||||||
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]}`;
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
erpCountInputFormatter,
|
erpCountInputFormatter,
|
||||||
erpNumberFormatter,
|
erpNumberFormatter,
|
||||||
fenToYuan,
|
fenToYuan,
|
||||||
|
formatFileSize,
|
||||||
formatPast2,
|
formatPast2,
|
||||||
} from '@vben/utils';
|
} from '@vben/utils';
|
||||||
|
|
||||||
@@ -211,12 +212,7 @@ setupVbenVxeTable({
|
|||||||
// add by 星语:文件大小格式化
|
// add by 星语:文件大小格式化
|
||||||
vxeUI.formats.add('formatFileSize', {
|
vxeUI.formats.add('formatFileSize', {
|
||||||
tableCellFormatMethod({ cellValue }, digits = 2) {
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
||||||
if (!cellValue) return '0 B';
|
return formatFileSize(cellValue, digits);
|
||||||
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]}`;
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -110,14 +110,14 @@ export function isImage(filename: null | string | undefined): boolean {
|
|||||||
* @param bytes 文件大小(字节)
|
* @param bytes 文件大小(字节)
|
||||||
* @returns 格式化后的文件大小字符串
|
* @returns 格式化后的文件大小字符串
|
||||||
*/
|
*/
|
||||||
export function formatFileSize(bytes: number): string {
|
export function formatFileSize(bytes: number, digits = 2): string {
|
||||||
if (bytes === 0) {
|
if (bytes === 0) {
|
||||||
return '0 B';
|
return '0 B';
|
||||||
}
|
}
|
||||||
const k = 1024;
|
const k = 1024;
|
||||||
const sizes = ['B', 'KB', 'MB', 'GB'];
|
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const index = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
return `${Number.parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`;
|
return `${Number.parseFloat((bytes / k ** index).toFixed(digits))} ${unitArr[index]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user