Created
November 14, 2016 10:49
-
-
Save AnasAboreeda/29d84253aa85f1345d59eb21e9ae856a to your computer and use it in GitHub Desktop.
create a unique identifier with the given `len`
This file contains hidden or 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
/** | |
* 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