forked from Kishanjay/LacunaV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotify.js
115 lines (103 loc) · 2.65 KB
/
dotify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Converting objects to DOT format
*
* @author Kishan Nirghin
* @date 17-03-2018
* @description
* DOT format is used to describe graphs in a textual manner
* http://www.webgraphviz.com/ can be used as a live demo.
*
* Since DOT is really convenient, but not very JSON/JavaScript friendly,
* this class was born to convert JSON to DOT; or built a DOT object on the fly.
*
* @example object format
* {
* graphName: "schoolgraph",
* graphType: "digraph",
* edges: [{
* node1: "University",
* node2: "Job",
* options: [{
* key: "label",
* value: "2 Years of experience"
* }]
* }]
* }
*
* @example DOT format
* digraph schoolgraph {
* "University" -> "Job" [label="2 Years of experience" color="red"];
* "PreSchool" -> "University" [label="Brains" color="green"];
* }
*/
class Dotify {
constructor(graphType, graphName) {
this.graphType = graphType;
this.graphName = graphName;
this.edges = [];
if (!graphType in GRAPH_TYPE_MAP) {
console.log("Dotify Warning: unknown graphType " + graphType);
}
}
/**
* Adds an edge to the object
*
* @param {String} node1
* @param {String} node2
* @param {Object} options
*/
addEdge(node1, node2, options) {
this.edges.push({
node1: node1,
node2, node2,
options: options
});
}
addEdges(edges) {
edges.forEach(edge => addEdge(edge));
}
/** Creates a JSON object of the dotify data */
getObject() {
return {
graphType: this.graphType,
graphName: this.graphName,
edges: this.edges
}
}
/** returns the representing DOT string */
getDOT() {
var obj = this.getObject();
var DOT = objectToDOT(obj);
return DOT;
}
}
const GRAPH_TYPE_MAP = {
graph: '--',
digraph: '->'
};
/** Converts a JSON object to a DOT string */
function objectToDOT(obj) {
var DOT = [];
DOT.push(obj.graphType);
DOT.push(obj.graphName);
DOT.push("{\n");
obj.edges.forEach((edge) => {
var edgeDOT = `"${edge.node1}" ${GRAPH_TYPE_MAP[obj.graphType]} "${edge.node2}"`;
if (edge.options) {
var edgeDOTOptions = "[";
for (const [key, value] of Object.entries(edge.options)) {
edgeDOTOptions += `${key}="${value}" `;
}
edgeDOTOptions += "]";
edgeDOT += " " + edgeDOTOptions;
}
edgeDOT += ";\n";
DOT.push(edgeDOT);
});
DOT.push("}");
return DOT.join(" ");
}
module.exports = {
Dotify,
objectToDOT
}