refactor: 将 formatFileSize 重命名为 formatBytes 并移至通用工具模块

This commit is contained in:
雨霖铃
2026-06-10 00:32:29 +08:00
parent 3f73adc015
commit 355a94d116
4 changed files with 8 additions and 28 deletions
+2 -2
View File
@@ -9,7 +9,7 @@
*/
import { Download } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { formatFileSize } from '@/utils/base64Converter';
import { formatBytes } from '@/utils/format';
import { useI18n } from '@/utils/chromeI18n';
interface DecodeResultPaperProps {
@@ -54,7 +54,7 @@ export default function DecodeResultPaper({
{t('inferredMimeType')}: {mimeType}
</span>
<span className="text-xs text-muted-foreground">
{t('decodedSize')}: {formatFileSize(blobSize)}
{t('decodedSize')}: {formatBytes(blobSize)}
</span>
</div>
@@ -27,7 +27,7 @@ describe('DecodeResultPaper 组件', () => {
expect(screen.getByText(/image\/png/)).toBeInTheDocument();
});
it('应通过 formatFileSize 渲染文件大小', () => {
it('应通过 formatBytes 渲染文件大小', () => {
render(<DecodeResultPaper {...{ ...defaultProps, blobSize: 1536 }} />);
expect(screen.getByText(/1\.5 KB/)).toBeInTheDocument();
});
@@ -4,7 +4,8 @@ import { useI18n } from '@/utils/chromeI18n';
import { CopyButton } from '@/components/CopyButton';
import DecodeResultPaper from '@/components/DecodeResultPaper';
import { Button } from '@/components/ui/button';
import { downloadBlob, formatFileSize } from '@/utils/base64Converter';
import { downloadBlob } from '@/utils/base64Converter';
import { formatBytes } from '@/utils/format';
import { useStorageState } from '@/utils/useStorageState';
import type { Base64ConvertDirection } from '@/types/storage';
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
@@ -122,7 +123,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
{info.name}
</span>
<span className="text-xs text-muted-foreground/80 font-mono tabular-nums">
{formatFileSize(info.size)} · {info.type}
{formatBytes(info.size)} · {info.type}
</span>
<span className="text-[11px] font-medium text-primary/80 mt-1">
{t('clickOrDropToReplace')}
@@ -190,14 +191,14 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
<span>
{t('originalSize')}:{' '}
<span className="font-semibold text-foreground/80">
{formatFileSize(result.originalBytes)}
{formatBytes(result.originalBytes)}
</span>
</span>
<span className="text-border/60">|</span>
<span>
{t('encodedSize')}:{' '}
<span className="font-semibold text-foreground/80">
{formatFileSize(result.outputBytes)}
{formatBytes(result.outputBytes)}
</span>
</span>
</div>
@@ -7,7 +7,6 @@ import {
isSupportedImageType,
isSupportedImageExtension,
extractMimeTypeFromDataUri,
formatFileSize,
base64ToBytes,
sniffMimeFromBytes,
base64ToBlob,
@@ -185,26 +184,6 @@ describe('extractMimeTypeFromDataUri', () => {
});
});
describe('formatFileSize', () => {
it('应该格式化字节', () => {
expect(formatFileSize(0)).toBe('0 B');
expect(formatFileSize(512)).toBe('512 B');
});
it('应该格式化 KB', () => {
expect(formatFileSize(1024)).toBe('1.0 KB');
expect(formatFileSize(1536)).toBe('1.5 KB');
});
it('应该格式化 MB', () => {
expect(formatFileSize(1048576)).toBe('1.00 MB');
});
it('应该格式化 GB', () => {
expect(formatFileSize(1073741824)).toBe('1.00 GB');
});
});
describe('base64ToBytes', () => {
it('应该解码标准 ASCII Base64 为字节序列', () => {
const bytes = base64ToBytes('aGVsbG8=');