From dccf4f3ad373ef597b42d80217c12939632b89cf Mon Sep 17 00:00:00 2001 From: John McNair Date: Wed, 27 Apr 2016 13:26:23 -0400 Subject: [PATCH] Parse Office 365 names (#2016) Office 365 likes to format email addresses as follows: Last Name, First Name (Some Description) 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. --- .../lib/message-participants.cjsx | 2 +- spec/models/contact-spec.coffee | 16 ++++++++++++ src/flux/models/contact.coffee | 26 +++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/internal_packages/message-list/lib/message-participants.cjsx b/internal_packages/message-list/lib/message-participants.cjsx index cb3b21a55..4837de7a3 100644 --- a/internal_packages/message-list/lib/message-participants.cjsx +++ b/internal_packages/message-list/lib/message-participants.cjsx @@ -58,7 +58,7 @@ class MessageParticipants extends React.Component if c.name?.length > 0 and c.name isnt c.email
- {c.name} + {c.fullName()}
{"<"}{c.email}{">#{comma}"} diff --git a/spec/models/contact-spec.coffee b/spec/models/contact-spec.coffee index c8e341cc5..c6a383def 100644 --- a/spec/models/contact-spec.coffee +++ b/spec/models/contact-spec.coffee @@ -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" diff --git a/src/flux/models/contact.coffee b/src/flux/models/contact.coffee index acdd7f3c7..78fbda038 100644 --- a/src/flux/models/contact.coffee +++ b/src/flux/models/contact.coffee @@ -165,12 +165,16 @@ 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 - parts = [] - parts = name.split(/\s+/) - parts = _.reject parts, (part) -> - part = part.toLowerCase().replace(/\./,'') - (part of name_prefixes) or (part of name_suffixes) + if parts.join('').length == 0 + parts = [] + parts = name.split(/\s+/) + parts = _.reject parts, (part) -> + part = part.toLowerCase().replace(/\./,'') + (part of name_prefixes) or (part of name_suffixes) # If we've removed all the parts, just return the whole name parts = [name] if parts.join('').length == 0 @@ -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