const toposort = require('toposort')
const cells = {
'A1': '5',
'A2': '=A1*2',
'A3': '=A2+A4',
'A4': '=A2',
}
var values = {}
const makeGraph = () => {
var graph = [];
for (var item in cells) {
const formula = String(cells[item]);
if (!formula.startsWith("=")) {
values[item] = formula;
} else {
var edgesAdded = 0;
const matches = formula.match(/[A-Z][0-9]+/g);
for (var match in matches) {
edgesAdded++;
graph.push([matches[match], item]);
}
if (edgesAdded === 0) {
// This cell has no dependencies, but it is a formula so
// compute its value right now. (e.g. pure math)
values[item] = parse(formula);
}
}
}
return graph
}
const graph = makeGraph()
console.log(graph);
toposort(graph);