2
0
mirror of https://github.com/moebooru/moebooru synced 2025-08-28 20:47:42 +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) { export function numberToHumanSize (size, precision) {
precision ??= 1; precision ??= 1;
size = Number(size); size = Number(size);
let text; let unit;
if (size.toFixed(0) === '1') { if (size.toFixed(0) === '1') {
text = '1 Byte'; precision = 0;
unit = 'Byte';
} else if (size < 1024) { } else if (size < 1024) {
text = `${size.toFixed(0)} Bytes`; precision = 0;
} else if (size < 1024 * 1024) { unit = 'Bytes';
text = `${(size / 1024).toFixed(precision)} KB`; } else if ((size /= 1024) < 1024) {
} else if (size < 1024 * 1024 * 1024) { unit = 'KB';
text = `${(size / (1024 * 1024)).toFixed(precision)} MB`; } else if ((size /= 1024) < 1024) {
} else if (size < 1024 * 1024 * 1024 * 1024) { unit = 'MB';
text = `${(size / (1024 * 1024 * 1024)).toFixed(precision)} GB`; } else if ((size /= 1024) < 1024) {
unit = 'GB';
} else { } 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}`;
} }