All files / analyzers utils.ts

100% Statements 29/29
80% Branches 12/15
100% Functions 6/6
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 504x   4x 65x 65x     4x 15x   15x     15x   13x 13x 13x                   13x     4x 4x 1x     4x 28x 4x 7x 8x 7x 2x       4x 6x 11x    
let _counter = 0;
 
export function uuid(): string {
  _counter++;
  return `${Date.now()}-${Math.random().toString(36).slice(2)}-${_counter}`;
}
 
export function detectLanguage(filePath: string): string {
  const basename = filePath.split(/[\\/]/).pop() ?? '';
  // Files with no extension or special DevOps names
  const basenameMap: Record<string, string> = {
    Dockerfile: 'Dockerfile', Makefile: 'Makefile', Jenkinsfile: 'Groovy',
  };
  if (basenameMap[basename]) return basenameMap[basename];
 
  const raw = basename.split('.').pop();
  const ext = (raw ? raw.toLowerCase() : '');
  const map: Record<string, string> = {
    ts: 'TypeScript', tsx: 'TypeScript/JSX', js: 'JavaScript', jsx: 'JavaScript/JSX',
    py: 'Python', java: 'Java', cs: 'C#', cpp: 'C++', c: 'C', go: 'Go',
    rs: 'Rust', rb: 'Ruby', php: 'PHP', swift: 'Swift', kt: 'Kotlin',
    sh: 'Shell', bash: 'Bash', ps1: 'PowerShell', sql: 'SQL',
    html: 'HTML', css: 'CSS', scss: 'SCSS', json: 'JSON', yaml: 'YAML', yml: 'YAML',
    md: 'Markdown', xml: 'XML',
    // IaC / DevOps
    tf: 'Terraform', hcl: 'HCL',
  };
  return map[ext] ?? (ext.toUpperCase() || 'Unknown');
}
 
export function truncate(text: string, maxChars: number): string {
  if (text.length <= maxChars) return text;
  return text.substring(0, maxChars) + '\n...[truncated]';
}
 
export function severityToNumber(s: string): number {
  switch (s) {
    case 'critical': return 4;
    case 'high': return 3;
    case 'medium': return 2;
    case 'low': return 1;
    default: return 0;
  }
}
 
export function maxSeverity(severities: string[]): string {
  if (!severities.length) return 'info';
  return severities.sort((a, b) => severityToNumber(b) - severityToNumber(a))[0];
}