From c7fd46e6b4e0f1f2f8add47dc16b9435661c39eb Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 16 Nov 2017 01:15:18 -0500 Subject: [PATCH] Don't ignore whitespace when sorting Intl.Collator's ignorePunctuation ignores whitespace too, so stop using it, since it produces much weirder results than sorting on punctuation does. --- chrome/content/zotero/xpcom/zotero.js | 7 +++++-- test/tests/zoteroTest.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 10f5a4186..847c41e35 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -1546,7 +1546,6 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); } var collator = new Intl.Collator(locales, { - ignorePunctuation: true, numeric: true, sensitivity: 'base' }); @@ -1558,7 +1557,6 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); try { Zotero.logError("Falling back to en-US sorting"); collator = new Intl.Collator(['en-US'], { - ignorePunctuation: true, numeric: true, sensitivity: 'base' }); @@ -1602,6 +1600,11 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); // If initial punctuation is equivalent, use collator comparison // that ignores all punctuation + // + // Update: Intl.Collator's ignorePunctuation also ignores whitespace, so we're + // no longer using it, meaning we could take out most of the code to handle + // initial punctuation separately, unless we think we'll at some point switch to + // a collation function that ignores punctuation but not whitespace. if (aInitP == bInitP || !aInitP && !bInitP) return collator.compare(a, b); // Otherwise consider "attached" words as well, e.g. the order should be diff --git a/test/tests/zoteroTest.js b/test/tests/zoteroTest.js index 40ba81a03..ddd825279 100644 --- a/test/tests/zoteroTest.js +++ b/test/tests/zoteroTest.js @@ -16,4 +16,15 @@ describe("Zotero", function() { assert.equal(str1, str2); }); }); + + + describe("#localeCompare", function () { + it("shouldn't ignore whitespace", function () { + assert.equal(Zotero.localeCompare("Chang", "Chan H"), 1); + }); + + it("shouldn't ignore leading punctuation", function () { + assert.equal(Zotero.localeCompare("_Abcd", "Abcd"), -1); + }); + }); });