email-utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { alphaNumericAndMarksCharsStr, alphaNumericAndMarksRe } from '../regex-lib';
  2. import { isKnownTld } from './uri-utils';
  3. /**
  4. * A regular expression to match a 'mailto:' prefix on an email address.
  5. */
  6. export var mailtoSchemePrefixRe = /^mailto:/i;
  7. /**
  8. * Regular expression for all of the valid characters of the local part of an
  9. * email address.
  10. */
  11. var emailLocalPartCharRegex = new RegExp("[".concat(alphaNumericAndMarksCharsStr, "!#$%&'*+/=?^_`{|}~-]"));
  12. /**
  13. * Determines if the given character may start the "local part" of an email
  14. * address. The local part is the part to the left of the '@' sign.
  15. *
  16. * Technically according to the email spec, any of the characters in the
  17. * {@link emailLocalPartCharRegex} can start an email address (including any of
  18. * the special characters), but this is so rare in the wild and the
  19. * implementation is much simpler by only starting an email address with a word
  20. * character. This is especially important when matching the '{' character which
  21. * generally starts a brace that isn't part of the email address.
  22. */
  23. export function isEmailLocalPartStartChar(char) {
  24. return alphaNumericAndMarksRe.test(char);
  25. }
  26. /**
  27. * Determines if the given character can be part of the "local part" of an email
  28. * address. The local part is the part to the left of the '@' sign.
  29. */
  30. export function isEmailLocalPartChar(char) {
  31. return emailLocalPartCharRegex.test(char);
  32. }
  33. /**
  34. * Determines if the given email address is valid. We consider it valid if it
  35. * has a valid TLD in its host.
  36. *
  37. * @param emailAddress email address
  38. * @return true is email have valid TLD, false otherwise
  39. */
  40. export function isValidEmail(emailAddress) {
  41. var emailAddressTld = emailAddress.split('.').pop() || '';
  42. return isKnownTld(emailAddressTld);
  43. }
  44. //# sourceMappingURL=email-utils.js.map