2
0
mirror of https://github.com/moebooru/moebooru synced 2025-08-22 09:57:31 +00:00

Simplify(?) function

This commit is contained in:
nanaya 2024-12-15 21:50:16 +09:00
parent 5f33751290
commit 3f79b8afa7

View File

@ -10,20 +10,23 @@ export function distanceSquared (x1, y1, x2, y2) {
export function numberToHumanSize (size, precision) {
precision ??= 1;
size = Number(size);
let text;
let unit;
if (size.toFixed(0) === '1') {
text = '1 Byte';
precision = 0;
unit = 'Byte';
} else if (size < 1024) {
text = `${size.toFixed(0)} Bytes`;
} else if (size < 1024 * 1024) {
text = `${(size / 1024).toFixed(precision)} KB`;
} else if (size < 1024 * 1024 * 1024) {
text = `${(size / (1024 * 1024)).toFixed(precision)} MB`;
} else if (size < 1024 * 1024 * 1024 * 1024) {
text = `${(size / (1024 * 1024 * 1024)).toFixed(precision)} GB`;
precision = 0;
unit = 'Bytes';
} else if ((size /= 1024) < 1024) {
unit = 'KB';
} else if ((size /= 1024) < 1024) {
unit = 'MB';
} else if ((size /= 1024) < 1024) {
unit = 'GB';
} else {
text = `${(size / (1024 * 1024 * 1024 * 1024)).toFixed(precision)} TB`;
size /= 1024;
unit = 'TB';
}
return text.replace(/([0-9]\.\d*?)0+ /, '$1 ').replace('. ', ' ');
return `${size.toLocaleString(undefined, { maximumFractionDigits: precision })} ${unit}`;
}