function parseGraphViz(graphVizStr) {
function parseSrcSide(str) {
const [objectsStr, annotationsStr] = str
.split("[")
.map((x, i) => (i > 0 ? x.slice(0, -1) : x));
const annotations = annotationsStr
? annotationsStr.split(",").reduce((memo, el) => {
const [key, val] = el.split("=");
memo[key] = val;
return memo;
}, {})
: undefined;
return objectsStr.split(",").map(id => ({ id, annotations }));
}
const statements = graphVizStr
.replace(/\s/g, "")
.split(";")
.filter((x) => x);
let id = 0;
return statements.map((s) => {
const [srcString, targetString] = s.split("->");
console.log(srcString);
return {
sources: parseSrcSide(srcString),
targets: targetString ? parseSrcSide(targetString) : []
}
})
.map(({sources, targets}) => {
const edges = sources.flatMap((s,i) => targets.map((t,j) => ({
source: s.id,
target: t.id,
id: id++,
annotations: t.annotations
})));
return {
nodes: [...targets.map(t => ({id: t.id })), ...sources],
edges
};
}).reduce((memo, el) => {
return {
nodes: _.uniqBy([...memo.nodes, ...el.nodes], d=>d.id),
edges: _.uniqBy([...memo.edges, ...el.edges], d=>d.id),
}
}, {nodes:[], edges:[]});
}