Dev Helpers
← Back

Build reverse dependency edges

Build reverse dependency edges. 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,
  unlocks: rows.filter((x) => x.needs.includes(r.task)).map((x) => x.task),
}));
console.log(JSON.stringify(result, null, 2));

Sample output

[
  {
    "task": "design",
    "unlocks": [
      "build"
    ]
  },
  {
    "task": "build",
    "unlocks": [
      "test",
      "ship"
    ]
  },
  {
    "task": "test",
    "unlocks": [
      "ship"
    ]
  },
  {
    "task": "ship",
    "unlocks": []
  }
]