aboutsummaryrefslogtreecommitdiffstats
path: root/src/web
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-06-18 21:16:44 -0500
committerJP Dillingham <jp@dillingham.me>2026-06-18 21:16:44 -0500
commitc31a9181f99e4e3be30157c2bfaddacce6568806 (patch)
tree10f2b78c684065444e4e9141890e97508c5d71ab /src/web
parent528e50684d9c57d7733d60b84461babaee6c321b (diff)
downloadslskd-c31a9181f99e4e3be30157c2bfaddacce6568806.tar.gz
add padding options
Diffstat (limited to 'src/web')
-rw-r--r--src/web/src/lib/util.js16
-rw-r--r--src/web/src/lib/util.test.js11
2 files changed, 22 insertions, 5 deletions
diff --git a/src/web/src/lib/util.js b/src/web/src/lib/util.js
index 755546de..caced7ac 100644
--- a/src/web/src/lib/util.js
+++ b/src/web/src/lib/util.js
@@ -19,8 +19,13 @@ export const formatBytesAsUnit = (bytes, unit, decimals = 2) => {
return Number.parseFloat((bytes / k ** sizes[unit]).toFixed(dm));
};
-export const formatBytes = (bytes, decimals = 2) => {
- if (bytes === 0) return '0 B';
+export const formatBytes = (
+ bytes,
+ decimals = 2,
+ padding = 0,
+ paddingCharacter = ' ',
+) => {
+ if (bytes === 0 || bytes < 1) return '0 B';
const k = 1_024;
const dm = decimals < 0 ? 0 : decimals;
@@ -28,9 +33,10 @@ export const formatBytes = (bytes, decimals = 2) => {
const index = Math.floor(Math.log(bytes) / Math.log(k));
- return (
- Number.parseFloat((bytes / k ** index).toFixed(dm)) + ' ' + sizes[index]
- );
+ const number = Number.parseFloat((bytes / k ** index).toFixed(dm));
+ const padded = number.toString().padStart(padding, paddingCharacter);
+
+ return padded + ' ' + sizes[index];
};
export const formatDate = (date) => {
diff --git a/src/web/src/lib/util.test.js b/src/web/src/lib/util.test.js
index 9bf79fab..0113a290 100644
--- a/src/web/src/lib/util.test.js
+++ b/src/web/src/lib/util.test.js
@@ -5,3 +5,14 @@ describe('formatBytesAsUnit', () => {
expect(utils.formatBytesAsUnit(1_234_567, 'MB', 2)).toBe(1.18);
});
});
+
+describe('formatBytes', () => {
+ it('returns 0 B for values under one byte', () => {
+ expect(utils.formatBytes(0.5)).toBe('0 B');
+ });
+
+ it('formats byte values for one and above', () => {
+ expect(utils.formatBytes(1)).toBe('1 B');
+ expect(utils.formatBytes(1_024)).toBe('1 KB');
+ });
+});