« get me outta code hell

preload, map and format sizes of additional files - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-05-12 23:23:17 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-05-12 23:23:17 -0300
commit3b770c69507ef139cd07f5335aefba33217d43ad (patch)
tree8168c79d18163418b8bedad227ecff436c5d5917 /src/data
parent589273c5e1d63e5f68ecedd68c9d4b49c36b25f4 (diff)
preload, map and format sizes of additional files
Diffstat (limited to 'src/data')
-rw-r--r--src/data/things.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/data/things.js b/src/data/things.js
index 80e22e3e..1865ee41 100644
--- a/src/data/things.js
+++ b/src/data/things.js
@@ -1649,6 +1649,28 @@ Object.assign(Language.prototype, {
         return this.intl_listUnit.format(array);
     },
 
+    // File sizes: 42.5 kB, 127.2 MB, 4.13 GB, 998.82 TB
+    formatFileSize(bytes) {
+        if (!bytes) return '';
+
+        bytes = parseInt(bytes);
+        if (isNaN(bytes)) return '';
+
+        const round = exp => Math.round(bytes / 10 ** (exp - 1)) / 10;
+
+        if (bytes >= 10 ** 12) {
+            return this.formatString('count.fileSize.terabytes', {terabytes: round(12)});
+        } else if (bytes >= 10 ** 9) {
+            return this.formatString('count.fileSize.gigabytes', {gigabytes: round(9)});
+        } else if (bytes >= 10 ** 6) {
+            return this.formatString('count.fileSize.megabytes', {megabytes: round(6)});
+        } else if (bytes >= 10 ** 3) {
+            return this.formatString('count.fileSize.kilobytes', {kilobytes: round(3)});
+        } else {
+            return this.formatString('count.fileSize.bytes', {bytes});
+        }
+    },
+
     // TODO: These are hard-coded. Is there a better way?
     countAlbums: countHelper('albums'),
     countCommentaryEntries: countHelper('commentaryEntries', 'entries'),