normalizeEmail function
canonicalize an email address.
options
is an Map
which defaults to
{ lowercase: true }
. With lowercase set to true, the local part of the
email address is lowercased for all domains; the hostname is always
lowercased and the local part of the email address is always lowercased
for hosts that are known to be case-insensitive (currently only GMail).
Normalization follows special rules for known providers: currently,
GMail addresses have dots removed in the local part and are stripped of
tags (e.g. some.one+tag@gmail.com
becomes someone@gmail.com
) and all
@googlemail.com
addresses are normalized to @gmail.com
.
Implementation
String normalizeEmail(String email, [Map? options]) {
options = merge(options, _default_normalize_email_options);
if (isEmail(email) == false) {
return '';
}
List parts = email.split('@');
parts[1] = parts[1].toLowerCase();
if (options!['lowercase'] == true) {
parts[0] = parts[0].toLowerCase();
}
if (parts[1] == 'gmail.com' || parts[1] == 'googlemail.com') {
if (options['lowercase'] == false) {
parts[0] = parts[0].toLowerCase();
}
parts[0] = parts[0].replaceAll('\.', '').split('+')[0];
parts[1] = 'gmail.com';
}
return parts.join('@');
}