# All files / src/lib/quality-validator/utils formatters.js

0% Statements 0/281
0% Branches 0/1
0% Functions 0/1
0% Lines 0/281

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
    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
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282

     /**
     * Formatting utilities for consistent output across reporters
     * Centralizes all formatting logic to reduce duplication
     */
    import { SEVERITY_ORDER } from './constants.js';
    /**
     * Format a numeric score as a percentage string
     *
     * @param {number} score - The score value (0-100)
     * @param {number} [precision=1] - Number of decimal places
     * @returns {string} Formatted score with percentage symbol
     *
     * @example
     * formatScore(85.567, 1) // Returns: "85.6%"
     * formatScore(92) // Returns: "92.0%"
     */
    export function formatScore(score, precision = 1) {
        return `${score.toFixed(precision)}%`;
    }
    /**
     * Format severity level for consistent display
     *
     * @param {Severity} severity - The severity level
     * @returns {string} Formatted severity with uppercase and padding
     *
     * @example
     * formatSeverity('high') // Returns: "HIGH"
     * formatSeverity('info') // Returns: "INFO"
     */
    export function formatSeverity(severity) {
        return severity.toUpperCase();
    }
    /**
     * Format file location for display in reports
     *
     * @param {FileLocation} location - The file location object
     * @returns {string} Formatted location string
     *
     * @example
     * formatFileLocation({ file: 'src/app.ts', line: 45 })
     * // Returns: "src/app.ts:45"
     */
    export function formatFileLocation(location) {
        if (!location)
            return '';
        const { file, line, column } = location;
        if (line && column) {
            return `${file}:${line}:${column}`;
        }
        return line ? `${file}:${line}` : file;
    }
    /**
     * Format a Finding for display in various report formats
     *
     * @param {Finding} finding - The finding to format
     * @returns {string} Formatted finding text
     *
     * @example
     * const finding = {
     *   title: 'High complexity',
     *   description: 'Function has complexity > 20',
     *   remediation: 'Refactor into smaller functions'
     * };
     * formatFinding(finding);
     * // Returns formatted text representation
     */
    export function formatFinding(finding) {
        const lines = [];
        lines.push(`[${formatSeverity(finding.severity)}] ${finding.title}`);
        lines.push(`Description: ${finding.description}`);
        if (finding.location) {
            lines.push(`Location: ${formatFileLocation(finding.location)}`);
        }
        if (finding.remediation) {
            lines.push(`Remediation: ${finding.remediation}`);
        }
        if (finding.evidence) {
            lines.push(`Evidence: ${finding.evidence}`);
        }
        return lines.join('\n');
    }
    /**
     * Format a Recommendation for display
     *
     * @param {Recommendation} rec - The recommendation to format
     * @param {number} [index=0] - Optional index for numbering
     * @returns {string} Formatted recommendation text
     *
     * @example
     * const rec = {
     *   priority: 'high',
     *   issue: 'Low test coverage',
     *   remediation: 'Add more tests',
     *   estimatedEffort: 'high',
     *   expectedImpact: 'Improved reliability'
     * };
     * formatRecommendation(rec, 1);
     */
    export function formatRecommendation(rec, index = 0) {
        const lines = [];
        if (index > 0) {
            lines.push(`${index}. ${rec.issue}`);
        }
        else {
            lines.push(rec.issue);
        }
        lines.push(`   Priority: ${rec.priority.toUpperCase()}`);
        lines.push(`   Remediation: ${rec.remediation}`);
        lines.push(`   Effort: ${rec.estimatedEffort} | Impact: ${rec.expectedImpact}`);
        return lines.join('\n');
    }
    /**
     * Format a metric value with appropriate unit and precision
     *
     * @param {number} value - The metric value
     * @param {string} unit - The unit label (e.g., '%', 'ms', 'lines')
     * @param {number} [precision=2] - Decimal places
     * @returns {string} Formatted metric string
     *
     * @example
     * formatMetric(92.5, '%') // Returns: "92.50%"
     * formatMetric(1234.567, 'ms', 1) // Returns: "1234.6ms"
     */
    export function formatMetric(value, unit, precision = 2) {
        return `${value.toFixed(precision)}${unit}`;
    }
    /**
     * Format a duration in milliseconds as human-readable time
     *
     * @param {number} ms - Duration in milliseconds
     * @returns {string} Formatted duration string
     *
     * @example
     * formatDuration(1500) // Returns: "1.5s"
     * formatDuration(250) // Returns: "250ms"
     */
    export function formatDuration(ms) {
        if (ms >= 1000) {
            return `${(ms / 1000).toFixed(1)}s`;
        }
        return `${Math.round(ms)}ms`;
    }
    /**
     * Format a file size in bytes as human-readable size
     *
     * @param {number} bytes - Size in bytes
     * @returns {string} Formatted size string
     *
     * @example
     * formatFileSize(1024) // Returns: "1.0KB"
     * formatFileSize(1048576) // Returns: "1.0MB"
     */
    export function formatFileSize(bytes) {
        const units = ['B', 'KB', 'MB', 'GB'];
        let size = bytes;
        let unitIndex = 0;
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        return `${size.toFixed(1)}${units[unitIndex]}`;
    }
    /**
     * Sort findings by severity level
     *
     * @param {Finding[]} findings - Array of findings to sort
     * @returns {Finding[]} Sorted findings (critical → info)
     *
     * @example
     * const findings = [
     *   { severity: 'info', ... },
     *   { severity: 'critical', ... },
     *   { severity: 'medium', ... }
     * ];
     * const sorted = sortFindingsBySeverity(findings);
     * // Returns: [critical, medium, info]
     */
    export function sortFindingsBySeverity(findings) {
        return [...findings].sort((a, b) => {
            const aOrder = SEVERITY_ORDER[a.severity];
            const bOrder = SEVERITY_ORDER[b.severity];
            return aOrder - bOrder;
        });
    }
    /**
     * Group findings by severity level
     *
     * @param {Finding[]} findings - Array of findings to group
     * @returns {Record<string, Finding[]>} Findings grouped by severity
     *
     * @example
     * const findings = [...];
     * const grouped = groupFindingsBySeverity(findings);
     * // Returns: { critical: [...], high: [...], medium: [...], ... }
     */
    export function groupFindingsBySeverity(findings) {
        const grouped = {
            critical: [],
            high: [],
            medium: [],
            low: [],
            info: [],
        };
        for (const finding of findings) {
            if (grouped[finding.severity]) {
                grouped[finding.severity].push(finding);
            }
        }
        return grouped;
    }
    /**
     * Truncate text to maximum length with ellipsis
     *
     * @param {string} text - Text to truncate
     * @param {number} maxLength - Maximum length
     * @param {string} [suffix='...'] - Suffix to append when truncated
     * @returns {string} Truncated text
     *
     * @example
     * truncateText('This is a very long text', 10)
     * // Returns: "This is..."
     */
    export function truncateText(text, maxLength, suffix = '...') {
        if (text.length <= maxLength) {
            return text;
        }
        return text.substring(0, maxLength - suffix.length) + suffix;
    }
    /**
     * Escape HTML special characters for safe rendering
     *
     * @param {string} text - Text to escape
     * @returns {string} HTML-escaped text
     *
     * @example
     * escapeHtml('<script>alert("xss")</script>')
     * // Returns: "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"
     */
    export function escapeHtml(text) {
        const map = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#039;',
        };
        return text.replace(/[&<>"']/g, (char) => map[char]);
    }
    /**
     * Format a status value with color coding suggestion
     *
     * @param {string} status - Status value ('pass', 'fail', 'warning')
     * @returns {object} Object with status and suggested color
     *
     * @example
     * formatStatus('pass')
     * // Returns: { text: 'PASS', color: 'green', icon: '✓' }
     */
    export function formatStatus(status) {
        const statusMap = {
            pass: { text: 'PASS', color: 'green', icon: '✓' },
            fail: { text: 'FAIL', color: 'red', icon: '✗' },
            warning: { text: 'WARNING', color: 'yellow', icon: '⚠' },
        };
        return statusMap[status] || { text: 'UNKNOWN', color: 'gray', icon: '?' };
    }
    /**
     * Create a data-url for inline images
     *
     * @param {string} svgContent - SVG content as string
     * @returns {string} Data URL for SVG
     *
     * @example
     * const svg = '<svg>...</svg>';
     * const dataUrl = createSvgDataUrl(svg);
     * // Returns: "data:image/svg+xml;base64,..."
     */
    export function createSvgDataUrl(svgContent) {
        const encoded = Buffer.from(svgContent).toString('base64');
        return `data:image/svg+xml;base64,${encoded}`;
    }