17564: Change file size unit base from 1000 to 1024
[arvados-workbench2.git] / src / common / formatters.ts
index 1386338c900197a2aa2d918f11a02195d6e88729..779809f1569494da93c599706fd1d977201df46a 100644 (file)
@@ -2,8 +2,8 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { PropertyValue } from "~/models/search-bar";
-import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "~/models/vocabulary";
+import { PropertyValue } from "models/search-bar";
+import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "models/vocabulary";
 
 export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
     if (isoDate) {
@@ -20,15 +20,20 @@ export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
     return "(none)";
 };
 
-export const formatFileSize = (size?: number) => {
+export const formatFileSize = (size?: number | string) => {
     if (typeof size === "number") {
+        if (size === 0) { return "0 B"; }
+
         for (const { base, unit } of FILE_SIZES) {
             if (size >= base) {
                 return `${(size / base).toFixed()} ${unit}`;
             }
         }
     }
-    return "";
+    if ((typeof size === "string" && size === '') || size === undefined) {
+        return '';
+    }
+    return "0 B";
 };
 
 export const formatTime = (time: number, seconds?: boolean) => {
@@ -61,19 +66,19 @@ export function formatUploadSpeed(prevLoaded: number, loaded: number, prevTime:
 
 const FILE_SIZES = [
     {
-        base: 1000000000000,
+        base: 1099511627776,
         unit: "TB"
     },
     {
-        base: 1000000000,
+        base: 1073741824,
         unit: "GB"
     },
     {
-        base: 1000000,
+        base: 1048576,
         unit: "MB"
     },
     {
-        base: 1000,
+        base: 1024,
         unit: "KB"
     },
     {