Skip to content

Instantly share code, notes, and snippets.

@xdmorgan
Last active September 9, 2015 22:29
Show Gist options
  • Save xdmorgan/832e0ff64d6d751178b7 to your computer and use it in GitHub Desktop.
Save xdmorgan/832e0ff64d6d751178b7 to your computer and use it in GitHub Desktop.
Generates a random string, in this case an ID.
/* 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