All files / apps/developer-portal/layers/benchmarks/app/utils benchmarks.ts

100% Statements 44/44
88.63% Branches 39/44
100% Functions 18/18
100% Lines 39/39

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            5x     5x           5x   5x                     5x 21x     5x 21x   21x           5x 12x 27x 26x 26x   21x   21x                       5x 8x               5x 5x   5x 16x 3x 13x 11x   4x             5x 28x   5x 12x       14x       5x 12x         4x 8x     4x       5x 8x 4x    
import type { Benchmark } from "../types/Benchmark.ts";
import type { BenchmarkVerdict } from "../types/BenchmarkVerdict.ts";
import { DOC_ASSETS } from "#layers/docs/shared/docLinks.ts";
import { stripLeadingEmoji } from "#layers/docs/shared/docPages.ts";
 
/** Ordered from most to least worth studying, which is also how the gallery sorts. */
export const BENCHMARK_VERDICTS = ["strong", "partial", "avoid"] as const;
 
/** Each entry opens with its label in bold, as `docs/benchmarks/TEMPLATE.md` lays it out. */
const BENCHMARK_VERDICT_LABELS = {
    strong: "Strong reference",
    partial: "Take one thing",
    avoid: "Avoid",
} as const satisfies Record<BenchmarkVerdict, string>;
 
export const BENCHMARKS_PATH = "/docs/benchmarks/";
 
const FURNITURE = new Set(["readme", "template"]);
 
export interface BenchmarkPage {
    path: string;
    title?: string;
    description?: string;
    url?: string;
    kind?: string;
    verdict?: string;
}
 
const isVerdict = (value: string | undefined): value is BenchmarkVerdict =>
    (BENCHMARK_VERDICTS as readonly string[]).includes(value ?? "");
 
/** The page's first paragraph minus the label it opens with, which the verdict pill already shows. */
const sentenceOf = (description: string, verdict: BenchmarkVerdict | null): string => {
    const label = verdict ? `${BENCHMARK_VERDICT_LABELS[verdict]}.` : null;
 
    return label && description.startsWith(label)
        ? description.slice(label.length).trim()
        : description;
};
 
/** One row per site; the folder's README and TEMPLATE are documentation about the library, not entries. */
export const toBenchmarks = (pages: BenchmarkPage[]): Benchmark[] =>
    pages
        .filter((page) => page.path.startsWith(BENCHMARKS_PATH))
        .map((page) => ({ page, id: page.path.slice(BENCHMARKS_PATH.length) }))
        .filter(({ id }) => !id.includes("/") && !FURNITURE.has(id))
        .map(({ page, id }) => {
            const verdict = isVerdict(page.verdict) ? page.verdict : null;
 
            return {
                path: page.path,
                id,
                title: stripLeadingEmoji(page.title ?? id),
                url: page.url ?? null,
                kind: page.kind ?? null,
                verdict,
                description: sentenceOf(page.description ?? "", verdict),
            };
        });
 
/** Where the build serves an entry's screenshot; `docs/benchmarks/README.md` names it after the file. */
export const screenshotOf = (benchmark: Benchmark): string =>
    `${DOC_ASSETS}${BENCHMARKS_PATH}images/${benchmark.id}.webp`;
 
interface BenchmarkFilter {
    verdict?: BenchmarkVerdict | "all";
    kind?: string;
    search?: string;
}
 
export const filterBenchmarks = (benchmarks: Benchmark[], filter: BenchmarkFilter): Benchmark[] => {
    const needle = filter.search?.trim().toLowerCase() ?? "";
 
    return benchmarks.filter((benchmark) => {
        if (filter.verdict && filter.verdict !== "all" && benchmark.verdict !== filter.verdict)
            return false;
        if (filter.kind && filter.kind !== "all" && benchmark.kind !== filter.kind) return false;
        if (needle.length === 0) return true;
 
        return `${benchmark.title} ${benchmark.url ?? ""} ${benchmark.description}`
            .toLowerCase()
            .includes(needle);
    });
};
 
/** A verdict that didn't parse sorts last rather than under a guessed value. */
const verdictRank = (verdict: BenchmarkVerdict | null): number =>
    verdict === null ? BENCHMARK_VERDICTS.length : BENCHMARK_VERDICTS.indexOf(verdict);
 
export const sortBenchmarks = (benchmarks: Benchmark[]): Benchmark[] =>
    benchmarks
        .slice()
        .sort(
            (a, b) =>
                verdictRank(a.verdict) - verdictRank(b.verdict) || a.title.localeCompare(b.title),
        );
 
/** Seeded from the vocabulary, so a verdict nothing carries yet still shows its zero. */
export const countByVerdict = (benchmarks: Benchmark[]): Record<BenchmarkVerdict, number> => {
    const counts = Object.fromEntries(BENCHMARK_VERDICTS.map((verdict) => [verdict, 0])) as Record<
        BenchmarkVerdict,
        number
    >;
 
    for (const benchmark of benchmarks) {
        Eif (benchmark.verdict !== null) counts[benchmark.verdict] += 1;
    }
 
    return counts;
};
 
/** Every kind some entry carries, for the filter, in a stable order. */
export const kindsOf = (benchmarks: Benchmark[]): string[] =>
    [...new Set(benchmarks.flatMap((benchmark) => (benchmark.kind ? [benchmark.kind] : [])))].sort(
        (a, b) => a.localeCompare(b),
    );