getSortTitle: Only strip initial punctuation clusters (#2537)

Specifically exclude @, #, and *
This commit is contained in:
Abe Jellinek 2022-04-14 23:08:14 -07:00 committed by GitHub
parent c9b39675ba
commit e8ad2fab95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 24 deletions

View file

@ -1991,11 +1991,9 @@ Zotero.Items = function() {
'<span style="font-variant:small-caps;">', '<span style="font-variant:small-caps;">',
'<span class="nocase">', '<span class="nocase">',
'</span>', '</span>',
// Any punctuation at the beginning of the string // Any punctuation at the beginning of the string, repeated any number
'^\\p{P}+', // of times, and any opening punctuation that follows
// Initial, opening, closing, final, and "other" punctuation that isn't '^\\s*([^\\P{P}@#*])\\1*[\\p{Ps}"\']*',
// followed by a digit. Doesn't match connectors or dashes.
'[\\p{Pi}\\p{Ps}\\p{Pe}\\p{Pf}\\p{Po}](?!\\d)'
].map(re => Zotero.Utilities.XRegExp(re, 'g')); ].map(re => Zotero.Utilities.XRegExp(re, 'g'));

View file

@ -1192,25 +1192,17 @@ describe("Zotero.Items", function () {
} }
}); });
it("should strip any punctuation at the beginning of the string", function () { it("should strip any punctuation at the beginning of the string besides @, #, and *", function () {
let tests = [ let tests = [
['_title', 'title'], ['_title', 'title'],
['-title', 'title'], ['-title', 'title'],
['-- longer title', 'longer title'], ['-- longer title', 'longer title'],
['"Quoted title', 'Quoted title'] ['-_ longer title with different second character', '_ longer title with different second character'],
]; ['"Quoted title', 'Quoted title'],
['@zotero on Twitter', '@zotero on Twitter'],
for (let [input, expected] of tests) { ['#hashtag', '#hashtag'],
assert.equal(Zotero.Items.getSortTitle(input), expected); ['*special', '*special'],
} ['**repeated', '**repeated']
});
it("should strip quotes", function () {
let tests = [
['A "title"', 'A title'],
['A “title”', 'A title'],
[' xyz ”””', 'xyz'],
['Punctuation', 'Punctuation']
]; ];
for (let [input, expected] of tests) { for (let [input, expected] of tests) {
@ -1231,16 +1223,40 @@ describe("Zotero.Items", function () {
} }
}); });
it("should not strip any punctuation before a digit", function () { it("should strip opening punctuation after string-initial punctuation", function () {
let tests = [ let tests = [
['1.5', '1.5'], ['.[Test]', 'Test]'],
['abc .5', 'abc .5'], ['"Word"', 'Word"'],
['abc 5.', 'abc 5'] ['"@": The Musical', '@": The Musical'],
]; ];
for (let [input, expected] of tests) { for (let [input, expected] of tests) {
assert.equal(Zotero.Items.getSortTitle(input), expected); assert.equal(Zotero.Items.getSortTitle(input), expected);
} }
}); });
it("should sort titles with special characters correctly", function () {
let tests = [
[
['A*B*@!@C*D 1', 'ABCD 2', 'A*B*@!@C*D 3', 'ABCD 4'],
['A*B*@!@C*D 1', 'A*B*@!@C*D 3', 'ABCD 2', 'ABCD 4']
],
[
['Why? Volume 1', 'Why! Volume 1', 'Why! Volume 2', 'Why? Volume 2'],
['Why! Volume 1', 'Why! Volume 2', 'Why? Volume 1', 'Why? Volume 2']
],
[
['Sign and symbol', '"@" Sign. Its accidental history.', 'Sign language'],
['"@" Sign. Its accidental history.', 'Sign and symbol', 'Sign language']
],
];
let st = Zotero.Items.getSortTitle;
let collation = Zotero.getLocaleCollation();
for (let [input, expected] of tests) {
input.sort((a, b) => collation.compareString(1, st(a), st(b)));
assert.deepEqual(input, expected);
}
});
}); });
}); });