All files / infrastructure/scripts/src/brand/domain contrast.ts

95.65% Statements 44/46
81.48% Branches 22/27
100% Functions 16/16
97.29% Lines 36/37

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        1x   1x   1x                   1x 8x   8x 8x 1x 1x             7x 8x     17x     17x       1x 3x 9x 3x     3x       3x 15x           1x 6x 6x 6x     1x 6x 6x   6x     1x 3x             2x   1x   3x   1x 2x         3x      
import { parse, wcagContrast } from "culori";
import { ExpectedError } from "../../shared/errors.ts";
 
/** WCAG 2.1 AA for body-size text. */
export const MINIMUM = 4.5;
 
const ACTION_ROLES = ["--ui-primary", "--ui-secondary", "--ui-error"];
 
const VAR = /^var\(\s*(--[\w-]+)\s*(?:,[^)]*)?\)$/;
 
interface Pair {
    text: string;
    surface: string;
}
 
export type Row = Pair & { token: string; surfaceToken: string; ratio: number };
 
/** Follows nested `var()` references down to a literal value. */
export const resolve = (map: Map<string, string>, name: string, seen: string[] = []): string => {
    Iif (seen.includes(name)) throw new ExpectedError(`${[...seen, name].join(" → ")} is a cycle.`);
 
    const value = map.get(name);
    if (value === undefined) {
        const from = seen.at(-1);
        throw new ExpectedError(
            from === undefined
                ? `${name} is not defined.`
                : `${from} points at ${name}, which is not defined.`,
        );
    }
 
    const reference = VAR.exec(value.trim())?.[1];
    return reference === undefined ? value.trim() : resolve(map, reference, [...seen, name]);
};
 
const isTextRole = (name: string): boolean =>
    (name === "--ui-text" || name.startsWith("--ui-text-")) && name !== "--ui-text-inverted";
 
const isSurfaceRole = (name: string): boolean =>
    (name === "--ui-bg" || name.startsWith("--ui-bg-")) && name !== "--ui-bg-inverted";
 
/** Every text role on every surface role, plus inverted text on the inverted surface. */
export const pairs = (map: Map<string, string>): Pair[] => {
    const names = [...map.keys()];
    const texts = [...names.filter(isTextRole), ...ACTION_ROLES.filter((role) => map.has(role))];
    const surfaces = names.filter(isSurfaceRole);
 
    const inverted =
        map.has("--ui-text-inverted") && map.has("--ui-bg-inverted")
            ? [{ text: "--ui-text-inverted", surface: "--ui-bg-inverted" }]
            : [];
 
    return [
        ...texts.flatMap((text) => surfaces.map((surface) => ({ text, surface }))),
        ...inverted,
    ];
};
 
/** The palette token a role names, without its `--color-` prefix, or the literal it holds. */
const tokenOf = (map: Map<string, string>, name: string): string => {
    const value = (map.get(name) ?? "").trim();
    const reference = VAR.exec(value)?.[1];
    return reference === undefined ? value : reference.replace(/^--color-/, "");
};
 
const colourOf = (map: Map<string, string>, name: string): string => {
    const value = resolve(map, name);
    Iif (parse(value) === undefined)
        throw new ExpectedError(`${name} resolves to "${value}", not a colour.`);
    return value;
};
 
export const measure = (map: Map<string, string>): Row[] =>
    pairs(map).map((pair) => ({
        ...pair,
        token: tokenOf(map, pair.text),
        surfaceToken: tokenOf(map, pair.surface),
        ratio: wcagContrast(colourOf(map, pair.text), colourOf(map, pair.surface)),
    }));
 
export const failing = (rows: Row[]): Row[] => rows.filter((row) => row.ratio < MINIMUM);
 
const BELOW = ` — below ${MINIMUM}:1`;
 
const ratioText = (ratio: number): string => `${ratio.toFixed(2)}:1${ratio < MINIMUM ? BELOW : ""}`;
 
export const contrastTable = (rows: Row[]): string =>
    [
        "| Role | Token | Surface | Ratio |",
        "| --- | --- | --- | --- |",
        ...rows.map(
            (row) =>
                `| \`${row.text}\` | \`${row.token}\` | \`${row.surface}\` → \`${row.surfaceToken}\` | ${ratioText(row.ratio)} |`,
        ),
    ].join("\n");