Skip to content

Instantly share code, notes, and snippets.

@AnasAboreeda
Created November 14, 2016 10:49
Show Gist options
  • Save AnasAboreeda/29d84253aa85f1345d59eb21e9ae856a to your computer and use it in GitHub Desktop.
Save AnasAboreeda/29d84253aa85f1345d59eb21e9ae856a to your computer and use it in GitHub Desktop.
create a unique identifier with the given `len`
/**
* Return a unique identifier with the given `len`.
*
* utils.uid(10);
* // => "FDaS435D2z"
*
* @param {Number} len
* @return {String}
* @api private
*/
/**
* Return a random int, used by `utils.uid()`
*
* @param {Number} min
* @param {Number} max
* @return {Number}
* @api private
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
exports.uid = function(len) {
var buf = [];
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charLen = chars.length;
for (var i = 0; i < len; ++i) {
buf.push(chars[getRandomInt(0, charLen - 1)]);
}
return buf.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment