Topological sort
This package is distributed as Javascript, but you can also use it in your TypeScript project.
API
Javascript example
const { TopologicalSort } = require('topological-sort');
const nodes = new Map();
nodes.set('variables', variablesObj);
nodes.set('mixins', mixinsObj);
const sortOp = new TopologicalSort(nodes);
sortOp.addNode('block', blocksObj);
sortOp.addNodes(new Map([
['block_mod_val1', blockModObj1],
['block_mod_val2', blockModObj2]
]));
sortOp.addEdge('variables', 'mixins');
sortOp.addEdge('mixins', 'block');
sortOp.addEdge('variables', 'block');
sortOp.addEdge('block', 'block_mod_val2');
sortOp.addEdge('block', 'block_mod_val1');
const sorted = sortOp.sort();
const sortedKeys = [...sorted.keys()];
const { node: variablesObj, children: variablesChildren } = sorted.get('variables');
const { node: blocksObj1 } = variablesChildren.get('block');
const { node: blocksObj2 } = sorted.get('block');
assert(blocksObj1 === blocksObj2);
Typescript example
import TopologicalSort from 'topological-sort';
const nodes = new Map<NodesKeyType, NodesValueType>();
nodes.set('variables', variablesObj);
nodes.set('mixins', mixinsObj);
const sortOp = new TopologicalSort<NodesKeyType, NodesValueType>(nodes);
sortOp.addEdge('variables', 'mixins');
const sorted = sortOp.sort();
const sortedKeys = [...sorted.keys()];
const { node: variablesObj, children: variablesChildren } = sorted.get('variables');
const { node: blocksObj1 } = variablesChildren.get('block');
const { node: blocksObj2 } = sorted.get('block');
assert(blocksObj1 === blocksObj2);
More info: