Skip to content

Commit

Permalink
Merge pull request #168 from autonomys/feat/add-space-formatter-function
Browse files Browse the repository at this point in the history
Adding space formatter utils functions
  • Loading branch information
marc-aurele-besner authored Nov 14, 2024
2 parents f8349f2 + e1d4ae1 commit 8265421
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/auto-consensus/src/utils/format.ts
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]
}
1 change: 1 addition & 0 deletions packages/auto-consensus/src/utils/index.ts
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'

0 comments on commit 8265421

Please sign in to comment.