zjk
2026-07-17 47f583f707d9177486af3ac5c3c7844513691ca1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 统一化存储单位,字节转化为英文缩写`bytes`, `KB`, `MB`, `GB`
 *
 * @param {number} bytes 需要转换的字节大小
 * @returns {string} 转化后的字节字符串
 * @example
 * ```
 * // Output: '1 MB'
 * normalizeSizeUnits(1048576)
 * ```
 */
export function normalizeSizeUnits(bytes: number): string {
  if (bytes === 0)
    return '0 bytes'
 
  const units = ['bytes', 'KB', 'MB', 'GB']
  const index = Math.floor(Math.log(bytes) / Math.log(1024))
  const size = +(bytes / 1024 ** index).toFixed(2)
  const unit = units[index]
 
  return `${size} ${unit}`
}