Dev Helpers
← Back

Find the most reused prerequisite

Find the most reused prerequisite. Start with typed records and inspect the inferred result.

Open this example in the interactive library

Source code

type Row = { task: string; needs: string[] };
const rows: Row[] = [
  { task: "design", needs: [] },
  { task: "build", needs: ["design"] },
  { task: "test", needs: ["build"] },
  { task: "ship", needs: ["build", "test"] },
];
const result = rows
  .map((r) => ({
    task: r.task,
    uses: rows.filter((x) => x.needs.includes(r.task)).length,
  }))
  .sort((a, b) => b.uses - a.uses)[0];
console.log(JSON.stringify(result, null, 2));

Sample output

{
  "task": "build",
  "uses": 2
}