Skip to content

Commit

Permalink
Allow setPath to take a function for its label argument
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent 53c93a0 commit fde8b40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/base-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ BaseGraph.prototype.setPath = function(vs, label) {
args = arguments;
_.reduce(vs, function(v, w) {
if (args.length > 1) {
self.setEdge(v, w, label);
if (_.isFunction(label)) {
self.setEdge(v, w, label({ v: v, w: w }));
} else {
self.setEdge(v, w, label);
}
} else {
self.setEdge(v, w);
}
Expand Down
6 changes: 6 additions & 0 deletions test/base-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ 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 Down

0 comments on commit fde8b40

Please sign in to comment.