MCPcopy Create free account
hub / github.com/effect-app/libs / tsort

Function tsort

packages/infra/src/routing/tsort.ts:3–40  ·  view source on GitHub ↗
(edges)

Source from the content-addressed store, hash-verified

1// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2// @ts-nocheck
3export function tsort(edges) {
4 const nodes = new Map(), sorted = [], visited = new Map()
5
6 const Node = function(id) {
7 this.id = id
8 this.afters = []
9 }
10
11 edges.forEach((v) => {
12 const from = v[0], to = v[1]
13 if (!nodes.get(from)) nodes.set(from, new Node(from))
14 if (!nodes.get(to)) nodes.set(to, new Node(to))
15 nodes.get(from).afters.push(to)
16 })
17 ;[...nodes.keys()].forEach(function visit(idstr, ancestors) {
18 const node = nodes.get(idstr), id = node.id
19
20 if (visited.get(idstr)) return
21 if (!Array.isArray(ancestors)) ancestors = []
22
23 ancestors.push(id)
24 visited.set(idstr, true)
25 node.afters.forEach(function(afterID) {
26 if (ancestors.indexOf(afterID) >= 0) {
27 throw new Error("closed chain : " + afterID + " is in " + id)
28 }
29 visit(
30 afterID,
31 ancestors.map(function(v) {
32 return v
33 })
34 )
35 })
36 sorted.unshift(id)
37 })
38
39 return sorted
40}
41
42export const createEdges = <T extends { dependsOn?: readonly any[] | undefined }>(dep: readonly T[]) => {
43 const result = []

Callers 1

sortFunction · 0.85

Calls 6

pushMethod · 0.80
forEachMethod · 0.65
getMethod · 0.65
setMethod · 0.65
mapMethod · 0.65
visitFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…