unique-login-credential
Version:
A package for generating unique passwords and usernames.
162 lines (161 loc) • 4.76 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var index_exports = {};
__export(index_exports, {
uniquePassword: () => uniquePassword,
uniqueUsername: () => uniqueUsername
});
module.exports = __toCommonJS(index_exports);
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;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
uniquePassword,
uniqueUsername
});
//# sourceMappingURL=index.cjs.map