From 2da121d0194f237bf05059d7b4a7209fa79fcccb Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Wed, 31 Aug 2016 11:21:42 -0400 Subject: [PATCH] Add UID utility --- src/util/uid.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/util/uid.js diff --git a/src/util/uid.js b/src/util/uid.js new file mode 100644 index 00000000..532f9e9b --- /dev/null +++ b/src/util/uid.js @@ -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;