normalizeEmail.js 6.3 KB

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