UNPKG

789 BJavaScriptView Raw
1/**
2 * @fileoverview UID generator, from Blockly.
3 */
4
5/**
6 * Legal characters for the unique ID.
7 * Should be all on a US keyboard. No XML special characters or control codes.
8 * Removed $ due to issue 251.
9 * @private
10 */
11const soup_ = '!#%()*+,-./:;=?@[]^_`{|}~' +
12 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
13
14/**
15 * Generate a unique ID, from Blockly. This should be globally unique.
16 * 87 characters ^ 20 length > 128 bits (better than a UUID).
17 * @return {string} A globally unique ID string.
18 */
19const uid = function () {
20 const length = 20;
21 const soupLength = soup_.length;
22 const id = [];
23 for (let i = 0; i < length; i++) {
24 id[i] = soup_.charAt(Math.random() * soupLength);
25 }
26 return id.join('');
27};
28
29module.exports = uid;