unique-login-credential
Version:
A package for generating unique passwords and usernames.
136 lines • 3.7 kB
JavaScript
// index.ts
var smallAlphabets = "abcdefghijklmnopqrstuvwxyz".split("");
var capitalAlphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
var numbers = "1234567890".split("");
var specialCharacters = ["!", "@", "#", "$", "_", "*"];
var refrenceObj = {
capitalAlphabet: capitalAlphabets,
smallAlphabet: smallAlphabets,
number: numbers,
specialCharacter: specialCharacters
};
var getFilledArray = (element, count) => {
if (count < 1) {
throw new Error("Invalid element count: element count is less than 1");
}
return new Array(count).fill(element);
};
var defaultPasswordOptions = {
length: 8,
capitalLetter: 1,
smallLetter: 3,
number: 2,
specialCharacter: 2,
random: false
};
var defaultUsernameOptions = {
prefix: "",
length: 6,
capitalLetter: 1,
smallLetter: 3,
number: 2,
specialCharacter: 0,
random: false
};
var uniquePassword = (options = {}) => {
const finalOptions = { ...defaultPasswordOptions, ...options };
const {
length,
capitalLetter,
smallLetter,
number,
specialCharacter,
random
} = finalOptions;
const totalRequested = capitalLetter + smallLetter + number + specialCharacter;
if (totalRequested > length) {
throw new Error(
"Invalid password length: Total character count exceeds the password length."
);
}
let choice = [];
if (capitalLetter > 0) {
choice.push(...getFilledArray("capitalAlphabet", capitalLetter));
}
if (smallLetter > 0) {
choice.push(...getFilledArray("smallAlphabet", smallLetter));
}
if (number > 0) {
choice.push(...getFilledArray("number", number));
}
if (specialCharacter > 0) {
choice.push(...getFilledArray("specialCharacter", specialCharacter));
}
while (choice.length < length) {
const types = [
"capitalAlphabet",
"smallAlphabet",
"number",
"specialCharacter"
];
choice.push(types[Math.floor(Math.random() * types.length)]);
}
if (random) {
choice = choice.sort(() => 0.5 - Math.random());
}
let password = "";
choice.forEach((type) => {
const ch = refrenceObj[type][Math.floor(Math.random() * refrenceObj[type].length)];
if (ch !== void 0) password += ch;
});
return password;
};
var uniqueUsername = (options = {}) => {
const finalOptions = { ...defaultUsernameOptions, ...options };
const {
prefix,
length,
capitalLetter,
smallLetter,
number,
specialCharacter,
random
} = finalOptions;
const totalRequested = capitalLetter + smallLetter + number + specialCharacter + prefix.length;
if (totalRequested > length) {
throw new Error(
"Invalid username length: Total character count exceeds the username length."
);
}
let choice = [];
let username = prefix;
if (capitalLetter > 0) {
choice.push(...getFilledArray("capitalAlphabet", capitalLetter));
}
if (smallLetter > 0) {
choice.push(...getFilledArray("smallAlphabet", smallLetter));
}
if (number > 0) {
choice.push(...getFilledArray("number", number));
}
if (specialCharacter > 0) {
choice.push(...getFilledArray("specialCharacter", specialCharacter));
}
while (choice.length < length - prefix.length) {
const types = [
"capitalAlphabet",
"smallAlphabet",
"number",
"specialCharacter"
];
choice.push(types[Math.floor(Math.random() * types.length)]);
}
if (random) {
choice = choice.sort(() => 0.5 - Math.random());
}
choice.forEach((type) => {
const ch = refrenceObj[type][Math.floor(Math.random() * refrenceObj[type].length)];
if (ch !== void 0) username += ch;
});
return username;
};
export {
uniquePassword,
uniqueUsername
};
//# sourceMappingURL=index.js.map