From 3bb0b9d85c6dd899c65ba03c9fafed804e9bf439 Mon Sep 17 00:00:00 2001 From: zhongzm Date: Wed, 25 Dec 2024 18:49:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/units.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/utils/units.ts b/src/utils/units.ts index 9faed39..4e0e39d 100644 --- a/src/utils/units.ts +++ b/src/utils/units.ts @@ -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 = { - 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' }; } // 时间单位转换