Permalink
Browse files

Make the name (from the DOM) show up in loading sidebars on compose f…

…orms…

ie. when loading a conversation with a draft open.
  • Loading branch information...
1 parent b540dac commit 5ca4f257ad6e8ae9e6f7435f95e89471ddfcdbfd @fblee fblee committed Feb 7, 2012
Showing with 48 additions and 0 deletions.
  1. +19 −0 public/javascripts/underscore_extensions.js
  2. +29 −0 spec/javascripts/lib/underscore_extensions_spec.js
@@ -174,6 +174,25 @@
return _.extractEmails(input)[0] || null;
},
+ // Takes a string that may contain a name, email address and arbitrary punctuation,
+ // and tries to return a sanitized version of just the name.
+ nameFromEmail: function (input) {
+ if (!input) {
+ return null;
+ }
+ 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();
+
+ // If there are email-address like things in the remainder, bail
+ if (input.indexOf('@') > -1) {
+ return null;
+ }
+ return input || null;
+ },
+
// Return a list of all (cleaned) email addresses in a string.
extractEmails: function (input) {
return _((input || "").split(_.RE_EMAIL))
@@ -98,4 +98,33 @@ describe("_.", function () {
});
});
});
+
+ describe("nameFromEmail", function () {
+ it("should return a name from a name-email address string", function () {
+ expect(_('Lee Mallabone <lee@rapportive.com>').nameFromEmail()).toBe('Lee Mallabone');
+ });
+
+ it("should handle quotes", function () {
+ expect(_('"Lee Mallabone" <foo@bar.com>').nameFromEmail()).toBe('Lee Mallabone');
+ });
+
+ it("should sanitize in the face of dubious punctuation", function () {
+ expect(_(' "Lee" - <foo@bar.com>, ').nameFromEmail()).toBe('Lee');
+ });
+
+ it("should return null if a name can't be determined", function () {
+ var cases = [
+ 'foo@bar.com',
+ '"" <foo@rapportive.com>',
+ '<foo@gmail.com>',
+ 'foo@bar.com <foo@bar.com>',
+ 'lee@rapportive.com <lee.mallabone@gmail.com>',
+ '"lee@localhost" <lee@rapportive.com>'
+ ];
+
+ _(cases).each(function (email) {
+ expect(_(email).nameFromEmail()).toBeNull();
+ });
+ });
+ });
});

0 comments on commit 5ca4f25

Please sign in to comment.