Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.7 KB
Raw Blame
export function formatBytes(bytes) {
    const abs = Math.abs(bytes)
    if (abs >= 1024 * 1024) {
        return `${(bytes / 1024 / 1024).toFixed(2)} MiB`
    }
    if (abs >= 1024) {
        return `${(bytes / 1024).toFixed(1)} KiB`
    }
    return `${bytes} B`
}

// Human-readable change vs a baseline, with an at-a-glance arrow. Used verbatim in the
// header summary and the section table so a reader sees the same phrasing in both.
// `noBaseline` overrides the wording when there is nothing to compare against.
export function formatDelta(bytes, baselineBytes, { noBaseline } = {}) {
    if (baselineBytes === undefined || baselineBytes === null) {
        return noBaseline ?? `🔺 +${formatBytes(bytes)} (new)`
    }
    const delta = bytes - baselineBytes
    if (delta === 0) {
        return 'no change'
    }
    const sign = delta > 0 ? '+' : '-'
    const magnitude = `${delta > 0 ? '🔺' : '🟢'} ${sign}${formatBytes(Math.abs(delta))}`
    if (baselineBytes === 0) {
        return `${magnitude} (new)`
    }
    const percent = ((Math.abs(delta) / baselineBytes) * 100).toFixed(1)
    return `${magnitude} (${sign}${percent}%)`
}

// One total compared against the base branch: the body line, header summary, and status
// for a size section, with the missing-baseline presentation handled once. Growth at or
// above the configured threshold warns, shrink/flat is ok, and no baseline is informational.
export function totalComparison(bytes, baselineBytes, { warningThresholdPercent = 0 } = {}) {
    if (baselineBytes === null || baselineBytes === undefined) {
        return {
            status: 'info',
            summary: 'no base branch to compare',
            totalLine: `**Total:** ${formatBytes(bytes)} _(no base branch measurement to compare against yet)_`,
        }
    }
    const growthPercent = baselineBytes === 0 ? Infinity : ((bytes - baselineBytes) / baselineBytes) * 100
    return {
        status: growthPercent >= warningThresholdPercent && bytes > baselineBytes ? 'warn' : 'ok',
        summary: formatDelta(bytes, baselineBytes),
        totalLine: `**Total:** ${formatBytes(bytes)} · ${formatDelta(bytes, baselineBytes)}`,
    }
}

// Makes untrusted text safe for a cell in a section's markdown table. Section
// writers interpolate PR-controlled strings (file paths, identifiers); git
// allows filenames containing backticks, pipes, and newlines, and the report
// parser treats `<!-- ci-report:section:... -->` sequences in the raw comment
// body as trusted section metadata. Stripping control characters removes the
// newlines those markers require, and removing backticks and pipes keeps the
// surrounding table well-formed.
export function markdownCell(value) {
    return String(value).replace(/[`|\p{C}]/gu, '')
}