2
0

feat:工具类更新

This commit is contained in:
zhongzm
2024-12-25 18:49:31 +08:00
parent caa97e7783
commit 3bb0b9d85c

View File

@@ -25,15 +25,16 @@ export function formatBandwidth(kbpsValue: number): { value: number; unit: Bandw
}
// 流量单位转换
export type StorageUnit = 'KB' | 'MB' | 'GB' | 'TB';
export type StorageUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB';
export const storageUnits: StorageUnit[] = ['KB', 'MB', 'GB', 'TB'];
export const storageUnits: StorageUnit[] = ['B', 'KB', 'MB', 'GB', 'TB'];
export const storageFactors: Record<StorageUnit, number> = {
KB: 1,
MB: 1024,
GB: 1024 * 1024,
TB: 1024 * 1024 * 1024
B: 1,
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024,
TB: 1024 * 1024 * 1024 * 1024
};
export function convertStorage(value: number, fromUnit: StorageUnit, toUnit: StorageUnit): number {
@@ -42,15 +43,17 @@ export function convertStorage(value: number, fromUnit: StorageUnit, toUnit: Sto
return (value * fromFactor) / toFactor;
}
export function formatStorage(kbValue: number): { value: number; unit: StorageUnit } {
if (kbValue >= 1024 * 1024 * 1024) {
return { value: kbValue / (1024 * 1024 * 1024), unit: 'TB' };
} else if (kbValue >= 1024 * 1024) {
return { value: kbValue / (1024 * 1024), unit: 'GB' };
} else if (kbValue >= 1024) {
return { value: kbValue / 1024, unit: 'MB' };
export function formatStorage(byteValue: number): { value: number; unit: StorageUnit } {
if (byteValue >= 1024 * 1024 * 1024 * 1024) {
return { value: byteValue / (1024 * 1024 * 1024 * 1024), unit: 'TB' };
} else if (byteValue >= 1024 * 1024 * 1024) {
return { value: byteValue / (1024 * 1024 * 1024), unit: 'GB' };
} else if (byteValue >= 1024 * 1024) {
return { value: byteValue / (1024 * 1024), unit: 'MB' };
} else if (byteValue >= 1024) {
return { value: byteValue / 1024, unit: 'KB' };
}
return { value: kbValue, unit: 'KB' };
return { value: byteValue, unit: 'B' };
}
// 时间单位转换