All files / apps/developer-portal/layers/decisions/shared decisionReports.ts

100% Statements 39/39
86.11% Branches 31/36
100% Functions 13/13
100% Lines 35/35

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                                        35x 46x 74x   66x   66x                                     35x       17x   17x 44x 4x 40x 4x 36x   12x       35x         35x 8x   35x 35x   35x 38x 67x 44x 5x 39x 4x   35x       35x 60x         20x 21x     20x       35x 11x 6x    
import type { DecisionOutcome, DecisionStatus } from "#layers/snapshot/shared/decisions.ts";
import type { DocPage } from "#layers/snapshot/shared/types/DocPage.ts";
import { DECISION_STATUSES } from "#layers/snapshot/shared/decisions.ts";
import { stripLeadingEmoji } from "#layers/docs/shared/docPages.ts";
 
export interface DecisionReport {
    /** The repo path, `docs/decisions/0009-comment-discipline.md`. */
    path: string;
    /** The filename without its extension — the segment the `/decisions/:id` route carries. */
    id: string;
    number: string;
    title: string;
    status: DecisionStatus | null;
    decision: DecisionOutcome | null;
    supersededBy: string | null;
    updatedAt: string | null;
    words: number;
}
 
/** Everything the collector already parsed off a report's frontmatter, as one row per file. */
export const toDecisionReports = (pages: DocPage[]): DecisionReport[] =>
    pages
        .filter((page) => page.kind === "decision")
        .map((page) => {
            const id = (page.path.split("/").at(-1) ?? page.path).replace(/\.md$/i, "");
 
            return {
                path: page.path,
                id,
                number: id.slice(0, 4),
                title: stripLeadingEmoji(page.title),
                status: page.decisionStatus,
                decision: page.decisionOutcome,
                supersededBy: page.decisionSupersededBy,
                updatedAt: page.updatedAt,
                words: page.words,
            };
        });
 
interface DecisionReportFilter {
    status?: DecisionStatus | "all";
    decision?: DecisionOutcome | "all";
    search?: string;
}
 
export const filterDecisions = (
    reports: DecisionReport[],
    filter: DecisionReportFilter,
): DecisionReport[] => {
    const needle = filter.search?.trim().toLowerCase() ?? "";
 
    return reports.filter((report) => {
        if (filter.status && filter.status !== "all" && report.status !== filter.status)
            return false;
        if (filter.decision && filter.decision !== "all" && report.decision !== filter.decision)
            return false;
        if (needle.length === 0) return true;
 
        return `${report.number} ${report.title}`.toLowerCase().includes(needle);
    });
};
 
export const DECISION_SORTS = ["newest", "oldest", "updated", "status"] as const;
 
export type DecisionSort = (typeof DECISION_SORTS)[number];
 
/** A report whose `status` didn't parse sorts last rather than under a guessed value. */
const statusRank = (status: DecisionStatus | null): number =>
    status === null ? DECISION_STATUSES.length : DECISION_STATUSES.indexOf(status);
 
const byNumberDescending = (a: DecisionReport, b: DecisionReport): number =>
    b.number.localeCompare(a.number);
 
export const sortDecisions = (reports: DecisionReport[], sort: DecisionSort): DecisionReport[] =>
    reports.slice().sort((a, b) => {
        if (sort === "oldest") return a.number.localeCompare(b.number);
        if (sort === "updated")
            return (b.updatedAt ?? "").localeCompare(a.updatedAt ?? "") || byNumberDescending(a, b);
        if (sort === "status")
            return statusRank(a.status) - statusRank(b.status) || byNumberDescending(a, b);
 
        return byNumberDescending(a, b);
    });
 
/** Seeded from the vocabulary, so a status nothing carries yet still shows its zero. */
export const countByStatus = (reports: DecisionReport[]): Record<DecisionStatus, number> => {
    const counts = Object.fromEntries(DECISION_STATUSES.map((status) => [status, 0])) as Record<
        DecisionStatus,
        number
    >;
 
    for (const report of reports) {
        if (report.status !== null) counts[report.status] += 1;
    }
 
    return counts;
};
 
/** Accepted decisions still waiting to land, newest first; a superseded one never lands. */
export const toImplement = (reports: DecisionReport[]): DecisionReport[] =>
    sortDecisions(reports, "newest").filter(
        (report) => report.status === "to-implement" && report.decision !== "superseded",
    );