mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-29 19:09:13 -04:00
feat(tools): added text stats
This commit is contained in:
parent
67bc09ccfd
commit
7e80f27551
2 changed files with 103 additions and 1 deletions
|
@ -1,7 +1,20 @@
|
|||
const base64ToString = (str: string) => Buffer.from(str, 'base64').toString('utf-8')
|
||||
const stringToBase64 = (str: string) => Buffer.from(str, 'utf-8').toString('base64')
|
||||
|
||||
const formatBytes = (bytes: number, decimals = 2) => {
|
||||
if (bytes === 0) {
|
||||
return '0 Bytes'
|
||||
}
|
||||
|
||||
const k = 1024
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i]
|
||||
}
|
||||
|
||||
export {
|
||||
stringToBase64,
|
||||
base64ToString
|
||||
base64ToString,
|
||||
formatBytes
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue