Skip to content

Commit aa92777

Browse files
RangerMauveRangerMauve
authored andcommitted
Initial implementation
1 parent c6575ba commit aa92777

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# graphlib-json-graph
22
Converts json-graph definitions into graphlib graphs.
3+
4+
**Note: This is a work in progress**

index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var graphlib = require("graphlib");
2+
var par = require("par");
3+
var prop = require("prop");
4+
5+
var Graph = graphlib.Graph;
6+
7+
module.exports = toGraph;
8+
9+
function toGraph(json) {
10+
var graphDefinitions = json.graphs;
11+
12+
if (graphDefinitions) return graphDefinitions.map(makeGraph);
13+
14+
var graphDefinition = json.graph;
15+
16+
if (graphDefinition)
17+
return makeGraph(graphDefinition);
18+
19+
throw new TypeError("No graphs defined");
20+
}
21+
22+
function makeGraph(graphDefinition) {
23+
var nodes = graphDefinition.nodes || [];
24+
var edges = graphDefinition.edges || [];
25+
26+
var directed = graphDefinition.directed || edges.some(prop("directed"));
27+
28+
var graph = new Graph({
29+
directed: directed,
30+
multigraph: true
31+
});
32+
33+
nodes.forEach(par(addNode, graph));
34+
edges.forEach(par(addEdge, graph));
35+
36+
return graph;
37+
}
38+
39+
function addNode(graph, node) {
40+
var id = node.id;
41+
graph.setNode(id, node);
42+
}
43+
44+
function addEdge(graph, edge) {
45+
var source = edge.source;
46+
var target = edge.target;
47+
var id = edge.id;
48+
graph.setEdge(source, target, edge, label);
49+
}

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "graphlib-json-graph",
3+
"version": "1.0.0",
4+
"description": "Converts json-graph definitions into graphlib graphs.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/jsongraph/graphlib-json-graph.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"graph",
16+
"graphlib"
17+
],
18+
"author": "",
19+
"license": "ISC",
20+
"bugs": {
21+
"url": "https://github.com/jsongraph/graphlib-json-graph/issues"
22+
},
23+
"homepage": "https://github.com/jsongraph/graphlib-json-graph",
24+
"dependencies": {
25+
"graphlib": "^1.0.4",
26+
"par": "^0.3.0",
27+
"prop": "^0.1.1"
28+
}
29+
}

0 commit comments

Comments
 (0)