Skip to content

Commit

Permalink
Merge pull request layer5io#909 from yash37158/master
Browse files Browse the repository at this point in the history
feat(utils): add convertToReadableUnit utility function
  • Loading branch information
amitamrutiya authored Feb 10, 2025
2 parents 38a469e + ecae0a3 commit 6362567
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/custom/ResourceDetailFormatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TextWithLinkFormatter
} from './Formatter';
import { useResourceCleanData } from './useResourceCleanData';
import { extractPodVolumnTables, splitCamelCaseString } from './utils';
import { convertToReadableUnit, extractPodVolumnTables, splitCamelCaseString } from './utils';

export {
CodeFormatter,
Expand All @@ -33,6 +33,7 @@ export {
StatusFormatter,
TableDataFormatter,
TextWithLinkFormatter,
convertToReadableUnit,
extractPodVolumnTables,
splitCamelCaseString,
useResourceCleanData
Expand Down
14 changes: 14 additions & 0 deletions src/custom/ResourceDetailFormatters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ export function isEmptyAtAllDepths(input: any): boolean {
return _.isEmpty(input);
}
}

export const convertToReadableUnit = (value: number): string => {
if (!value) return '0';

const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
let index = 0;

while (value >= 1024 && index < units.length - 1) {
value /= 1024;
index++;
}

return `${value.toFixed(2)} ${units[index]}`;
};

0 comments on commit 6362567

Please sign in to comment.