Last active
March 14, 2017 12:58
-
-
Save SeanJM/c44a8f3afb54104f5e082827cda9940c to your computer and use it in GitHub Desktop.
A function which generates a random 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
(function () { | |
var lib = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
var n = lib.length - 1; | |
function generateId(index) { | |
var id = []; | |
while (index-- > 0) { | |
id.push(lib[Math.round(Math.random() * n)]); | |
} | |
return id.join(''); | |
} | |
if (typeof module === 'object') { | |
module.exports = generateId; | |
} else if (typeof window === 'object') { | |
window.generateId = generateId; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment