normalizeEmail.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. };
  36. // List of domains used by iCloud
  37. var icloud_domains = ['icloud.com', 'me.com'];
  38. // List of domains used by Outlook.com and its predecessors
  39. // This list is likely incomplete.
  40. // Partial reference:
  41. // https://blogs.office.com/2013/04/17/outlook-com-gets-two-step-verification-sign-in-by-alias-and-new-international-domains/
  42. 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'];
  43. // List of domains used by Yahoo Mail
  44. // This list is likely incomplete
  45. var yahoo_domains = ['rocketmail.com', 'yahoo.ca', 'yahoo.co.uk', 'yahoo.com', 'yahoo.de', 'yahoo.fr', 'yahoo.in', 'yahoo.it', 'ymail.com'];
  46. // List of domains used by yandex.ru
  47. var yandex_domains = ['yandex.ru', 'yandex.ua', 'yandex.kz', 'yandex.com', 'yandex.by', 'ya.ru'];
  48. // replace single dots, but not multiple consecutive dots
  49. function dotsReplacer(match) {
  50. if (match.length > 1) {
  51. return match;
  52. }
  53. return '';
  54. }
  55. export default function normalizeEmail(email, options) {
  56. options = merge(options, default_normalize_email_options);
  57. var raw_parts = email.split('@');
  58. var domain = raw_parts.pop();
  59. var user = raw_parts.join('@');
  60. var parts = [user, domain];
  61. // The domain is always lowercased, as it's case-insensitive per RFC 1035
  62. parts[1] = parts[1].toLowerCase();
  63. if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
  64. // Address is GMail
  65. if (options.gmail_remove_subaddress) {
  66. parts[0] = parts[0].split('+')[0];
  67. }
  68. if (options.gmail_remove_dots) {
  69. // this does not replace consecutive dots like example..email@gmail.com
  70. parts[0] = parts[0].replace(/\.+/g, dotsReplacer);
  71. }
  72. if (!parts[0].length) {
  73. return false;
  74. }
  75. if (options.all_lowercase || options.gmail_lowercase) {
  76. parts[0] = parts[0].toLowerCase();
  77. }
  78. parts[1] = options.gmail_convert_googlemaildotcom ? 'gmail.com' : parts[1];
  79. } else if (icloud_domains.indexOf(parts[1]) >= 0) {
  80. // Address is iCloud
  81. if (options.icloud_remove_subaddress) {
  82. parts[0] = parts[0].split('+')[0];
  83. }
  84. if (!parts[0].length) {
  85. return false;
  86. }
  87. if (options.all_lowercase || options.icloud_lowercase) {
  88. parts[0] = parts[0].toLowerCase();
  89. }
  90. } else if (outlookdotcom_domains.indexOf(parts[1]) >= 0) {
  91. // Address is Outlook.com
  92. if (options.outlookdotcom_remove_subaddress) {
  93. parts[0] = parts[0].split('+')[0];
  94. }
  95. if (!parts[0].length) {
  96. return false;
  97. }
  98. if (options.all_lowercase || options.outlookdotcom_lowercase) {
  99. parts[0] = parts[0].toLowerCase();
  100. }
  101. } else if (yahoo_domains.indexOf(parts[1]) >= 0) {
  102. // Address is Yahoo
  103. if (options.yahoo_remove_subaddress) {
  104. var components = parts[0].split('-');
  105. parts[0] = components.length > 1 ? components.slice(0, -1).join('-') : components[0];
  106. }
  107. if (!parts[0].length) {
  108. return false;
  109. }
  110. if (options.all_lowercase || options.yahoo_lowercase) {
  111. parts[0] = parts[0].toLowerCase();
  112. }
  113. } else if (yandex_domains.indexOf(parts[1]) >= 0) {
  114. if (options.all_lowercase || options.yandex_lowercase) {
  115. parts[0] = parts[0].toLowerCase();
  116. }
  117. parts[1] = 'yandex.ru'; // all yandex domains are equal, 1st preferred
  118. } else if (options.all_lowercase) {
  119. // Any other address
  120. parts[0] = parts[0].toLowerCase();
  121. }
  122. return parts.join('@');
  123. }