Skip to content

Instantly share code, notes, and snippets.

@agoalofalife
Created January 16, 2017 13:13
Show Gist options
  • Save agoalofalife/94ea286c6aab9b8c5da7021f60a83323 to your computer and use it in GitHub Desktop.
Save agoalofalife/94ea286c6aab9b8c5da7021f60a83323 to your computer and use it in GitHub Desktop.
/**
* Generates unique id (mix datetime and random).
* @param {number=} len Required length of id (16 by default).
* @returns {string} Generated id.
*/
function genUID(len){
function base36(val){
return Math.round(val).toString(36);
}
// uid should starts with alpha
var result = base36(10 + 25 * Math.random());
if (!len)
len = 16;
while (result.length < len)
result += base36(new Date * Math.random());
return result.substr(0, len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment