Skip to content

Commit e1d4ae1

Browse files
adding space formatter utils functions
1 parent f8349f2 commit e1d4ae1

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Binary format (powers of 2)
2+
export const formatSpaceToBinary = (value: number, decimals = 2) => {
3+
if (value === 0) return '0 Bytes'
4+
5+
const k = 1024
6+
const dm = decimals < 0 ? 0 : decimals
7+
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
8+
9+
const i = Math.floor(Math.log(value) / Math.log(k))
10+
11+
return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
12+
}
13+
14+
// Decimal format (powers of 10)
15+
export const formatSpaceToDecimal = (value: number, decimals = 2) => {
16+
if (value === 0) return '0 Bytes'
17+
18+
const k = 1000
19+
const dm = decimals < 0 ? 0 : decimals
20+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
21+
22+
const i = Math.floor(Math.log(value) / Math.log(k))
23+
24+
return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// file: src/utils/index.ts
22

33
export * from './events'
4+
export * from './format'
45
export * from './parse'
56
export * from './query'
67
export * from './sudo'

0 commit comments

Comments
 (0)