Parse Office 365 names (#2016)

Office 365 likes to format email addresses as follows:
Last Name, First Name (Some Description) <email@address.org>

This causes Contact.firstName() (and hence sometimes displayName()) to return
"Last Name," which looks a bit odd in the message/thread views.

The intent of the commit is to correctly parse these names so that (using the
above example):
- firstName = "First Name"
- lastName = "Last Name (Some Description)"
- fullName = "First Name Last Name (Some Description)"

These behavioral changes only impact names containing a ','.

I don't know that this really provides exhaustive coverage of Office 365, and
keeping the description as part of the last name is not completely guilt free,
but it's not any worse than the previous state of affairs which also has
the description in the same field but combined instead with (misplaced)
first name data.
This commit is contained in:
John McNair 2016-04-27 13:26:23 -04:00 committed by Ben Gotow
parent cbded86bd4
commit dccf4f3ad3
3 changed files with 38 additions and 6 deletions

View file

@ -58,7 +58,7 @@ class MessageParticipants extends React.Component
if c.name?.length > 0 and c.name isnt c.email
<div key={"#{c.email}-#{i}"} className="participant selectable">
<div className="participant-primary" onClick={@_selectText}>
{c.name}
{c.fullName()}
</div>
<div className="participant-secondary">
{"<"}<span onClick={@_selectText}>{c.email}</span>{">#{comma}"}

View file

@ -116,6 +116,22 @@ describe "Contact", ->
expect(c8.firstName()).toBe "Mike"
expect(c8.lastName()).toBe "K@ylor"
it "properly parses names with last, first (description)", ->
c1 = new Contact {name: "Smith, Bob"}
expect(c1.firstName()).toBe "Bob"
expect(c1.lastName()).toBe "Smith"
expect(c1.fullName()).toBe "Bob Smith"
c2 = new Contact {name: "von Smith, Ricky Bobby"}
expect(c2.firstName()).toBe "Ricky Bobby"
expect(c2.lastName()).toBe "von Smith"
expect(c2.fullName()).toBe "Ricky Bobby von Smith"
c3 = new Contact {name: "von Smith, Ricky Bobby (Awesome Employee)"}
expect(c3.firstName()).toBe "Ricky Bobby"
expect(c3.lastName()).toBe "von Smith (Awesome Employee)"
expect(c3.fullName()).toBe "Ricky Bobby von Smith (Awesome Employee)"
it "should properly return `You` as the display name for the current user", ->
c1 = new Contact {name: " Test Monkey", email: @account.emailAddress}
expect(c1.displayName()).toBe "You"

View file

@ -165,7 +165,11 @@ class Contact extends Model
# Take care of whitespace
name = name.trim()
# Handle last name, first name
parts = @_parseReverseNames(name)
# Split the name into words and remove parts that are prefixes and suffixes
if parts.join('').length == 0
parts = []
parts = name.split(/\s+/)
parts = _.reject parts, (part) ->
@ -180,6 +184,18 @@ class Contact extends Model
parts
_parseReverseNames: (name) ->
parts = []
[lastName, firstName] = name.split(',')
if firstName
[firstName, description] = firstName.split('(')
parts.push(firstName.trim())
parts.push(lastName.trim())
parts.push("(" + description.trim()) if description
parts
module.exports = Contact
_.each ['2dlt','2lt','2nd lieutenant','adm','administrative','admiral','amb','ambassador','attorney','atty','baron','baroness','bishop','br','brig gen or bg','brigadier general','brnss','brother','capt','captain','chancellor','chaplain','chapln','chief petty officer','cmdr','cntss','coach','col','colonel','commander','corporal','count','countess','cpl','cpo','cpt','doctor','dr','dr and mrs','drs','duke','ens','ensign','estate of','father','father','fr','frau','friar','gen','general','gov','governor','hon','honorable','judge','justice','lieutenant','lieutenant colonel','lieutenant commander','lieutenant general','lieutenant junior grade','lord','lt','ltc','lt cmdr','lt col','lt gen','ltg','lt jg','m','madame','mademoiselle','maj','maj','master sergeant','master sgt','miss','miss','mlle','mme','monsieur','monsignor','monsignor','mr','mr','mr & dr','mr and dr','mr & mrs','mr and mrs','mrs & mr','mrs and mr','ms','ms','msgr','msgr','ofc','officer','president','princess','private','prof','prof & mrs','professor','pvt','rabbi','radm','rear admiral','rep','representative','rev','reverend','reverends','revs','right reverend','rtrev','s sgt','sargent','sec','secretary','sen','senator','senor','senora','senorita','sergeant','sgt','sgt','sheikh','sir','sister','sister','sr','sra','srta','staff sergeant','superintendent','supt','the hon','the honorable','the venerable','treas','treasurer','trust','trustees of','vadm','vice admiral'], (prefix) -> name_prefixes[prefix] = true