Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Sanitizer | Description
**blacklist(input, chars)** | remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`.
**escape(input)** | replace `<`, `>`, `&`, `'`, `"`, `` ` ``, `\` and `/` with HTML entities.
**ltrim(input [, chars])** | trim characters from the left-side of the input.
**normalizeEmail(email [, options])** | canonicalize an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand).<br/><br/>`options` is an object with the following keys and default values:<br/><ul><li>*all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it is case insensitive per RFC 1035.</li><li>*gmail_lowercase: true* - Gmail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Gmail addresses are lowercased regardless of the value of this setting.</li><li>*gmail_remove_dots: true*: Removes dots from the local part of the email address, as Gmail ignores them (e.g. "john.doe" and "johndoe" are considered equal).</li><li>*gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.</li><li>*outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.</li><li>*outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.</li><li>*yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.</li><li>*icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li></ul>
**normalizeEmail(email [, options])** | canonicalize an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand).<br/><br/>`options` is an object with the following keys and default values:<br/><ul><li>*all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it is case insensitive per RFC 1035.</li><li>*gmail_lowercase: true* - Gmail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Gmail addresses are lowercased regardless of the value of this setting.</li><li>*gmail_remove_dots: true*: Removes dots from the local part of the email address, as Gmail ignores them (e.g. "john.doe" and "johndoe" are considered equal).</li><li>*gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.</li><li>*outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.</li><li>*outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.</li><li>*yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.</li><li>*icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*yandex_lowercase: true* - Yandex addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yandex addresses are lowercased regardless of the value of this setting.</li><li>*yandex_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*comcast_lowercase: true* - Comcast addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Comcast addresses are lowercased regardless of the value of this setting.</li><li>*comcast_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*fastmail_lowercase: true* - FastMail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, FastMail addresses are lowercased regardless of the value of this setting.</li><li>*fastmail_remove_dots: true*: Removes dots from the local part of the email address, as FastMail ignores them.</li><li>*fastmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*gmx_lowercase: true* - GMX addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, GMX addresses are lowercased regardless of the value of this setting.</li><li>*gmx_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*mailfence_lowercase: true* - Mailfence addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Mailfence addresses are lowercased regardless of the value of this setting.</li><li>*mailfence_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*proton_lowercase: true* - Proton (ProtonMail) addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Proton addresses are lowercased regardless of the value of this setting.</li><li>*proton_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*skiff_lowercase: true* - Skiff addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Skiff addresses are lowercased regardless of the value of this setting.</li><li>*skiff_remove_dots: true*: Removes dots from the local part of the email address, as Skiff ignores them.</li><li>*skiff_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li><li>*zoho_lowercase: true* - Zoho addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Zoho addresses are lowercased regardless of the value of this setting.</li><li>*zoho_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "[email protected]" becomes "[email protected]").</li></ul>
**rtrim(input [, chars])** | trim characters from the right-side of the input.
**stripLow(input [, keep_new_lines])** | remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript.
**toBoolean(input [, strict])** | convert the input string to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`.
Expand Down
207 changes: 207 additions & 0 deletions src/lib/normalizeEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const default_normalize_email_options = {
// The following conversions are specific to Yandex
// Lowercases the local part of the Yandex address (known to be case-insensitive)
yandex_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
yandex_remove_subaddress: true,
// all yandex domains are equal, this explicitly sets the domain to 'yandex.ru'
yandex_convert_yandexru: true,

Expand All @@ -40,6 +42,52 @@ const default_normalize_email_options = {
icloud_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
icloud_remove_subaddress: true,

// The following conversions are specific to Comcast
// Lowercases the local part of the Comcast address (known to be case-insensitive)
comcast_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
comcast_remove_subaddress: true,

// The following conversions are specific to FastMail
// Lowercases the local part of the FastMail address (known to be case-insensitive)
fastmail_lowercase: true,
// Removes dots from the local part of the email address, as that's ignored by FastMail
fastmail_remove_dots: true,
// Removes the subaddress (e.g. "+foo") from the email address
fastmail_remove_subaddress: true,

// The following conversions are specific to GMX
// Lowercases the local part of the GMX address (known to be case-insensitive)
gmx_lowercase: true,
// Removes the subaddress (e.g. "-foo") from the email address
gmx_remove_subaddress: true,

// The following conversions are specific to Mailfence
// Lowercases the local part of the Mailfence address (known to be case-insensitive)
mailfence_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
mailfence_remove_subaddress: true,

// The following conversions are specific to Proton
// Lowercases the local part of the Proton address (known to be case-insensitive)
proton_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
proton_remove_subaddress: true,

// The following conversions are specific to Skiff
// Lowercases the local part of the Skiff address (known to be case-insensitive)
skiff_lowercase: true,
// Removes dots from the local part of the email address, as that's ignored by Skiff
skiff_remove_dots: true,
// Removes the subaddress (e.g. "+foo") from the email address
skiff_remove_subaddress: true,

// The following conversions are specific to Zoho
// Lowercases the local part of the Zoho address (known to be case-insensitive)
zoho_lowercase: true,
// Removes the subaddress (e.g. "+foo") from the email address
zoho_remove_subaddress: true,
};

// List of domains used by iCloud
Expand Down Expand Up @@ -149,6 +197,8 @@ const yahoo_domains = [
'yahoo.in',
'yahoo.it',
'ymail.com',
'cox.net',
'sbcglobal.net',
];

// List of domains used by yandex.ru
Expand All @@ -161,6 +211,72 @@ const yandex_domains = [
'ya.ru',
];

// List of domains used by Comcast
const comcast_domains = [
'comcast.com',
'comcast.net',
];

// List of domains used by FastMail
const fastmail_domains = [
'fastmail.com',
'fastmail.cn',
'fastmail.co.uk',
'fastmail.com.au',
'fastmail.de',
'fastmail.es',
'fastmail.fm',
'fastmail.fr',
'fastmail.im',
'fastmail.in',
'fastmail.jp',
'fastmail.mx',
'fastmail.net',
'fastmail.nl',
'fastmail.org',
'fastmail.se',
'fastmail.to',
'fastmail.tw',
'fastmail.uk',
'fastmail.us',
'sent.com',
];

// List of domains used by GMX
const gmx_domains = [
'gmx.at',
'gmx.ca',
'gmx.ch',
'gmx.co.uk',
'gmx.com',
'gmx.de',
'gmx.es',
'gmx.fr',
'gmx.net',
'gmx.us',
];

// List of domains used by Mailfence
const mailfence_domains = [
'mailfence.com',
];

// List of domains used by Proton
const proton_domains = [
'proton.me',
'protonmail.com',
];

// List of domains used by Skiff
const skiff_domains = [
'skiff.com',
];

// List of domains used by Zoho
const zoho_domains = [
'zohomail.com',
];

// replace single dots, but not multiple consecutive dots
function dotsReplacer(match) {
if (match.length > 1) {
Expand Down Expand Up @@ -231,10 +347,101 @@ export default function normalizeEmail(email, options) {
parts[0] = parts[0].toLowerCase();
}
} else if (yandex_domains.indexOf(parts[1]) >= 0) {
// Address is Yandex
if (options.yandex_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.yandex_lowercase) {
parts[0] = parts[0].toLowerCase();
}
parts[1] = options.yandex_convert_yandexru ? 'yandex.ru' : parts[1];
} else if (comcast_domains.indexOf(parts[1]) >= 0) {
// Address is Comcast
if (options.comcast_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.comcast_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (fastmail_domains.indexOf(parts[1]) >= 0) {
// Address is FastMail
if (options.fastmail_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (options.fastmail_remove_dots) {
parts[0] = parts[0].replace(/\.+/g, dotsReplacer);
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.fastmail_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (gmx_domains.indexOf(parts[1]) >= 0) {
// Address is GMX (uses - for subaddress like Yahoo)
if (options.gmx_remove_subaddress) {
let components = parts[0].split('-');
parts[0] = (components.length > 1) ? components.slice(0, -1).join('-') : components[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.gmx_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (mailfence_domains.indexOf(parts[1]) >= 0) {
// Address is Mailfence
if (options.mailfence_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.mailfence_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (proton_domains.indexOf(parts[1]) >= 0) {
// Address is Proton
if (options.proton_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.proton_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (skiff_domains.indexOf(parts[1]) >= 0) {
// Address is Skiff
if (options.skiff_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (options.skiff_remove_dots) {
parts[0] = parts[0].replace(/\.+/g, dotsReplacer);
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.skiff_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (zoho_domains.indexOf(parts[1]) >= 0) {
// Address is Zoho
if (options.zoho_remove_subaddress) {
parts[0] = parts[0].split('+')[0];
}
if (!parts[0].length) {
return false;
}
if (options.all_lowercase || options.zoho_lowercase) {
parts[0] = parts[0].toLowerCase();
}
} else if (options.all_lowercase) {
// Any other address
parts[0] = parts[0].toLowerCase();
Expand Down
Loading
Loading