Skip to content

Commit 505be2a

Browse files
committed
Treat parent ids as strings
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.
1 parent f29808d commit 505be2a

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/graph.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ Graph.prototype.setParent = function(v, parent) {
198198
if (_.isUndefined(parent)) {
199199
parent = GRAPH_NODE;
200200
} else {
201+
// Coerce parent to string
202+
parent += "";
201203
for (var ancestor = parent;
202204
!_.isUndefined(ancestor);
203205
ancestor = this.parent(ancestor)) {

test/graph-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ describe("Graph", function() {
303303
expect(_.sortBy(g.children())).to.eql(["a", "parent"]);
304304
});
305305

306+
it("uses the stringified form of the id", function() {
307+
g.setParent(2, 1);
308+
g.setParent(3, 2);
309+
expect(g.parent(2)).equals("1");
310+
expect(g.parent("2")).equals("1");
311+
expect(g.parent(3)).equals("2");
312+
});
313+
306314
it("preserves the tree invariant", function() {
307315
g.setParent("c", "b");
308316
g.setParent("b", "a");

0 commit comments

Comments
 (0)