From bf1c52a5fd4e910486297d7e10310b3f3c8390c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Sat, 11 Feb 2017 12:56:22 -0300 Subject: [PATCH] Get a better last name if current guess starts with weird symbols --- chrome/content/zotero/xpcom/utilities.js | 10 +++++++--- test/tests/utilitiesTest.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 3851c13fd..2f00a2380 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -179,9 +179,13 @@ Zotero.Utilities = { var lastName = author; } } else { - var spaceIndex = author.lastIndexOf(" "); - var lastName = author.substring(spaceIndex+1); - var firstName = author.substring(0, spaceIndex); + // Don't parse "Firstname Lastname [Country]" as "[Country], Firstname Lastname" + var spaceIndex = author.length; + do { + spaceIndex = author.lastIndexOf(" ", spaceIndex-1); + var lastName = author.substring(spaceIndex + 1); + var firstName = author.substring(0, spaceIndex); + } while (!Zotero.Utilities.XRegExp('\\pL').test(lastName[0]) && spaceIndex > 0) } if(firstName && allCapsRe.test(firstName) && diff --git a/test/tests/utilitiesTest.js b/test/tests/utilitiesTest.js index 6791a70e8..abdca117e 100644 --- a/test/tests/utilitiesTest.js +++ b/test/tests/utilitiesTest.js @@ -16,6 +16,21 @@ describe("Zotero.Utilities", function() { } } }); + + it('should not parse words starting with symbols as last name', function() { + let author = Zotero.Utilities.cleanAuthor('First Middle Last [CountryName]', false); + assert.equal(author.firstName, 'First Middle'); + // Brackets at the beginning and end of a string get removed for strings + // such as [First Last] -> Last, First. + // The current output is not ideal, but better than "[CountryName, First Middle Last" + assert.equal(author.lastName, 'Last [CountryName'); + }); + + it('should parse names starting with unicode characters correctly', function() { + let author = Zotero.Utilities.cleanAuthor('Ąžuolas Žolynas', false); + assert.equal(author.firstName, 'Ąžuolas'); + assert.equal(author.lastName, 'Žolynas'); + }) }); describe("cleanISBN", function() { let cleanISBN = Zotero.Utilities.cleanISBN;