move email regexp to module scope#4341
Open
jenul-ferdinand wants to merge 1 commit intoSSWConsulting:mainfrom
Open
move email regexp to module scope#4341jenul-ferdinand wants to merge 1 commit intoSSWConsulting:mainfrom
jenul-ferdinand wants to merge 1 commit intoSSWConsulting:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR moves the email validation regex from function scope to module scope in validator.ts to avoid re-compiling the regex on every function call, providing a minor performance optimization.
Key Changes
- Email regex pattern extracted to module-level constant
EMAIL_REGEX - Associated comments and eslint directives moved alongside the constant
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| /* eslint-disable */ | ||
| const regex = | ||
| /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
| /* eslint-enable */ |
There was a problem hiding this comment.
The /* eslint-enable */ comment is in the wrong location. Since /* eslint-disable */ is now at line 4 (module scope), the corresponding /* eslint-enable */ should be at line 5 (after the EMAIL_REGEX constant definition), not inside the isEmail function.
Suggested fix:
// Found at https://emailregex.com/
/* eslint-disable */
const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
/* eslint-enable */
export const isEmail = (email: string): boolean => {
return EMAIL_REGEX.test(email);
};
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The email validation regex (in
validator.ts) had been defined inside theisEmail()function, so it's re-compiled every time the function is called. Moving it to the module scope ensures that it is compiled only once.Not too much of a big deal, minimal performance gain.