refactor: 将 formatFileSize 重命名为 formatBytes 并移至通用工具模块
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
import { Download } from 'lucide-react';
|
import { Download } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { formatFileSize } from '@/utils/base64Converter';
|
import { formatBytes } from '@/utils/format';
|
||||||
import { useI18n } from '@/utils/chromeI18n';
|
import { useI18n } from '@/utils/chromeI18n';
|
||||||
|
|
||||||
interface DecodeResultPaperProps {
|
interface DecodeResultPaperProps {
|
||||||
@@ -54,7 +54,7 @@ export default function DecodeResultPaper({
|
|||||||
{t('inferredMimeType')}: {mimeType}
|
{t('inferredMimeType')}: {mimeType}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-xs text-muted-foreground">
|
||||||
{t('decodedSize')}: {formatFileSize(blobSize)}
|
{t('decodedSize')}: {formatBytes(blobSize)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ describe('DecodeResultPaper 组件', () => {
|
|||||||
expect(screen.getByText(/image\/png/)).toBeInTheDocument();
|
expect(screen.getByText(/image\/png/)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应通过 formatFileSize 渲染文件大小', () => {
|
it('应通过 formatBytes 渲染文件大小', () => {
|
||||||
render(<DecodeResultPaper {...{ ...defaultProps, blobSize: 1536 }} />);
|
render(<DecodeResultPaper {...{ ...defaultProps, blobSize: 1536 }} />);
|
||||||
expect(screen.getByText(/1\.5 KB/)).toBeInTheDocument();
|
expect(screen.getByText(/1\.5 KB/)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import { useI18n } from '@/utils/chromeI18n';
|
|||||||
import { CopyButton } from '@/components/CopyButton';
|
import { CopyButton } from '@/components/CopyButton';
|
||||||
import DecodeResultPaper from '@/components/DecodeResultPaper';
|
import DecodeResultPaper from '@/components/DecodeResultPaper';
|
||||||
import { Button } from '@/components/ui/button';
|
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 { useStorageState } from '@/utils/useStorageState';
|
||||||
import type { Base64ConvertDirection } from '@/types/storage';
|
import type { Base64ConvertDirection } from '@/types/storage';
|
||||||
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
|
import SwitchButtonGroup from '@/components/SwitchButtonGroup';
|
||||||
@@ -122,7 +123,7 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
|
|||||||
{info.name}
|
{info.name}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-xs text-muted-foreground/80 font-mono tabular-nums">
|
<span className="text-xs text-muted-foreground/80 font-mono tabular-nums">
|
||||||
{formatFileSize(info.size)} · {info.type}
|
{formatBytes(info.size)} · {info.type}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-[11px] font-medium text-primary/80 mt-1">
|
<span className="text-[11px] font-medium text-primary/80 mt-1">
|
||||||
{t('clickOrDropToReplace')}
|
{t('clickOrDropToReplace')}
|
||||||
@@ -190,14 +191,14 @@ export default function Base64ConverterSection({ mode }: Base64ConverterSectionP
|
|||||||
<span>
|
<span>
|
||||||
{t('originalSize')}:{' '}
|
{t('originalSize')}:{' '}
|
||||||
<span className="font-semibold text-foreground/80">
|
<span className="font-semibold text-foreground/80">
|
||||||
{formatFileSize(result.originalBytes)}
|
{formatBytes(result.originalBytes)}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
<span className="text-border/60">|</span>
|
<span className="text-border/60">|</span>
|
||||||
<span>
|
<span>
|
||||||
{t('encodedSize')}:{' '}
|
{t('encodedSize')}:{' '}
|
||||||
<span className="font-semibold text-foreground/80">
|
<span className="font-semibold text-foreground/80">
|
||||||
{formatFileSize(result.outputBytes)}
|
{formatBytes(result.outputBytes)}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import {
|
|||||||
isSupportedImageType,
|
isSupportedImageType,
|
||||||
isSupportedImageExtension,
|
isSupportedImageExtension,
|
||||||
extractMimeTypeFromDataUri,
|
extractMimeTypeFromDataUri,
|
||||||
formatFileSize,
|
|
||||||
base64ToBytes,
|
base64ToBytes,
|
||||||
sniffMimeFromBytes,
|
sniffMimeFromBytes,
|
||||||
base64ToBlob,
|
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', () => {
|
describe('base64ToBytes', () => {
|
||||||
it('应该解码标准 ASCII Base64 为字节序列', () => {
|
it('应该解码标准 ASCII Base64 为字节序列', () => {
|
||||||
const bytes = base64ToBytes('aGVsbG8=');
|
const bytes = base64ToBytes('aGVsbG8=');
|
||||||
|
|||||||
Reference in New Issue
Block a user