Last active
May 29, 2017 10:46
-
-
Save Chalarangelo/9f759cd75cd93b6ad2644c645cd46aa8 to your computer and use it in GitHub Desktop.
Create unique id with javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on this answer: https://stackoverflow.com/a/10734934/1650200 | |
// Compressed version is 139 bytes - Generated using http://refresh-sf.com/ | |
function g(){ | |
var s = String.fromCharCode, // Shorthand for String.fromCharCode() | |
r = Math.random, // Shorthand for Math.random() | |
i = s(~~((r()*25)+65)), // Generate a letter to start the id (for DOM friendliness) | |
a = b = 0; // Declare variable 'a' and set counter to 0 | |
for(;b<32;a = ~~((r()*42)+48)) // While less the id's length is less than 32, generate a character | |
a<58||a>64 ? // If the generated character is not a special symbol, | |
(i+=s(a),b++) // Add to the end of id, increment length counter by 1 | |
: a; // Otherwise, do nothing | |
return i; // Return the id | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function g(){for(var r=String.fromCharCode,n=Math.random,o=r(~~(25*n()+65)),a=b=0;b<32;a=~~(42*n()+48))58>a||a>64?(o+=r(a),b++):a;return o} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment