Last active
September 9, 2015 22:29
-
-
Save xdmorgan/832e0ff64d6d751178b7 to your computer and use it in GitHub Desktop.
Generates a random string, in this case an 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
/* Optional customization ========================== | |
generateID(); | |
generateID({ | |
base: 'video_', | |
length: 12, | |
charset: '1234asdf', | |
overkill: true | |
}); | |
================================================= */ | |
function generateID(opts){ | |
var length, charset, base, time, rand, output = ''; | |
if(typeof opts === 'undefined'){ opts = {}; } | |
length = opts.length || 8; | |
charset = opts.charset || 'abcdefghijklmnopqrstuvwxyz012345689'; | |
base = opts.base || ''; | |
time = ( opts.overkill === true ) ? true : false; | |
charset = charset.split(''); | |
if( base !== '' ){ output += base; } | |
for (var i = length; i >= 0; i--) { | |
output += charset[ Math.floor( Math.random() * charset.length ) ]; | |
} | |
if( time === true ){ output += String(new Date().getTime()); } | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment