Spaces:
Sleeping
Sleeping
| const HttpError = require('./httpError'); | |
| const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| function normalizeEmail(email) { | |
| return String(email || '').trim().toLowerCase(); | |
| } | |
| function validateEmail(email) { | |
| const normalizedEmail = normalizeEmail(email); | |
| if (!EMAIL_REGEX.test(normalizedEmail)) { | |
| throw new HttpError( | |
| 400, | |
| 'A valid email address is required.', | |
| 'INVALID_EMAIL', | |
| ); | |
| } | |
| return normalizedEmail; | |
| } | |
| function maskEmail(email) { | |
| const normalizedEmail = normalizeEmail(email); | |
| const [localPart, domainPart] = normalizedEmail.split('@'); | |
| if (!localPart || !domainPart) { | |
| return normalizedEmail; | |
| } | |
| if (localPart.length <= 2) { | |
| return `${localPart[0] || '*'}*@${domainPart}`; | |
| } | |
| return `${localPart[0]}${'*'.repeat(Math.max(localPart.length - 2, 1))}${localPart.slice(-1)}@${domainPart}`; | |
| } | |
| module.exports = { | |
| maskEmail, | |
| normalizeEmail, | |
| validateEmail, | |
| }; | |