Last active
August 29, 2015 14:04
-
-
Save 9point6/d1e89ea5cb2cea43de59 to your computer and use it in GitHub Desktop.
Email address normalizer in CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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