From 55068a00597c6d6dd2b9cb285ab5688bedded67b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 26 Nov 2014 20:09:58 -0500 Subject: [PATCH] Use the whole firstCreator string in sortCreatorAsString mode And in the default mode, use the whole string up through 'and' or 'et al.' before falling back to full creator sorting, which could speed things up slightly. --- chrome/content/zotero/xpcom/itemTreeView.js | 37 +++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 6b0c8de16..f01178727 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -1444,22 +1444,47 @@ Zotero.ItemTreeView.prototype.sort = function(itemID) var firstCreatorSortCache = {}; + // Regexp to extract the whole string up to an optional "and" or "et al." + var andEtAlRegExp = new RegExp( + // Extract the beginning of the string in non-greedy mode + "^.+?" + // up to either the end of the string, "et al." at the end of string + + "(?=(?: " + Zotero.getString('general.etAl').replace('.', '\.') + ")?$" + // or ' and ' + + "| " + Zotero.getString('general.and') + " " + + ")" + ); + function creatorSort(a, b) { // - // Try sorting by first word in firstCreator field, since we already have it + // Try sorting by the first name in the firstCreator field, since we already have it + // + // For sortCreatorAsString mode, just use the whole string // var aItemID = a.id, bItemID = b.id, fieldA = firstCreatorSortCache[aItemID], fieldB = firstCreatorSortCache[bItemID]; if (fieldA === undefined) { - var matches = Zotero.Items.getSortTitle(a.getField('firstCreator')).match(/^[^\s]+/); - var fieldA = matches ? matches[0] : ''; + let firstCreator = Zotero.Items.getSortTitle(a.getField('firstCreator')); + if (sortCreatorAsString) { + var fieldA = firstCreator; + } + else { + var matches = andEtAlRegExp.exec(firstCreator); + var fieldA = matches ? matches[0] : ''; + } firstCreatorSortCache[aItemID] = fieldA; } if (fieldB === undefined) { - var matches = Zotero.Items.getSortTitle(b.getField('firstCreator')).match(/^[^\s]+/); - var fieldB = matches ? matches[0] : ''; + let firstCreator = Zotero.Items.getSortTitle(b.getField('firstCreator')); + if (sortCreatorAsString) { + var fieldB = firstCreator; + } + else { + var matches = andEtAlRegExp.exec(firstCreator); + var fieldB = matches ? matches[0] : ''; + } firstCreatorSortCache[bItemID] = fieldB; } @@ -1473,7 +1498,7 @@ Zotero.ItemTreeView.prototype.sort = function(itemID) } // - // If first word is the same, compare actual creators + // If first name is the same, compare actual creators // var aRef = a.ref, bRef = b.ref,