PlatDetect / frontend /src /utils /formatters.js
haisyam's picture
Update kodingan PlatDetect
1f6c75f
Raw History Blame Contribute Delete
1.5 kB
/**
* Format confidence YOLO menjadi persentase.
* Contoh: 0.87 -> "87%"
*/
export function formatConfidence(value) {
if (value === null || value === undefined) return "-";
const num = parseFloat(value);
if (isNaN(num)) return "-";
return `${Math.round(num * 100)}%`;
}
/**
* Format ukuran file menjadi human-readable.
* Contoh: 1048576 -> "1.0 MB"
*/
export function formatFileSize(bytes) {
if (!bytes) return "";
const units = ["B", "KB", "MB", "GB"];
let unitIndex = 0;
let size = bytes;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(1)} ${units[unitIndex]}`;
}
/**
* Validasi apakah file merupakan gambar yang didukung.
*/
export function isValidImageFile(file) {
const validTypes = [
"image/jpeg",
"image/jpg",
"image/png",
"image/webp",
];
return validTypes.includes(file.type);
}
/**
* Cek apakah ukuran file tidak melebihi batas (default 10MB).
*/
export function isFileSizeValid(file, maxSizeMB = 10) {
return file.size <= maxSizeMB * 1024 * 1024;
}
/**
* Format waktu deteksi.
* Contoh: "2026-06-08 10:30:12" -> "08 Jun 2026, 10:30"
*/
export function formatDateTime(dateStr) {
if (!dateStr) return "-";
try {
const date = new Date(dateStr);
return date.toLocaleDateString("id-ID", {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
});
} catch {
return dateStr;
}
}