Skip to content

Commit

Permalink
refactor(utils): centralize email regex for reuse
Browse files Browse the repository at this point in the history
- Added a detailed `emailRegex` with extended documentation in `text-transform.ts` to centralize email validation logic.
- Updated `detectEmail` and `removeEmail` functions to reuse the new `emailRegex` constant, improving maintainability and consistency.
  • Loading branch information
cswni committed Feb 4, 2025
1 parent 092ac35 commit 77406f2
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/utils/text-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ const specialChars = [
'{}',
];


/**
* A regular expression pattern used for matching and validating email addresses.
*
* This pattern supports common email formats, where:
* - The local part can contain alphanumeric characters, dots, underscores,
* percent signs, plus signs, and hyphens.
* - The domain part is structured as one or more labels separated by dots,
* with each label containing alphanumeric characters or hyphens.
* - The top-level domain must be at least two characters long and consist
* only of alphabetic characters.
*
* Flags:
* - The "g" flag enables global matching, allowing multiple occurrences
* of email addresses to be matched in a single string.
*/
const emailRegex = /[a-zA-Z0-9][a-zA-Z0-9._%+-]*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}/g;
/**
* Removes special characters from the given text string and returns the cleaned string.
*
Expand All @@ -62,7 +79,6 @@ function removeSpecialChars(text: string): string {
* @return {boolean} Returns `true` if the text contains a valid email address, otherwise `false`.
*/
function detectEmail(text: string): boolean {
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
return emailRegex.test(text);
}

Expand All @@ -73,8 +89,7 @@ function detectEmail(text: string): boolean {
* @return {string} The modified string with the email address removed and trimmed of extra spaces.
*/
function removeEmail(text: string): string {
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
return text.replace(emailRegex, '').trim(); // Remove the email and trim any extra spaces
return text.replace(emailRegex, '').trim();
}

/**
Expand Down

0 comments on commit 77406f2

Please sign in to comment.