Skip to content

Commit

Permalink
Simpler ID generator. Fixes #201.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Mar 1, 2016
1 parent 12de557 commit bfd24a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
25 changes: 6 additions & 19 deletions src/math/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,12 @@ dwv.math.getStats = function (array)

/**
* Unique ID generator.
* @class IdGenerator
* @namespace dwv.math
* @constructor
* See http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
* and this answer http://stackoverflow.com/a/13403498.
* @method guid
* @static
*/
dwv.math.IdGenerator = function ()
dwv.math.guid = function ()
{
/**
* Root for IDs.
* @property root
* @private
* @type Number
*/
var root = Math.floor( Math.random() * 26 ) + Date.now();
/**
* Get a unique id.
* @method get
* @return {Number} The unique Id.
*/
this.get = function () {
return root++;
};
return Math.random().toString(36).substring(2, 15);
};
10 changes: 1 addition & 9 deletions src/tools/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,6 @@ dwv.tool.Draw = function (app, shapeFactoryList)
*/
var drawLayer = null;

/**
* The associated draw layer.
* @property drawLayer
* @private
* @type Object
*/
var idGenerator = new dwv.math.IdGenerator();

/**
* Handle mouse down event.
* @method mousedown
Expand Down Expand Up @@ -479,7 +471,7 @@ dwv.tool.Draw = function (app, shapeFactoryList)
// create final shape
var factory = new self.shapeFactoryList[self.shapeName]();
var group = factory.create(points, app.getStyle(), app.getImage());
group.id( idGenerator.get() );
group.id( dwv.math.guid() );
// re-activate layer
drawLayer.hitGraphEnabled(true);
// draw shape command
Expand Down
28 changes: 18 additions & 10 deletions tests/math/stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ QUnit.test("Test getStats.", function (assert) {
assert.equal(q1.stdDev, 2.9832867780352594, "stdDev.1");
});

QUnit.test("Test IdGenerator.", function (assert) {
var generator = new dwv.math.IdGenerator();
var id0 = generator.get();
var id1 = generator.get();
assert.equal((id0 == id1), false, "Ids should not be equal for the same generator.");

var generator2 = new dwv.math.IdGenerator();
var id2 = generator2.get();
assert.equal((id0 == id2), false, "Ids should not be equal for different generators (1).");
assert.equal((id1 == id2), false, "Ids should not be equal for different generators (2).");
QUnit.test("Test GUID.", function (assert) {
var id0 = dwv.math.guid();
var id1 = dwv.math.guid();
assert.equal((id0 == id1), false, "Two GUids should not be equal.");

var duplicate = false;
// create an array of guids
var ids = [];
for (var i = 0; i < 1000; ++i) {
ids[ids.length] = dwv.math.guid();
}
// check duplicates
for (i = 0; i < ids.length; ++i) {
if (ids.indexOf(ids.pop()) !== -1) {
duplicate = true;
break;
}
}
assert.ok(!duplicate, "1000 GUids should not be equal.");
});

0 comments on commit bfd24a2

Please sign in to comment.