From a430284aa21e3ae1f0d5654e55b2ad2852519cc2 Mon Sep 17 00:00:00 2001 From: wwf <yearningwang@iqtogether.com> Date: 星期三, 04 六月 2025 15:17:49 +0800 Subject: [PATCH] 初始化 --- utils/format.ts | 56 +++++++++++++++++--------------------------------------- 1 files changed, 17 insertions(+), 39 deletions(-) diff --git a/utils/format.ts b/utils/format.ts index 720c8f6..1eeb6af 100644 --- a/utils/format.ts +++ b/utils/format.ts @@ -1,8 +1,8 @@ -/** - * Formats a number with comma separators. - * @example formatNumber(1234567) will return '1,234,567' - * @example formatNumber(1234567.89) will return '1,234,567.89' - */ +/* +* Formats a number with comma separators. + formatNumber(1234567) will return '1,234,567' + formatNumber(1234567.89) will return '1,234,567.89' +*/ export const formatNumber = (num: number | string) => { if (!num) return num @@ -11,48 +11,26 @@ return parts.join('.') } -/** - * Format file size into standard string format. - * @param fileSize file size (Byte) - * @example formatFileSize(1024) will return '1.00KB' - * @example formatFileSize(1024 * 1024) will return '1.00MB' - */ -export const formatFileSize = (fileSize: number) => { - if (!fileSize) - return fileSize +export const formatFileSize = (num: number) => { + if (!num) + return num const units = ['', 'K', 'M', 'G', 'T', 'P'] let index = 0 - while (fileSize >= 1024 && index < units.length) { - fileSize = fileSize / 1024 + while (num >= 1024 && index < units.length) { + num = num / 1024 index++ } - return `${fileSize.toFixed(2)}${units[index]}B` + return `${num.toFixed(2)}${units[index]}B` } -/** - * Format time into standard string format. - * @example formatTime(60) will return '1.00 min' - * @example formatTime(60 * 60) will return '1.00 h' - */ -export const formatTime = (seconds: number) => { - if (!seconds) - return seconds +export const formatTime = (num: number) => { + if (!num) + return num const units = ['sec', 'min', 'h'] let index = 0 - while (seconds >= 60 && index < units.length) { - seconds = seconds / 60 + while (num >= 60 && index < units.length) { + num = num / 60 index++ } - return `${seconds.toFixed(2)} ${units[index]}` -} - -export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => { - const url = window.URL.createObjectURL(data) - const a = document.createElement('a') - a.href = url - a.download = fileName - document.body.appendChild(a) - a.click() - a.remove() - window.URL.revokeObjectURL(url) + return `${num.toFixed(2)} ${units[index]}` } -- Gitblit v1.8.0