Skip to content

Commit

Permalink
Treat parent ids as strings
Browse files Browse the repository at this point in the history
Prior to this change graph.parent(x) could return a non-string id, which
is inconsistent with how node ids are treated in all other parts of the
API. This change ensures that parent ids are coerced to strings.
  • Loading branch information
cpettitt committed May 15, 2015
1 parent f29808d commit 505be2a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ Graph.prototype.setParent = function(v, parent) {
if (_.isUndefined(parent)) {
parent = GRAPH_NODE;
} else {
// Coerce parent to string
parent += "";
for (var ancestor = parent;
!_.isUndefined(ancestor);
ancestor = this.parent(ancestor)) {
Expand Down
8 changes: 8 additions & 0 deletions test/graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ describe("Graph", function() {
expect(_.sortBy(g.children())).to.eql(["a", "parent"]);
});

it("uses the stringified form of the id", function() {
g.setParent(2, 1);
g.setParent(3, 2);
expect(g.parent(2)).equals("1");
expect(g.parent("2")).equals("1");
expect(g.parent(3)).equals("2");
});

it("preserves the tree invariant", function() {
g.setParent("c", "b");
g.setParent("b", "a");
Expand Down

0 comments on commit 505be2a

Please sign in to comment.