Skip to content

Instantly share code, notes, and snippets.

@alexcraviotto
Created February 11, 2022 13:18
Show Gist options
  • Save alexcraviotto/bb2ecc24f8dfb1915dd0d846fb4d2f44 to your computer and use it in GitHub Desktop.
Save alexcraviotto/bb2ecc24f8dfb1915dd0d846fb4d2f44 to your computer and use it in GitHub Desktop.
A simple method to generate random strings with TypeScript
export const generateRandomString = (length: number):string => {
const allCaps: string[] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P", "Q","R","S","T","U","V","W","X", "Y","Z"]
const allLower: string[] = allCaps.map(letter => letter.toLowerCase())
const alphabet: string[] = [...allCaps, ...allLower]
let output: string= ""
for(let i = 0; i < length; i++){
output += (alphabet[Math.floor(Math.random() * (alphabet.length + 1))])
}
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment