Skip to content

Commit

Permalink
Allow setNodes to take a function for the label argument
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent cc4f231 commit 53c93a0
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -148,7 +148,11 @@ BaseGraph.prototype.setNodes = function(vs, label) {
var args = arguments;
_.each(vs, function(v) {
if (args.length > 1) {
this.setNode(v, label);
if (_.isFunction(label)) {
this.setNode(v, label(v));
} else {
this.setNode(v, label);
}
} else {
this.setNode(v);
}
Expand Down
9 changes: 9 additions & 0 deletions test/base-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ 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 Down

0 comments on commit 53c93a0

Please sign in to comment.