Skip to content

Instantly share code, notes, and snippets.

@mausworks
Last active October 18, 2019 09:14
Show Gist options
  • Save mausworks/e849ef8ab13212b254324ffed64628d8 to your computer and use it in GitHub Desktop.
Save mausworks/e849ef8ab13212b254324ffed64628d8 to your computer and use it in GitHub Desktop.
Generate an easy to interpret ID.
/**
* 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