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)}` +} +