From ebec1957e7f50e8937796665dedd6ae480cd3250 Mon Sep 17 00:00:00 2001 From: goodmice Date: Mon, 25 Jul 2022 14:28:15 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/factory.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/components/factory.ts b/src/components/factory.ts index 077ea3e..43df914 100755 --- a/src/components/factory.ts +++ b/src/components/factory.ts @@ -121,11 +121,22 @@ export const downloadFile = async (fileInfo: FileInfoDto) => { } } -export const formatBytes = (bytes: number) => { - if(bytes < 1024) - return `${bytes.toFixed(0)}b` - if(bytes < 1024 * 1024) - return `${(bytes / 1024).toFixed(2)}kb` - else - return `${(bytes / 1024 / 1024).toFixed(2)}Mb` +const bytesUnits = { + ru: { + short: ['Б', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ', 'ЭБ', 'ЗБ', 'ЙБ'], + full: ['байт', 'Килобайт', 'Мегабайт', 'Гигабайт', 'Петабайт', 'Экзабайт', 'Зетабайт', 'Йотабайт'], + }, + en: { + short: ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'], + full: ['byte', 'kibibyte', 'mebibyte', 'gibibyte', 'tebibyte', 'pebibyte', 'exbibyte', 'zebibyte', 'yobubyte'], + } } + +export const formatBytes = (bytes: number, fixed: number = 2, fullname?: boolean, locale: keyof typeof bytesUnits = 'ru') => { + const units = bytesUnits[locale][fullname ? 'full' : 'short'] + for (let i = 0; i < units.length; i++) + if (bytes < 2 ** (10 * (i + 1))) + return `${(bytes / (2 ** (10 * i))).toFixed(fixed)} ${units[i]}` + return `>1024 ${units.at(-1)}` +} +