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 class="nocase">',
'</span>',
// Any punctuation at the beginning of the string
'^\\p{P}+',
// Initial, opening, closing, final, and "other" punctuation that isn't
// followed by a digit. Doesn't match connectors or dashes.
'[\\p{Pi}\\p{Ps}\\p{Pe}\\p{Pf}\\p{Po}](?!\\d)'
// Any punctuation at the beginning of the string, repeated any number
// of times, and any opening punctuation that follows
'^\\s*([^\\P{P}@#*])\\1*[\\p{Ps}"\']*',
].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 = [
['_title', 'title'],
['-title', 'title'],
['-- longer title', 'longer title'],
['"Quoted title', 'Quoted title']
];
for (let [input, expected] of tests) {
assert.equal(Zotero.Items.getSortTitle(input), expected);
}
});
it("should strip quotes", function () {
let tests = [
['A "title"', 'A title'],
['A “title”', 'A title'],
[' xyz ”””', 'xyz'],
['Punctuation', 'Punctuation']
['-_ longer title with different second character', '_ longer title with different second character'],
['"Quoted title', 'Quoted title'],
['@zotero on Twitter', '@zotero on Twitter'],
['#hashtag', '#hashtag'],
['*special', '*special'],
['**repeated', '**repeated']
];
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 = [
['1.5', '1.5'],
['abc .5', 'abc .5'],
['abc 5.', 'abc 5']
['.[Test]', 'Test]'],
['"Word"', 'Word"'],
['"@": The Musical', '@": The Musical'],
];
for (let [input, expected] of tests) {
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);
}
});
});
});