-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from autonomys/feat/add-space-formatter-function
Adding space formatter utils functions
- Loading branch information
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Binary format (powers of 2) | ||
export const formatSpaceToBinary = (value: number, decimals = 2) => { | ||
if (value === 0) return '0 Bytes' | ||
|
||
const k = 1024 | ||
const dm = decimals < 0 ? 0 : decimals | ||
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] | ||
|
||
const i = Math.floor(Math.log(value) / Math.log(k)) | ||
|
||
return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i] | ||
} | ||
|
||
// Decimal format (powers of 10) | ||
export const formatSpaceToDecimal = (value: number, decimals = 2) => { | ||
if (value === 0) return '0 Bytes' | ||
|
||
const k = 1000 | ||
const dm = decimals < 0 ? 0 : decimals | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
|
||
const i = Math.floor(Math.log(value) / Math.log(k)) | ||
|
||
return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// file: src/utils/index.ts | ||
|
||
export * from './events' | ||
export * from './format' | ||
export * from './parse' | ||
export * from './query' | ||
export * from './sudo' |