Skip to content

Commit ea6b3d0

Browse files
committed
added enum to use base 2 or base 10 for calcs
1 parent c5ba4f3 commit ea6b3d0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/utils/convert.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
export function formatBytes(bytes: number, decimals = 2) {
1+
export enum UNIT_BASE {
2+
BASE_2 = 1024,
3+
BASE_10 = 1000
4+
}
5+
6+
export function formatBytes(bytes: number, decimals = 2, base: UNIT_BASE = UNIT_BASE.BASE_2) {
27
if (bytes === 0) {
38
return '0 Bytes';
49
}
510

6-
const k = 1024;
11+
const k = base;
712
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
813
const i = Math.floor(Math.log(bytes) / Math.log(k));
914

0 commit comments

Comments
 (0)