forked from dagrejs/graphlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var _ = require("lodash"), | ||
Graph = require("./Graph"); | ||
|
||
module.exports = { | ||
write: write, | ||
read: read | ||
}; | ||
|
||
function write(g) { | ||
var json = { | ||
options: { | ||
directed: g.isDirected(), | ||
multigraph: g.isMultigraph(), | ||
compound: g.isCompound() | ||
}, | ||
nodes: writeNodes(g), | ||
edges: writeEdges(g) | ||
}; | ||
if (!_.isUndefined(g.getGraph())) { | ||
json.value = _.clone(g.getGraph()); | ||
} | ||
return json; | ||
} | ||
|
||
function writeNodes(g) { | ||
return _.map(g.nodes(), function(v) { | ||
var nodeValue = g.getNode(v), | ||
parent = g.getParent(v), | ||
node = { v: v }; | ||
if (!_.isUndefined(nodeValue)) { | ||
node.value = nodeValue; | ||
} | ||
if (!_.isUndefined(parent)) { | ||
node.parent = parent; | ||
} | ||
return node; | ||
}); | ||
} | ||
|
||
function writeEdges(g) { | ||
return _.map(g.edges(), function(e) { | ||
var edgeValue = g.getEdge(e), | ||
edge = { v: e.v, w: e.w }; | ||
if (!_.isUndefined(e.name)) { | ||
edge.name = e.name; | ||
} | ||
if (!_.isUndefined(edgeValue)) { | ||
edge.value = edgeValue; | ||
} | ||
return edge; | ||
}); | ||
} | ||
|
||
function read(json) { | ||
var g = new Graph(json.options).setGraph(json.value); | ||
_.each(json.nodes, function(entry) { | ||
g.setNode(entry.v, entry.value); | ||
if (entry.parent) { | ||
g.setParent(entry.v, entry.parent); | ||
} | ||
}); | ||
_.each(json.edges, function(entry) { | ||
g.setEdge({ v: entry.v, w: entry.w, name: entry.name }, entry.value); | ||
}); | ||
return g; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var expect = require("./chai").expect, | ||
Graph = require("..").Graph, | ||
read = require("..").json.read, | ||
write = require("..").json.write; | ||
|
||
describe("json", function() { | ||
it("preserves the graph options", function() { | ||
expect(rw(new Graph({ directed: true })).isDirected()).to.be.true; | ||
expect(rw(new Graph({ directed: false })).isDirected()).to.be.false; | ||
expect(rw(new Graph({ multigraph: true })).isMultigraph()).to.be.true; | ||
expect(rw(new Graph({ multigraph: false })).isMultigraph()).to.be.false; | ||
expect(rw(new Graph({ compound: true })).isCompound()).to.be.true; | ||
expect(rw(new Graph({ compound: false })).isCompound()).to.be.false; | ||
}); | ||
|
||
it("preserves the graph value, if any", function() { | ||
expect(rw(new Graph().setGraph(1)).getGraph()).equals(1); | ||
expect(rw(new Graph().setGraph({ foo: "bar" })).getGraph()).eqls({ foo: "bar" }); | ||
expect(rw(new Graph()).getGraph()).to.be.undefined; | ||
}); | ||
|
||
it("preserves nodes", function() { | ||
expect(rw(new Graph().setNode("a")).hasNode("a")).to.be.true; | ||
expect(rw(new Graph().setNode("a")).getNode("a")).to.be.undefined; | ||
expect(rw(new Graph().setNode("a", 1)).getNode("a")).equals(1); | ||
expect(rw(new Graph().setNode("a", { foo: "bar" })).getNode("a")) | ||
.eqls({ foo: "bar" }); | ||
}); | ||
|
||
it("preserves simple edges", function() { | ||
expect(rw(new Graph().setEdge("a", "b")).hasEdge("a", "b")).to.be.true; | ||
expect(rw(new Graph().setEdge("a", "b")).getEdge("a", "b")).to.be.undefined; | ||
expect(rw(new Graph().setEdge("a", "b", 1)).getEdge("a", "b")).equals(1); | ||
expect(rw(new Graph().setEdge("a", "b", { foo: "bar" })).getEdge("a", "b")) | ||
.eqls({ foo: "bar" }); | ||
}); | ||
|
||
it("preserves multi-edges", function() { | ||
var g = new Graph({ multigraph: true }); | ||
|
||
g.setEdge({ v: "a", w: "b", name: "foo" }); | ||
expect(rw(g).hasEdge("a", "b", "foo")).to.be.true; | ||
|
||
g.setEdge({ v: "a", w: "b", name: "foo" }); | ||
expect(rw(g).getEdge("a", "b", "foo")).to.be.undefined; | ||
|
||
g.setEdge({ v: "a", w: "b", name: "foo" }, 1); | ||
expect(rw(g).getEdge("a", "b", "foo")).equals(1); | ||
|
||
g.setEdge({ v: "a", w: "b", name: "foo" }, { foo: "bar" }); | ||
expect(rw(g).getEdge("a", "b", "foo")).eqls({ foo: "bar" }); | ||
}); | ||
|
||
it("preserves parent / child relationships", function() { | ||
expect(rw(new Graph({ compound: true }).setNode("a")).getParent("a")) | ||
.to.be.undefined; | ||
expect(rw(new Graph({ compound: true }).setParent("a", "parent")).getParent("a")) | ||
.to.equal("parent"); | ||
}); | ||
}); | ||
|
||
function rw(g) { | ||
return read(write(g)); | ||
} |