Skip to content

Commit

Permalink
Add updateNodes / updatePath
Browse files Browse the repository at this point in the history
Remove special behavior for function labels in setNode and setEdge. Use
updateNodes / updatePath instead - these already take an update
function.
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent fde8b40 commit 7e7d7ed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
32 changes: 20 additions & 12 deletions lib/base-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ BaseGraph.prototype.setNodes = function(vs, label) {
var args = arguments;
_.each(vs, function(v) {
if (args.length > 1) {
if (_.isFunction(label)) {
this.setNode(v, label(v));
} else {
this.setNode(v, label);
}
this.setNode(v, label);
} else {
this.setNode(v);
}
Expand All @@ -161,7 +157,14 @@ BaseGraph.prototype.setNodes = function(vs, label) {
};

BaseGraph.prototype.updateNode = function(v, updater) {
return this.setNode(v, updater(this.getNode(v)));
return this.setNode(v, updater(this.getNode(v), v));
};

BaseGraph.prototype.updateNodes = function(vs, updater) {
_.each(vs, function(v) {
this.updateNode(v, updater);
}, this);
return this;
};

BaseGraph.prototype.removeNode = function(v) {
Expand Down Expand Up @@ -250,11 +253,7 @@ BaseGraph.prototype.setPath = function(vs, label) {
args = arguments;
_.reduce(vs, function(v, w) {
if (args.length > 1) {
if (_.isFunction(label)) {
self.setEdge(v, w, label({ v: v, w: w }));
} else {
self.setEdge(v, w, label);
}
self.setEdge(v, w, label);
} else {
self.setEdge(v, w);
}
Expand All @@ -263,8 +262,17 @@ BaseGraph.prototype.setPath = function(vs, label) {
return this;
};

BaseGraph.prototype.updatePath = function(vs, updater) {
var self = this;
_.reduce(vs, function(v, w) {
self.setEdge(v, w, updater(self.getEdge(v, w), { v: v, w: w }));
return w;
});
return this;
};

BaseGraph.prototype.updateEdge = function(v, w, updater) {
return this.setEdge(v, w, updater(this.getEdge(v, w)));
return this.setEdge(v, w, updater(this.getEdge(v, w), { v: v, w: w }));
};

BaseGraph.prototype.removeEdge = function(v, w) {
Expand Down
51 changes: 36 additions & 15 deletions test/base-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,6 @@ exports.tests = function(GraphConstructor) {
expect(g.getNode("c")).to.be.undefined;
});

it("can take a function for the label argument", function() {
g.setNodes(["a", "b", "c"], function(v) {
return v + "-label";
});
expect(g.getNode("a")).to.equal("a-label");
expect(g.getNode("b")).to.equal("b-label");
expect(g.getNode("c")).to.equal("c-label");
});

it("is chainable", function() {
var g2 = g.setNode("key", "label");
expect(g).to.equal(g2);
Expand All @@ -161,12 +152,32 @@ exports.tests = function(GraphConstructor) {
expectSingleNodeGraph(g, "key", "label-new");
});

it("passes in the node's id", function() {
g.setNode("key", "label");
g.updateNode("key", function(prev, v) { return v + "-" + prev; });
expectSingleNodeGraph(g, "key", "key-label");
});

it("is chainable", function() {
var g2 = g.updateNode("key", updater);
expect(g).to.equal(g2);
});
});

describe("updateNodes", function() {
it("updates all of the specified nodes", function() {
g.setNode("a", "label");
g.updateNodes(["a", "b"], function(prev, v) { return v + "-" + prev; });
expect(g.getNode("a")).to.equal("a-label");
expect(g.getNode("b")).to.equal("b-undefined");
});

it("is chainable", function() {
var g2 = g.updateNodes(["key"], function() {});
expect(g).to.equal(g2);
});
});

describe("removeNode", function() {
it("does nothing if the node is not part of the graph", function() {
var removed;
Expand Down Expand Up @@ -381,12 +392,6 @@ exports.tests = function(GraphConstructor) {
expect(g.getEdge("b", "c")).to.be.undefined;
});

it("can take a function as the label argument", function() {
g.setPath(["a", "b", "c"], function(edge) { return edge.v + "-" + edge.w; });
expect(g.getEdge("a", "b")).to.equal("a-b");
expect(g.getEdge("b", "c")).to.equal("b-c");
});

it("is chainable", function() {
var g2 = g.setPath(["a", "b", "c"]);
expect(g).to.equal(g2);
Expand All @@ -413,6 +418,22 @@ exports.tests = function(GraphConstructor) {
});
});

describe("updatePath", function() {
it("updates all edges on the path", function() {
g.setEdge("a", "b", "label");
g.updatePath(["a", "b", "c"], function(prev, edge) {
return edge.v + "->" + edge.w + ":" + prev;
});
expect(g.getEdge("a", "b")).to.equal("a->b:label");
expect(g.getEdge("b", "c")).to.equal("b->c:undefined");
});

it("is chainable", function() {
var g2 = g.updatePath(["a", "b"], function() {});
expect(g).to.equal(g2);
});
});

describe("removeEdge", function() {
it("does nothing if the edge is not in the graph", function() {
g.setNode("n1");
Expand Down

0 comments on commit 7e7d7ed

Please sign in to comment.