-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (99 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Define regex components with descriptive names
const LOCAL_PART_START = "[a-zA-Z0-9]";
const LOCAL_PART_MIDDLE = "(?!.*\\.\\.)[a-zA-Z0-9._+-]*"; // Prevent consecutive dots
const LOCAL_PART_PLUS = "(?:\\+[a-zA-Z0-9._+-]+)?"; // Plus sign handling after the first character
const LOCAL_PART_END = "[a-zA-Z0-9]";
const DOMAIN_START = "[a-zA-Z0-9]";
const DOMAIN_MIDDLE = "(?!.*\\.\\.)[a-zA-Z0-9.-]*"; // Prevent consecutive dots in domain part
const DOMAIN_END = "[a-zA-Z0-9]";
const TLD = "[a-zA-Z]{2,}";
// Combine components into full email regex
const emailRegex = new RegExp(
`^${LOCAL_PART_START}${LOCAL_PART_MIDDLE}${LOCAL_PART_END}${LOCAL_PART_PLUS}@${DOMAIN_START}${DOMAIN_MIDDLE}${DOMAIN_END}\\.${TLD}$`
);
/**
* Validates if the provided email has a correct format.
*
* Valid email formats:
* - Basic: [email protected]
* - With dots: [email protected]
* - With plus: [email protected]
* - With plus and dots: [email protected]
* - With special chars: [email protected]
*
* Invalid email formats:
* - Consecutive dots: [email protected]
* - Dot at start/end: [email protected] or [email protected]
* - Invalid chars: user*[email protected]
* - Missing parts: [email protected] or @example.com
*
* Validation rules:
* - Local part: Allows alphanumeric, dots, underscores, hyphens, plus signs
* - Dots cannot be first/last or consecutive
* - Plus sign allowed after first character
* - Domain: Allows alphanumeric, dots, hyphens
* - TLD: At least 2 characters, alphabetic only
*
* @param {string} email - The email address to validate.
* @returns {boolean} - Returns true if email is valid, else false.
*/
function isValidEmail(email) {
console.log("Validating email: ", email);
// Handle null/undefined/empty inputs
if (email === null || email === undefined) {
return false;
}
// Validate input type
if (typeof email !== "string") {
return false;
}
// Handle empty string
if (email.trim() === "") {
return false;
}
// Sanitize input
email = email.trim().toLowerCase();
// Updated regex pattern with plus sign support
return emailRegex.test(email);
}
/**
* Generates a random password of specified length.
* @param {number} [length=8] - The length of the password to generate.
* @param {Object} [options] - Password generation options
* @param {boolean} [options.numbers=true] - Include numbers in password
* @param {boolean} [options.special=true] - Include special characters in password
* @param {boolean} [options.alphabets=true] - Include alphabets in password
* @returns {string} - The generated password.
*/
// Note: For production use, consider using crypto.randomBytes() instead of Math.random()
// for cryptographically secure password generation
function randomPasswordGenerator(
length = 8,
options = { numbers: true, special: true, alphabets: true }
) {
const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerCase = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const special = "!@#$%^&*()_+-=[]{}|;:,.<>?";
let allChars = "";
if (options.alphabets) {
allChars += upperCase + lowerCase;
}
if (options.numbers) {
allChars += numbers;
}
if (options.special) {
allChars += special;
}
// Ensure at least one character set is included
if (allChars === "") {
allChars = upperCase + lowerCase; // Default to alphabets if nothing selected
}
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * allChars.length);
password += allChars[randomIndex];
}
return password;
}
export { isValidEmail, randomPasswordGenerator };