Skip to content

Commit ccbb66e

Browse files
authored
Merge pull request #478 from metaturso/477/fix-unique-id-toString
Fixed #477: uniqueId producing [object undefined].
2 parents 23295c7 + 3c6952e commit ccbb66e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ module.exports = {
3131
* Adds a dummy node to the graph and return v.
3232
*/
3333
function addDummyNode(g, type, attrs, name) {
34-
let v;
35-
do {
34+
var v = name;
35+
while (g.hasNode(v)) {
3636
v = uniqueId(name);
37-
} while (g.hasNode(v));
37+
}
3838

3939
attrs.dummy = type;
4040
g.setNode(v, attrs);
@@ -278,7 +278,7 @@ function notime(name, fn) {
278278
let idCounter = 0;
279279
function uniqueId(prefix) {
280280
var id = ++idCounter;
281-
return toString(prefix) + id;
281+
return prefix + ("" + id);
282282
}
283283

284284
function range(start, limit, step = 1) {

test/unique-id-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var expect = require("./chai").expect;
2+
var util = require("../lib/util.js");
3+
4+
describe("Given a function to generate unique identifiers", function () {
5+
it("uniqueId(name) generates a valid identifier", function () {
6+
// This test guards against a bug #477, where the call to toString(prefix) inside
7+
// uniqueId() produced [object undefined].
8+
var id = util.uniqueId("_root");
9+
expect(id).not.to.include('[object undefined]');
10+
expect(id).match(/_root\d+/);
11+
});
12+
13+
it("Calling uniqueId(name) multiple times generate distinct values", function () {
14+
var first = util.uniqueId("name");
15+
var second = util.uniqueId("name");
16+
var third = util.uniqueId("name");
17+
expect(first).not.equals(second);
18+
expect(second).not.equals(third);
19+
});
20+
21+
it("Calling uniqueId(number) with a number creates a valid identifier string", function() {
22+
var id = util.uniqueId(99);
23+
expect(id).to.be.a('string');
24+
expect(id).not.to.be.a('number');
25+
26+
expect(id).to.match(/99\d+/);
27+
});
28+
});

0 commit comments

Comments
 (0)