Skip to content
Browse files

Don't mangle names in nameFromEmail

  • Loading branch information...
1 parent 9d769ae commit dbd87ba0e1f2c8a352b7c50e570b1902c585f0cf @ConradIrwin ConradIrwin committed Feb 10, 2012
Showing with 15 additions and 2 deletions.
  1. +10 −2 public/javascripts/underscore_extensions.js
  2. +5 −0 spec/javascripts/lib/underscore_extensions_spec.js
View
12 public/javascripts/underscore_extensions.js
@@ -183,8 +183,7 @@
if (input.indexOf('@') > -1 && input.indexOf('<') === -1) {
return null; // an email address only, no name present
}
- input = input.replace(/<.+$/, '').replace(/\W{2,}/g, '').trim().
- replace(/^\W+/, '').replace(/\W+$/, '').trim();
+ input = input.replace(/<.+$/, '').replace(_.RE_PUNCT, '').trim();
// If there are email-address like things in the remainder, bail
if (input.indexOf('@') > -1) {
@@ -279,4 +278,13 @@
// The regex also excludes colons from the user part (filtering out mailto:foo@example.com) and
// question marks from the domain part (filtering out foo@example.com?subject=bar).
_.RE_EMAIL = /([a-z0-9!#$%&'\*+\-\/=?\^_`\{\|\}~\.]+@(?:[a-z0-9\-]+)(?:\.[a-z0-9\-]+)+)/i;
+
+ // Copied from jQuery.truncate.js — a list of common punctuation.
+ _.RE_PUNCT = new RegExp('([\"\'/\\\\~|.<>:;\\-=#_' + [
+ "\u00a6", "\u00ab", "\u00b7", "\u00bb", // broken bar, &laquo;, middle dot, &raquo;
+ "\u2010", "\u2011", "\u2012", "\u2013", // hyphen, non-breaking hyphen, figure dash, en dash
+ "\u2014", "\u2015", "\u2016", "\u2022", // em dash, horizontal bar, double bar, bullet
+ "\u2023", "\u2039", "\u203a" // triangular bullet, &lsaquo;, &rsaquo;
+ ].join("") + "])", "g");
+
}(_));
View
5 spec/javascripts/lib/underscore_extensions_spec.js
@@ -112,6 +112,11 @@ describe("_.", function () {
expect(_(' "Lee" - <foo@bar.com>, ').nameFromEmail()).toBe('Lee');
});
+ it("should not mangle foreign names", function () {
+ expect(_("Gerbert Olivé Vázquez").nameFromEmail()).toBe("Gerbert Olivé Vázquez");
+ expect(_("Ævar Arnfjörð Bjarmason").nameFromEmail()).toBe("Ævar Arnfjörð Bjarmason");
+ });
+
it("should return null if a name can't be determined", function () {
var cases = [
'foo@bar.com',

0 comments on commit dbd87ba

Please sign in to comment.
Something went wrong with that request. Please try again.