Created
February 11, 2022 13:18
-
-
Save alexcraviotto/bb2ecc24f8dfb1915dd0d846fb4d2f44 to your computer and use it in GitHub Desktop.
A simple method to generate random strings with TypeScript
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
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