Skip to content

Commit 8b9d682

Browse files
committed
feat(utils): add convertToReadableUnit utility function
Signed-off-by: yash37158 <[email protected]>
1 parent d579e08 commit 8b9d682

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/custom/ResourceDetailFormatters/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import {
1515
TextWithLinkFormatter
1616
} from './Formatter';
1717
import { useResourceCleanData } from './useResourceCleanData';
18-
import { extractPodVolumnTables, splitCamelCaseString } from './utils';
18+
import { convertToReadableUnit, extractPodVolumnTables, splitCamelCaseString } from './utils';
1919

2020
export {
2121
CodeFormatter,
2222
CollapsibleSectionFormatter,
2323
ContainerFormatter,
24+
convertToReadableUnit,
25+
extractPodVolumnTables,
2426
KeyValueInRow,
2527
LabelFormatter,
2628
ListFormatter,
@@ -30,10 +32,9 @@ export {
3032
OperatorDataFormatter,
3133
OperatorDynamicFormatter,
3234
SecretFormatter,
35+
splitCamelCaseString,
3336
StatusFormatter,
3437
TableDataFormatter,
3538
TextWithLinkFormatter,
36-
extractPodVolumnTables,
37-
splitCamelCaseString,
3839
useResourceCleanData
3940
};

src/custom/ResourceDetailFormatters/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,17 @@ export function isEmptyAtAllDepths(input: any): boolean {
8989
return _.isEmpty(input);
9090
}
9191
}
92+
93+
export const convertToReadableUnit = (value: number): string => {
94+
if (!value) return '0';
95+
96+
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
97+
let index = 0;
98+
99+
while (value >= 1024 && index < units.length - 1) {
100+
value /= 1024;
101+
index++;
102+
}
103+
104+
return `${value.toFixed(2)} ${units[index]}`;
105+
};

0 commit comments

Comments
 (0)