Merge branch 'master' into 13840-favorites-service
[arvados-workbench2.git] / src / common / formatters.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export const formatDate = (isoDate: string) => {
6     const date = new Date(isoDate);
7     const text = date.toLocaleString();
8     return text === 'Invalid Date' ? "" : text;
9 };
10
11 export const formatFileSize = (size?: number) => {
12     if (typeof size === "number") {
13         for (const { base, unit } of FILE_SIZES) {
14             if (size >= base) {
15                 return `${(size / base).toFixed()} ${unit}`;
16             }
17         }
18     }
19     return "";
20 };
21
22 const FILE_SIZES = [
23     {
24         base: 1000000000000,
25         unit: "TB"
26     },
27     {
28         base: 1000000000,
29         unit: "GB"
30     },
31     {
32         base: 1000000,
33         unit: "MB"
34     },
35     {
36         base: 1000,
37         unit: "KB"
38     },
39     {
40         base: 1,
41         unit: "B"
42     }
43 ];