File size: 1,764 Bytes
c21180c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Format numbers for display — use abbreviation only for very large numbers */
export function formatNumber(value: any, decimals = 2): string {
  // Coerce to number — backend may return numeric stats as strings
  const num = typeof value === 'number' ? value : Number(value);
  if (value == null || isNaN(num)) return '-';
  if (Math.abs(num) >= 10_000_000) return `${(num / 1_000_000).toFixed(1)}M`;
  if (Math.abs(num) >= 1_000_000) return `${(num / 1_000_000).toFixed(decimals)}M`;
  if (Number.isInteger(num)) return num.toLocaleString();
  return num.toFixed(decimals);
}

/** Format a cell value for display in tables */
export function formatCellValue(value: any): string {
  if (value == null) return '\u2014'; // em dash
  if (typeof value === 'number') {
    // For small integers, just show them plainly
    if (Number.isInteger(value) && Math.abs(value) < 1_000_000) return value.toLocaleString();
    return formatNumber(value);
  }
  if (typeof value === 'boolean') return value ? 'true' : 'false';
  return String(value);
}

/** Return a CSS badge class for column type */
export function typeBadgeClass(columnType: string): string {
  const map: Record<string, string> = {
    numeric: 'badge-numeric',
    categorical: 'badge-categorical',
    datetime: 'badge-datetime',
    text: 'badge-text',
    geo_lat_lon: 'badge-geo',
    boolean: 'badge-boolean',
    unknown: 'badge-text',
  };
  return map[columnType] || 'badge-text';
}

/** Short label for column type */
export function typeLabel(columnType: string): string {
  const map: Record<string, string> = {
    numeric: '#',
    categorical: 'Aa',
    datetime: 'DT',
    text: 'T',
    geo_lat_lon: 'G',
    boolean: 'T/F',
    unknown: '?',
  };
  return map[columnType] || '?';
}