-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml-utils.ts
More file actions
92 lines (86 loc) · 2.51 KB
/
Copy pathhtml-utils.ts
File metadata and controls
92 lines (86 loc) · 2.51 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Utility functions for HTML report generation
* Pure functions for text escaping, path truncation, and styling calculations
*/
/**
* Escape HTML special characters to prevent XSS attacks
*
* @param text - Text to escape
* @returns Escaped HTML-safe text
*/
export function escapeHtml(text: string): string {
return (text || '')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/**
* Truncate file path to maximum length with ellipsis
*
* @param path - File path to truncate
* @param max - Maximum length (default: 40)
* @returns Truncated path with ... in the middle
*/
export function truncatePath(path: string, max = 40): string {
if (path.length <= max) return path;
const half = Math.floor((max - 3) / 2);
return path.slice(0, half) + '...' + path.slice(-half);
}
/**
* Map risk percentage to risk band category
*
* @param riskPercent - Risk percentage (0-100)
* @returns Risk band: high, medium, low, or minimal
*/
export function getRiskBand(riskPercent: number): string {
if (riskPercent >= 70) return 'high';
if (riskPercent >= 50) return 'medium';
if (riskPercent >= 30) return 'low';
return 'minimal';
}
/**
* Get CSS width class name based on percentage
*
* @param percentage - Percentage value (0-100)
* @returns Width class name (w-0, w-1, w-2, etc.)
*/
export function getWidthClass(percentage: number): string {
if (percentage <= 0) return 'w-0';
if (percentage <= 1) return 'w-1';
if (percentage <= 2) return 'w-2';
if (percentage <= 3) return 'w-3';
if (percentage <= 4) return 'w-4';
if (percentage <= 5) return 'w-5';
if (percentage <= 10) return 'w-10';
if (percentage <= 15) return 'w-15';
if (percentage <= 20) return 'w-20';
if (percentage <= 25) return 'w-25';
if (percentage <= 30) return 'w-30';
if (percentage <= 40) return 'w-40';
if (percentage <= 50) return 'w-50';
if (percentage <= 60) return 'w-60';
if (percentage <= 70) return 'w-70';
if (percentage <= 80) return 'w-80';
if (percentage <= 90) return 'w-90';
return 'w-100';
}
/**
* Format number for display with thousands separators
*
* @param n - Number to format
* @returns Formatted number string
*/
export function formatNumber(n: number): string {
return new Intl.NumberFormat().format(n);
}
/**
* Format percentage for display
*
* @param n - Percentage value
* @returns Formatted percentage string
*/
export function formatPercentage(n: number): string {
return `${n.toFixed(1)}%`;
}