normalizeEmail.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = normalizeEmail;
  6. var _merge = _interopRequireDefault(require("./util/merge"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var default_normalize_email_options = {
  9. // The following options apply to all email addresses
  10. // Lowercases the local part of the email address.
  11. // Please note this may violate RFC 5321 as per http://stackoverflow.com/a/9808332/192024).
  12. // The domain is always lowercased, as per RFC 1035
  13. all_lowercase: true,
  14. // The following conversions are specific to GMail
  15. // Lowercases the local part of the GMail address (known to be case-insensitive)
  16. gmail_lowercase: true,
  17. // Removes dots from the local part of the email address, as that's ignored by GMail
  18. gmail_remove_dots: true,
  19. // Removes the subaddress (e.g. "+foo") from the email address
  20. gmail_remove_subaddress: true,
  21. // Conversts the googlemail.com domain to gmail.com
  22. gmail_convert_googlemaildotcom: true,
  23. // The following conversions are specific to Outlook.com / Windows Live / Hotmail
  24. // Lowercases the local part of the Outlook.com address (known to be case-insensitive)
  25. outlookdotcom_lowercase: true,
  26. // Removes the subaddress (e.g. "+foo") from the email address
  27. outlookdotcom_remove_subaddress: true,
  28. // The following conversions are specific to Yahoo
  29. // Lowercases the local part of the Yahoo address (known to be case-insensitive)
  30. yahoo_lowercase: true,
  31. // Removes the subaddress (e.g. "-foo") from the email address
  32. yahoo_remove_subaddress: true,
  33. // The following conversions are specific to Yandex
  34. // Lowercases the local part of the Yandex address (known to be case-insensitive)
  35. yandex_lowercase: true,
  36. // The following conversions are specific to iCloud
  37. // Lowercases the local part of the iCloud address (known to be case-insensitive)
  38. icloud_lowercase: true,
  39. // Removes the subaddress (e.g. "+foo") from the email address
  40. icloud_remove_subaddress: true
  41. }; // List of domains used by iCloud
  42. var icloud_domains = ['icloud.com', 'me.com']; // List of domains used by Outlook.com and its predecessors
  43. // This list is likely incomplete.
  44. // Partial reference:
  45. // https://blogs.office.com/2013/04/17/outlook-com-gets-two-step-verification-sign-in-by-alias-and-new-international-domains/
  46. 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
  47. // This list is likely incomplete
  48. 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
  49. var yandex_domains = ['yandex.ru', 'yandex.ua', 'yandex.kz', 'yandex.com', 'yandex.by', 'ya.ru']; // replace single dots, but not multiple consecutive dots
  50. function dotsReplacer(match) {
  51. if (match.length > 1) {
  52. return match;
  53. }
  54. return '';
  55. }
  56. function normalizeEmail(email, options) {
  57. options = (0, _merge.default)(options, default_normalize_email_options);
  58. var raw_parts = email.split('@');
  59. var domain = raw_parts.pop();
  60. var user = raw_parts.join('@');
  61. var parts = [user, domain]; // 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. }
  124. module.exports = exports.default;
  125. module.exports.default = exports.default;