Skip to content

Instantly share code, notes, and snippets.

@amadeuszblanik
Created March 3, 2020 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amadeuszblanik/f6b7f06a46e81d84e60285cb2df14c89 to your computer and use it in GitHub Desktop.
Save amadeuszblanik/f6b7f06a46e81d84e60285cb2df14c89 to your computer and use it in GitHub Desktop.
[TypeScript] Generate random ID
export const idGenerator = (length: number) => {
const allowedCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let response: string = ""
for (let i = 0; i < length; i++) {
const {length: charLength} = allowedCharacters
response += allowedCharacters.charAt(Math.floor(Math.random() * charLength))
}
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment