fix(dataset): 统一大小与版本元数据

This commit is contained in:
caoxiaozhu
2026-07-27 12:26:09 +08:00
parent e486d36a80
commit bce586697b
8 changed files with 345 additions and 31 deletions

View 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`
}