11 lines
747 B
JavaScript
11 lines
747 B
JavaScript
export function value(input) { return input === undefined || input === null || input === '' ? '-' : String(input) }
|
|
export function escapeHtml(text) { return String(text ?? '').replace(/[&<>"']/g, ch => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[ch])) }
|
|
export function formatBytes(input) {
|
|
if (input === undefined || input === null || input === '') return '-'
|
|
const number = Number(input)
|
|
if (!Number.isFinite(number)) return value(input)
|
|
const units = ['B', 'KB', 'MB', 'GB', 'TB']; let size = Math.abs(number); let index = 0
|
|
while (size >= 1024 && index < units.length - 1) { size /= 1024; index += 1 }
|
|
return `${number < 0 ? -size : size.toFixed(size >= 10 || index === 0 ? 0 : 1)} ${units[index]}`
|
|
}
|