Skip to content

Instantly share code, notes, and snippets.

@callahad
Created May 21, 2013 04:31
Show Gist options
  • Save callahad/5617492 to your computer and use it in GitHub Desktop.
Save callahad/5617492 to your computer and use it in GitHub Desktop.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const
assert = require('assert'),
gmailEq = require('../lib/gmail_compare.js'),
vows = require('vows');
var suite = vows.describe('Gmail Equivalence');
suite.addBatch({
'Identical valid addresses are equivalent': function() {
assert.isTrue(gmailEq('alice@gmail.com', 'alice@gmail.com'));
},
'Identical invalid addresses are not equivalent': function() {
assert.isFalse(gmailEq('alice-at-gmail.com', 'alice-at-gmail.com'));
assert.isFalse(gmailEq('alice@@gmail.com', 'alice@@gmail.com'));
assert.isFalse(gmailEq('', ''));
assert.isFalse(gmailEq(false, false));
assert.isFalse(gmailEq(undefined, undefined));
},
'Case is ignored': function() {
assert.isTrue(gmailEq('Alice@gmail.com', 'ALiCE@gmail.com'));
},
'Dots are ignored': {
'in the local part': function() {
assert.isTrue(gmailEq('a.l.i.c.e@gmail.com', 'al.ice@gmail.com'));
},
'but not on the right hand side': function() {
assert.isFalse(gmailEq('alice@gmail.com', 'alice@g.mail.com'));
},
},
'Plus addresses are ignored': function() {
assert.isTrue(gmailEq('alice+foo@gmail.com', 'alice+bar@gmail.com'));
},
'Gmail and Googlemail are interchangable': function() {
assert.isTrue(gmailEq('alice@gmail.com', 'alice@googlemail.com'));
}
});
if (process.argv[1] === __filename) {
suite.run();
} else {
suite.export(module);
}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const
valid = require('./validation/email.js');
function gmailUser(s) {
// Normalize case
s = s.toLowerCase();
var parts = s.split('@');
var lhs = parts[0];
var rhs = parts[1];
// Ignore dots
lhs = lhs.replace(/\./g, '');
// Trim plus addresses
if (lhs.indexOf('+') > -1) {
lhs = lhs.slice(0, lhs.indexOf('+'));
}
// Normalize googlemail.com to gmail.com
if (rhs === 'googlemail.com') {
rhs = 'gmail.com';
}
return lhs + '@' + rhs;
}
module.exports = function (a, b) {
// Both arguments must be valid email addresses
if (!valid(a) || !valid(b)) {
return false;
}
a = gmailUser(a);
b = gmailUser(b);
return a === b;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment