diff --git a/pages/tools/text/text-stats.vue b/pages/tools/text/text-stats.vue new file mode 100644 index 00000000..fc67ab7c --- /dev/null +++ b/pages/tools/text/text-stats.vue @@ -0,0 +1,89 @@ + + + + + diff --git a/utils/convert.ts b/utils/convert.ts index abe9f983..35065d5f 100644 --- a/utils/convert.ts +++ b/utils/convert.ts @@ -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 }