mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-03 09:40:28 -04:00
Add UID utility
This commit is contained in:
parent
be06078df1
commit
2da121d019
1 changed files with 29 additions and 0 deletions
29
src/util/uid.js
Normal file
29
src/util/uid.js
Normal 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;
|
Loading…
Add table
Add a link
Reference in a new issue