Skip to content

Instantly share code, notes, and snippets.

@9point6
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9point6/d1e89ea5cb2cea43de59 to your computer and use it in GitHub Desktop.
Save 9point6/d1e89ea5cb2cea43de59 to your computer and use it in GitHub Desktop.
Email address normalizer in CoffeeScript
defaultOptions =
'normalizeGmail': true
'lowerCaseLocal': true
'stripTags': true
'stripComments': true
class EmailNormalizer
constructor: ( @email, options = {} ) ->
@options = defaultOptions
for own k, v of options
@options[k] = v
normalize: ( ) ->
return @normalized if @normalized
out = @email
if @options.lowerCaseLocal
out = out.toLowerCase( )
if @options.stripTags
out = out.replace /^([^+]+)(\+[^@]*)/, '$1'
if @options.stripComments
out = out.replace /^((\([^)]*\))?)([^@]+)((\([^)]*\))?)\@/, '$3@'
if @options.normalizeGmail
out = out.split '@'
if out[1] is 'googlemail.com'
out[1] = 'gmail.com'
out[0] = out[0].replace /\./g, ''
out = out.join '@'
@normalized = out
( module?.exports or window?.EmailNormalizer ) = EmailNormalizer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment