Skip to content

Commit 6362567

Browse files
authored
Merge pull request #909 from yash37158/master
feat(utils): add convertToReadableUnit utility function
2 parents 38a469e + ecae0a3 commit 6362567

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/custom/ResourceDetailFormatters/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ 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,
@@ -33,6 +33,7 @@ export {
3333
StatusFormatter,
3434
TableDataFormatter,
3535
TextWithLinkFormatter,
36+
convertToReadableUnit,
3637
extractPodVolumnTables,
3738
splitCamelCaseString,
3839
useResourceCleanData

src/custom/ResourceDetailFormatters/utils.ts

+14
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)