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.

view source

Declarations
#

3 declarations

Sortable
#

sort.ts view source

Sortable import type {Sortable} from '@fuzdev/fuz_util/sort.js';

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> import {topological_sort} from '@fuzdev/fuz_util/sort.js';

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

generics

topological_sort<T extends Sortable>
T
constraint Sortable

TopologicalSortResult
#

sort.ts view source

TopologicalSortResult<T> import type {TopologicalSortResult} from '@fuzdev/fuz_util/sort.js';

Result of topological sort.

generics

TopologicalSortResult<T extends Sortable>
T
constraint Sortable

Imported by
#