fix(dataset): 统一大小与版本元数据
This commit is contained in:
26
frontend/src/utils/fileSize.ts
Normal file
26
frontend/src/utils/fileSize.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export function parseSizeBytes(value?: string | number | null) {
|
||||
if (typeof value === 'number') return Number.isFinite(value) ? Math.max(0, value) : 0
|
||||
const match = String(value || '').trim().match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB|TB)?$/i)
|
||||
if (!match) return 0
|
||||
const units: Record<string, number> = {
|
||||
B: 1,
|
||||
KB: 1024,
|
||||
MB: 1024 ** 2,
|
||||
GB: 1024 ** 3,
|
||||
TB: 1024 ** 4,
|
||||
}
|
||||
return Number(match[1]) * units[(match[2] || 'B').toUpperCase()]
|
||||
}
|
||||
|
||||
/** 列表统一使用 MB,非零的小文件至少展示为 0.01 MB,避免误显示为零。 */
|
||||
export function formatMegabytes(
|
||||
sizeBytes?: number | null,
|
||||
legacySize?: string | number | null,
|
||||
) {
|
||||
const numericSize = Number(sizeBytes)
|
||||
const bytes = Number.isFinite(numericSize) && numericSize > 0
|
||||
? numericSize
|
||||
: parseSizeBytes(legacySize)
|
||||
if (bytes <= 0) return '0.00 MB'
|
||||
return `${Math.max(bytes / 1024 ** 2, 0.01).toFixed(2)} MB`
|
||||
}
|
||||
Reference in New Issue
Block a user