mirror of
https://github.com/directus/directus.git
synced 2026-02-02 10:25:05 -05:00
* Declare return types on functions And a very few other type related minor fixes * Minor syntax fixes * Remove unnecessary escape chars in regexes * Remove unnecessary awaits * Replace deprecated req.connection with req.socket * Replace deprecated upload with uploadOne * Remove unnecessary eslint-disable-next-line comments * Comment empty functions / catch or finally clauses * Fix irregular whitespaces * Add missing returns (null) * Remove unreachable code * A few logical fixes * Remove / Handle non-null assertions which are certainly unnecessary (e.g. in tests)
26 lines
624 B
TypeScript
26 lines
624 B
TypeScript
export default function formatFilesize(bytes = 0, decimal = true): string {
|
|
const threshold = decimal ? 1000 : 1024;
|
|
|
|
if (Boolean(bytes) === false) {
|
|
return '--';
|
|
}
|
|
|
|
if (Math.abs(bytes) < threshold) {
|
|
return `${bytes} B`;
|
|
}
|
|
|
|
const units = decimal
|
|
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
|
|
let unitIndex = -1;
|
|
let remainingBytes = bytes;
|
|
|
|
do {
|
|
remainingBytes /= threshold;
|
|
unitIndex += 1;
|
|
} while (Math.abs(remainingBytes) >= threshold && unitIndex < units.length - 1);
|
|
|
|
return `${remainingBytes.toFixed(1)} ${units[unitIndex]}`;
|
|
}
|