All files / apps/developer-portal/app/utils format.ts

100% Statements 40/40
96.66% Branches 29/30
100% Functions 10/10
100% Lines 29/29

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      33x               486x     33x 55x             33x 109x   94x   94x   94x                 264x   109x             33x 235x 132x 15x   11x       33x 24x 9x   5x     33x                 33x 9x   6x 21x   6x           81x     33x 217x  
import type { Tone } from "~/types/Tone";
import type { Translate } from "~/types/Translate";
 
const TONE_CLASSES = {
    good: "tone-good",
    warn: "tone-warn",
    bad: "tone-bad",
    neutral: "",
} as const satisfies Record<Tone, string>;
 
/** Empty for `neutral`, which takes whatever colour its surroundings already have. */
export const toneClass = (tone: Tone): string => TONE_CLASSES[tone];
 
/** A `YYYY-MM-DD` calendar day as "6 Oct", read in UTC so the server and the browser agree. */
export const calendarDay = (day: string, month: "short" | "only" = "short"): string =>
    new Date(`${day}T00:00:00Z`).toLocaleDateString(
        "en-GB",
        month === "only"
            ? { timeZone: "UTC", month: "short" }
            : { timeZone: "UTC", day: "numeric", month: "short" },
    );
 
export const relativeTime = (iso: string | null | undefined, t: Translate): string => {
    if (!iso) return t("common.never");
 
    const seconds = Math.round((Date.now() - new Date(iso).getTime()) / 1000);
 
    const smallest = { unit: "second" as Intl.RelativeTimeFormatUnit, size: 1 };
 
    const scale = [
        smallest,
        { unit: "minute" as Intl.RelativeTimeFormatUnit, size: 60 },
        { unit: "hour" as Intl.RelativeTimeFormatUnit, size: 3600 },
        { unit: "day" as Intl.RelativeTimeFormatUnit, size: 86_400 },
        { unit: "month" as Intl.RelativeTimeFormatUnit, size: 2_592_000 },
        { unit: "year" as Intl.RelativeTimeFormatUnit, size: 31_536_000 },
    ];
 
    const step = scale.findLast((entry) => Math.abs(seconds) >= entry.size) ?? smallest;
 
    return new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }).format(
        -Math.round(seconds / step.size),
        step.unit,
    );
};
 
/** The one threshold pair in the app, so the overview tile and the coverage table cannot disagree. */
export const coverageTone = (pct: number | null | undefined): Tone => {
    if (pct === null || pct === undefined) return "neutral";
    if (pct >= 80) return "good";
    if (pct >= 60) return "warn";
 
    return "bad";
};
 
/** `pnpm audit`'s own five-band scale, collapsed to the app's three tones. */
export const severityTone = (severity: "info" | "low" | "moderate" | "high" | "critical"): Tone => {
    if (severity === "critical" || severity === "high") return "bad";
    if (severity === "moderate") return "warn";
 
    return "neutral";
};
 
const SHORT_UNITS = [
    { unit: "years", size: 31_536_000 },
    { unit: "months", size: 2_592_000 },
    { unit: "days", size: 86_400 },
    { unit: "hours", size: 3600 },
    { unit: "minutes", size: 60 },
] as const;
 
/** "23 h", "2 d": an age that fits a figure column on a phone. */
export const shortAge = (iso: string | null | undefined, t: Translate): string => {
    if (!iso) return t("common.never");
 
    const seconds = Math.max(0, (Date.now() - new Date(iso).getTime()) / 1000);
    const step = SHORT_UNITS.find((entry) => seconds >= entry.size);
 
    return step
        ? t(`common.shortAge.${step.unit}`, { count: Math.floor(seconds / step.size) })
        : t("common.now");
};
 
/** The last path segment, which names a project's page under `/projects/`. */
export const folderOf = (root: string): string => root.split("/").at(-1) ?? root;
 
/** A metric with its unit, or a dash for a project nothing collected it for. */
export const dash = (value: number | null | undefined, unit = ""): string =>
    typeof value === "number" ? `${value}${unit}` : "—";