isEmail.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isEmail;
  6. var _assertString = require('./util/assertString');
  7. var _assertString2 = _interopRequireDefault(_assertString);
  8. var _merge = require('./util/merge');
  9. var _merge2 = _interopRequireDefault(_merge);
  10. var _isByteLength = require('./isByteLength');
  11. var _isByteLength2 = _interopRequireDefault(_isByteLength);
  12. var _isFQDN = require('./isFQDN');
  13. var _isFQDN2 = _interopRequireDefault(_isFQDN);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. var default_email_options = {
  16. allow_display_name: false,
  17. allow_utf8_local_part: true,
  18. require_tld: true
  19. };
  20. /* eslint-disable max-len */
  21. /* eslint-disable no-control-regex */
  22. var displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
  23. var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
  24. var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
  25. var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
  26. var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
  27. /* eslint-enable max-len */
  28. /* eslint-enable no-control-regex */
  29. function isEmail(str, options) {
  30. (0, _assertString2.default)(str);
  31. options = (0, _merge2.default)(options, default_email_options);
  32. if (options.allow_display_name) {
  33. var display_email = str.match(displayName);
  34. if (display_email) {
  35. str = display_email[1];
  36. }
  37. }
  38. var parts = str.split('@');
  39. var domain = parts.pop();
  40. var user = parts.join('@');
  41. var lower_domain = domain.toLowerCase();
  42. if (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com') {
  43. user = user.replace(/\./g, '').toLowerCase();
  44. }
  45. if (!(0, _isByteLength2.default)(user, { max: 64 }) || !(0, _isByteLength2.default)(domain, { max: 256 })) {
  46. return false;
  47. }
  48. if (!(0, _isFQDN2.default)(domain, { require_tld: options.require_tld })) {
  49. return false;
  50. }
  51. if (user[0] === '"') {
  52. user = user.slice(1, user.length - 1);
  53. return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);
  54. }
  55. var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
  56. var user_parts = user.split('.');
  57. for (var i = 0; i < user_parts.length; i++) {
  58. if (!pattern.test(user_parts[i])) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. module.exports = exports['default'];