Skip to content

Commit

Permalink
Simplify(?) function
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Dec 15, 2024
1 parent 5f33751 commit 3f79b8a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions app/javascript/src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

0 comments on commit 3f79b8a

Please sign in to comment.