Skip to content

Instantly share code, notes, and snippets.

@raishid
Last active August 27, 2024 03:04
Show Gist options
  • Save raishid/49f3ca259bd1bb533dbf7d01a99af564 to your computer and use it in GitHub Desktop.
Save raishid/49f3ca259bd1bb533dbf7d01a99af564 to your computer and use it in GitHub Desktop.
const PasswordGenerator = (
formGeneratePassword: TypeGeneratePassword
) => {
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+~`|}{[]:;?><,./-=";
const letters = "abcdefghijklmnopqrstuvwxyz";
const uppercaseLetters = letters.toUpperCase();
const lowercaseLetters = letters.toLowerCase();
let characters = "";
let password = "";
if (formGeneratePassword.numbers) {
characters += numbers;
}
if (formGeneratePassword.symbols) {
characters += symbols;
}
if (formGeneratePassword.upperCase) {
characters += uppercaseLetters;
}
if (formGeneratePassword.lowerCase) {
characters += lowercaseLetters;
}
for (let i = 0; i < formGeneratePassword.length; i++) {
password += characters.charAt(
Math.floor(Math.random() * characters.length)
);
}
return password;
};
interface TypeGeneratePassword {
numbers: boolean;
symbols: boolean;
length: number;
lowerCase: boolean;
upperCase: boolean;
password?: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment