Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Make the name (from the DOM) show up in loading sidebars on compose f…
Browse files Browse the repository at this point in the history
…orms…

ie. when loading a conversation with a draft open.
  • Loading branch information
LeeMallabone committed Feb 8, 2012
1 parent b540dac commit 5ca4f25
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions public/javascripts/underscore_extensions.js
Expand Up @@ -174,6 +174,25 @@
return _.extractEmails(input)[0] || null; 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. // Return a list of all (cleaned) email addresses in a string.
extractEmails: function (input) { extractEmails: function (input) {
return _((input || "").split(_.RE_EMAIL)) return _((input || "").split(_.RE_EMAIL))
Expand Down
29 changes: 29 additions & 0 deletions spec/javascripts/lib/underscore_extensions_spec.js
Expand Up @@ -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.