Add UID utility

This commit is contained in:
Tim Mickel 2016-08-31 11:21:42 -04:00
parent be06078df1
commit 2da121d019

29
src/util/uid.js Normal file
View file

@ -0,0 +1,29 @@
/**
* @fileoverview UID generator, from Blockly.
*/
/**
* Legal characters for the unique ID.
* Should be all on a US keyboard. No XML special characters or control codes.
* Removed $ due to issue 251.
* @private
*/
var soup_ = '!#%()*+,-./:;=?@[]^_`{|}~' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
/**
* Generate a unique ID, from Blockly. This should be globally unique.
* 87 characters ^ 20 length > 128 bits (better than a UUID).
* @return {string} A globally unique ID string.
*/
var uid = function () {
var length = 20;
var soupLength = soup_.length;
var id = [];
for (var i = 0; i < length; i++) {
id[i] = soup_.charAt(Math.random() * soupLength);
}
return id.join('');
};
module.exports = uid;