Topological sort
This package is distributed as Javascript, but you can also use it in your TypeScript project.
API
Javascript example
const TopologicalSort = ; // you can pass nodes as a map into constructor:const nodes = ;nodes;nodes;const sortOp = nodes; // ...or add them to existing object instance with addNode() or addNodes():sortOp;sortOp; // then you should add adges between nodessortOp; // from, tosortOp;sortOp;sortOp;sortOp; // sorting is simple: it returns a new map wih sorted elements// if circular dependency is found, sort() operation throws an AssertionErrorconst sorted = sortOp;const sortedKeys = ...sorted; // ['variables', 'mixins', 'block', 'block_mod_val1', 'block_mod_val2'] // values of the `sorted` map are objects with this shape: `{ children, node }`// where node is the node object that you provided// and children is a map which values have the same shapeconst node: variablesObj children: variablesChildren = sorted;const node: blocksObj1 = variablesChildren;const node: blocksObj2 = sorted;; // true
Typescript example
; // TopologicalSort class instances have a map inside.// This map stores the references between your nodes (edges)// "NodesKeyType" is the type for your tree node identifiers// "NodesValueType" is the type for your tree nodes;nodes.set'variables', variablesObj;nodes.set'mixins', mixinsObj;; // `sortedKeys` is a topologically sorted list of node keyssortOp.addEdge'variables', 'mixins';;; // ['variables', 'mixins'] // `sorted` contains all nodes and their children;;;assertblocksObj1 === blocksObj2; // true