Skip to content

Commit

Permalink
Merge pull request #460 from chriso/chriso/configurable-email-normali…
Browse files Browse the repository at this point in the history
…zation

Configurable GMail address normalization
  • Loading branch information
chriso committed Dec 1, 2015
2 parents 82ed00f + a856995 commit 66ac635
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ bower install validator-js
- **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. `options` is an object 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. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
- **normalizeEmail(email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true, remove_dots: true, remove_extension: 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. `[email protected]` becomes `[email protected]`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
- **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 to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`.
Expand Down
20 changes: 18 additions & 2 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ describe('Sanitizers', function () {
, 'an invalid email address': false
, '': false
, '[email protected]': false
// [email protected] was removed from test cases because of a bug with validator.isEmail. See issue #258
, '[email protected]': false
, '[email protected]': false
, '[email protected]': '[email protected]'
}
});
test({
Expand All @@ -211,13 +213,27 @@ describe('Sanitizers', function () {
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'

// Domains that are known for being case-insensitive are always lowercased
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
, '[email protected]': '[email protected]'
}
});
test({
sanitizer: 'normalizeEmail'
, args: [{remove_dots: false}]
, expect: {
'[email protected]': '[email protected]'
}
});
test({
sanitizer: 'normalizeEmail'
, args: [{remove_extension: false}]
, expect: {
'[email protected]': '[email protected]'
}
});
});

});
15 changes: 11 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,9 @@
};

var default_normalize_email_options = {
lowercase: true
lowercase: true,
remove_dots: true,
remove_extension: true
};

validator.normalizeEmail = function (email, options) {
Expand All @@ -788,11 +790,16 @@
var parts = email.split('@', 2);
parts[1] = parts[1].toLowerCase();
if (parts[1] === 'gmail.com' || parts[1] === 'googlemail.com') {
parts[0] = parts[0].toLowerCase().replace(/\./g, '');
if (parts[0][0] === '+') {
if (options.remove_extension) {
parts[0] = parts[0].split('+')[0];
}
if (options.remove_dots) {
parts[0] = parts[0].replace(/\./g, '');
}
if (!parts[0].length) {
return false;
}
parts[0] = parts[0].split('+')[0];
parts[0] = parts[0].toLowerCase();
parts[1] = 'gmail.com';
} else if (options.lowercase) {
parts[0] = parts[0].toLowerCase();
Expand Down

0 comments on commit 66ac635

Please sign in to comment.