From ea6b3d0c3885d5a14349333c74b05ee4b4c5dff3 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Mon, 29 Jul 2024 13:52:05 +0000 Subject: [PATCH] added enum to use base 2 or base 10 for calcs --- src/utils/convert.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/utils/convert.ts b/src/utils/convert.ts index 9eac1921..f834a1c3 100644 --- a/src/utils/convert.ts +++ b/src/utils/convert.ts @@ -1,9 +1,14 @@ -export function formatBytes(bytes: number, decimals = 2) { +export enum UNIT_BASE { + BASE_2 = 1024, + BASE_10 = 1000 +} + +export function formatBytes(bytes: number, decimals = 2, base: UNIT_BASE = UNIT_BASE.BASE_2) { if (bytes === 0) { return '0 Bytes'; } - const k = 1024; + const k = base; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k));