sort.ts

Generic topological sort using Kahn's algorithm.

Orders items so that dependencies come before dependents. Works with any item type that has id and optional depends_on.

Declarations
#

3 declarations

view source

Sortable
#

sort.ts view source

Sortable

Minimum shape required for topological sorting.

id

type string

depends_on

type Array<string>

topological_sort
#

sort.ts view source

<T extends Sortable>(items: T[], label?: string): TopologicalSortResult<T>

Sort items by their dependencies using Kahn's algorithm.

Returns items ordered so that dependencies come before dependents. If a cycle is detected, returns an error with the cycle path.

items

Array of items to sort.

type T[]

label

Label for error messages (e.g. "resource", "step").

type string
default 'item'

returns

TopologicalSortResult<T>

Sorted items or error if cycle detected.

TopologicalSortResult
#

Imported by
#