From e1d4ae15e7f4c3491d90b0463e79d22b7f713285 Mon Sep 17 00:00:00 2001 From: Marc-Aurele Besner <82244926+marc-aurele-besner@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:39:18 -0500 Subject: [PATCH] adding space formatter utils functions --- packages/auto-consensus/src/utils/format.ts | 25 +++++++++++++++++++++ packages/auto-consensus/src/utils/index.ts | 1 + 2 files changed, 26 insertions(+) create mode 100644 packages/auto-consensus/src/utils/format.ts diff --git a/packages/auto-consensus/src/utils/format.ts b/packages/auto-consensus/src/utils/format.ts new file mode 100644 index 00000000..30a629b2 --- /dev/null +++ b/packages/auto-consensus/src/utils/format.ts @@ -0,0 +1,25 @@ +// Binary format (powers of 2) +export const formatSpaceToBinary = (value: number, decimals = 2) => { + if (value === 0) return '0 Bytes' + + const k = 1024 + const dm = decimals < 0 ? 0 : decimals + const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] + + const i = Math.floor(Math.log(value) / Math.log(k)) + + return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i] +} + +// Decimal format (powers of 10) +export const formatSpaceToDecimal = (value: number, decimals = 2) => { + if (value === 0) return '0 Bytes' + + const k = 1000 + const dm = decimals < 0 ? 0 : decimals + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] + + const i = Math.floor(Math.log(value) / Math.log(k)) + + return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i] +} diff --git a/packages/auto-consensus/src/utils/index.ts b/packages/auto-consensus/src/utils/index.ts index 01631cc5..6abc4b7f 100644 --- a/packages/auto-consensus/src/utils/index.ts +++ b/packages/auto-consensus/src/utils/index.ts @@ -1,6 +1,7 @@ // file: src/utils/index.ts export * from './events' +export * from './format' export * from './parse' export * from './query' export * from './sudo'