UNPKG

nx

Version:

The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.

42 lines (41 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.topologicalSort = topologicalSort; /** * Topologically sorts a directed graph, returning the sorted nodes. * Handles cycles by breaking them where needed. * * @param nodes All nodes in the graph * @param getEdges Function that returns outgoing edges for a node * @returns Topologically sorted list of nodes */ function topologicalSort(nodes, getEdges) { const result = []; const visited = new Set(); const temp = new Set(); function visit(node) { // Node is already in result if (visited.has(node)) { return; } // Cycle detected, skip this edge to break the cycle if (temp.has(node)) { return; } temp.add(node); // Visit all dependencies first for (const dep of getEdges(node)) { visit(dep); } temp.delete(node); visited.add(node); result.push(node); } // Visit all nodes for (const node of nodes) { if (!visited.has(node)) { visit(node); } } return result; }