Last active
October 18, 2019 09:14
-
-
Save mausworks/e849ef8ab13212b254324ffed64628d8 to your computer and use it in GitHub Desktop.
Generate an easy to interpret 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
/** | |
* The characters which will be used in the ID. | |
* Note that similar characters such as 0 and O are removed. | |
*/ | |
const CHARACTERS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; | |
const ID_LENGTH = 5; | |
const randomCharIndex = () => Math.floor(Math.random() * CHARACTERS.length); | |
const getRandomChar = () => CHARACTERS[randomCharIndex()]; | |
const generateId = () => { | |
let result = ""; | |
for (let i = 0; i < ID_LENGTH; i++) { | |
result += getRandomChar(); | |
} | |
return result; | |
}; | |
console.log(generateId()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment