normalizeEmail.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import merge from './util/merge';
  2. var default_normalize_email_options = {
  3. // The following options apply to all email addresses
  4. // Lowercases the local part of the email address.
  5. // Please note this may violate RFC 5321 as per http://stackoverflow.com/a/9808332/192024).
  6. // The domain is always lowercased, as per RFC 1035
  7. all_lowercase: true,
  8. // The following conversions are specific to GMail
  9. // Lowercases the local part of the GMail address (known to be case-insensitive)
  10. gmail_lowercase: true,
  11. // Removes dots from the local part of the email address, as that's ignored by GMail
  12. gmail_remove_dots: true,
  13. // Removes the subaddress (e.g. "+foo") from the email address
  14. gmail_remove_subaddress: true,
  15. // Conversts the googlemail.com domain to gmail.com
  16. gmail_convert_googlemaildotcom: true,
  17. // The following conversions are specific to Outlook.com / Windows Live / Hotmail
  18. // Lowercases the local part of the Outlook.com address (known to be case-insensitive)
  19. outlookdotcom_lowercase: true,
  20. // Removes the subaddress (e.g. "+foo") from the email address
  21. outlookdotcom_remove_subaddress: true,
  22. // The following conversions are specific to Yahoo
  23. // Lowercases the local part of the Yahoo address (known to be case-insensitive)
  24. yahoo_lowercase: true,
  25. // Removes the subaddress (e.g. "-foo") from the email address
  26. yahoo_remove_subaddress: true,
  27. // The following conversions are specific to Yandex
  28. // Lowercases the local part of the Yandex address (known to be case-insensitive)
  29. yandex_lowercase: true,
  30. // The following conversions are specific to iCloud
  31. // Lowercases the local part of the iCloud address (known to be case-insensitive)
  32. icloud_lowercase: true,
  33. // Removes the subaddress (e.g. "+foo") from the email address
  34. icloud_remove_subaddress: true
  35. }; // List of domains used by iCloud
  36. var icloud_domains = ['icloud.com', 'me.com']; // List of domains used by Outlook.com and its predecessors
  37. // This list is likely incomplete.
  38. // Partial reference:
  39. // https://blogs.office.com/2013/04/17/outlook-com-gets-two-step-verification-sign-in-by-alias-and-new-international-domains/
  40. var outlookdotcom_domains = ['hotmail.at', 'hotmail.be', 'hotmail.ca', 'hotmail.cl', 'hotmail.co.il', 'hotmail.co.nz', 'hotmail.co.th', 'hotmail.co.uk', 'hotmail.com', 'hotmail.com.ar', 'hotmail.com.au', 'hotmail.com.br', 'hotmail.com.gr', 'hotmail.com.mx', 'hotmail.com.pe', 'hotmail.com.tr', 'hotmail.com.vn', 'hotmail.cz', 'hotmail.de', 'hotmail.dk', 'hotmail.es', 'hotmail.fr', 'hotmail.hu', 'hotmail.id', 'hotmail.ie', 'hotmail.in', 'hotmail.it', 'hotmail.jp', 'hotmail.kr', 'hotmail.lv', 'hotmail.my', 'hotmail.ph', 'hotmail.pt', 'hotmail.sa', 'hotmail.sg', 'hotmail.sk', 'live.be', 'live.co.uk', 'live.com', 'live.com.ar', 'live.com.mx', 'live.de', 'live.es', 'live.eu', 'live.fr', 'live.it', 'live.nl', 'msn.com', 'outlook.at', 'outlook.be', 'outlook.cl', 'outlook.co.il', 'outlook.co.nz', 'outlook.co.th', 'outlook.com', 'outlook.com.ar', 'outlook.com.au', 'outlook.com.br', 'outlook.com.gr', 'outlook.com.pe', 'outlook.com.tr', 'outlook.com.vn', 'outlook.cz', 'outlook.de', 'outlook.dk', 'outlook.es', 'outlook.fr', 'outlook.hu', 'outlook.id', 'outlook.ie', 'outlook.in', 'outlook.it', 'outlook.jp', 'outlook.kr', 'outlook.lv', 'outlook.my', 'outlook.ph', 'outlook.pt', 'outlook.sa', 'outlook.sg', 'outlook.sk', 'passport.com']; // List of domains used by Yahoo Mail
  41. // This list is likely incomplete
  42. var yahoo_domains = ['rocketmail.com', 'yahoo.ca', 'yahoo.co.uk', 'yahoo.com', 'yahoo.de', 'yahoo.fr', 'yahoo.in', 'yahoo.it', 'ymail.com']; // List of domains used by yandex.ru
  43. var yandex_domains = ['yandex.ru', 'yandex.ua', 'yandex.kz', 'yandex.com', 'yandex.by', 'ya.ru']; // replace single dots, but not multiple consecutive dots
  44. function dotsReplacer(match) {
  45. if (match.length > 1) {
  46. return match;
  47. }
  48. return '';
  49. }
  50. export default function normalizeEmail(email, options) {
  51. options = merge(options, default_normalize_email_options);
  52. var raw_parts = email.split('@');
  53. var domain = raw_parts.pop();
  54. var user = raw_parts.join('@');
  55. var parts = [user, domain]; // The domain is always lowercased, as it's case-insensitive per RFC 1035
  56. parts[1] = parts[1].toLowerCase();
  57. if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
  58. // Address is GMail
  59. if (options.gmail_remove_subaddress) {
  60. parts[0] = parts[0].split('+')[0];
  61. }
  62. if (options.gmail_remove_dots) {
  63. // this does not replace consecutive dots like example..email@gmail.com
  64. parts[0] = parts[0].replace(/\.+/g, dotsReplacer);
  65. }
  66. if (!parts[0].length) {
  67. return false;
  68. }
  69. if (options.all_lowercase || options.gmail_lowercase) {
  70. parts[0] = parts[0].toLowerCase();
  71. }
  72. parts[1] = options.gmail_convert_googlemaildotcom ? 'gmail.com' : parts[1];
  73. } else if (icloud_domains.indexOf(parts[1]) >= 0) {
  74. // Address is iCloud
  75. if (options.icloud_remove_subaddress) {
  76. parts[0] = parts[0].split('+')[0];
  77. }
  78. if (!parts[0].length) {
  79. return false;
  80. }
  81. if (options.all_lowercase || options.icloud_lowercase) {
  82. parts[0] = parts[0].toLowerCase();
  83. }
  84. } else if (outlookdotcom_domains.indexOf(parts[1]) >= 0) {
  85. // Address is Outlook.com
  86. if (options.outlookdotcom_remove_subaddress) {
  87. parts[0] = parts[0].split('+')[0];
  88. }
  89. if (!parts[0].length) {
  90. return false;
  91. }
  92. if (options.all_lowercase || options.outlookdotcom_lowercase) {
  93. parts[0] = parts[0].toLowerCase();
  94. }
  95. } else if (yahoo_domains.indexOf(parts[1]) >= 0) {
  96. // Address is Yahoo
  97. if (options.yahoo_remove_subaddress) {
  98. var components = parts[0].split('-');
  99. parts[0] = components.length > 1 ? components.slice(0, -1).join('-') : components[0];
  100. }
  101. if (!parts[0].length) {
  102. return false;
  103. }
  104. if (options.all_lowercase || options.yahoo_lowercase) {
  105. parts[0] = parts[0].toLowerCase();
  106. }
  107. } else if (yandex_domains.indexOf(parts[1]) >= 0) {
  108. if (options.all_lowercase || options.yandex_lowercase) {
  109. parts[0] = parts[0].toLowerCase();
  110. }
  111. parts[1] = 'yandex.ru'; // all yandex domains are equal, 1st preferred
  112. } else if (options.all_lowercase) {
  113. // Any other address
  114. parts[0] = parts[0].toLowerCase();
  115. }
  116. return parts.join('@');
  117. }