normalizeEmail.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = normalizeEmail;
  6. var _isEmail = require('./isEmail');
  7. var _isEmail2 = _interopRequireDefault(_isEmail);
  8. var _merge = require('./util/merge');
  9. var _merge2 = _interopRequireDefault(_merge);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var default_normalize_email_options = {
  12. lowercase: true,
  13. remove_dots: true,
  14. remove_extension: true
  15. };
  16. function normalizeEmail(email, options) {
  17. options = (0, _merge2.default)(options, default_normalize_email_options);
  18. if (!(0, _isEmail2.default)(email)) {
  19. return false;
  20. }
  21. var parts = email.split('@', 2);
  22. parts[1] = parts[1].toLowerCase();
  23. if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
  24. if (options.remove_extension) {
  25. parts[0] = parts[0].split('+')[0];
  26. }
  27. if (options.remove_dots) {
  28. parts[0] = parts[0].replace(/\./g, '');
  29. }
  30. if (!parts[0].length) {
  31. return false;
  32. }
  33. parts[0] = parts[0].toLowerCase();
  34. parts[1] = 'gmail.com';
  35. } else if (options.lowercase) {
  36. parts[0] = parts[0].toLowerCase();
  37. }
  38. return parts.join('@');
  39. }
  40. module.exports = exports['default'];