File size: 1,499 Bytes
1f6c75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * 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;
  }
}