Sort LC call numbers better, sort integers as integers (#2569)

* Update utilities submodule after https://github.com/zotero/utilities/pull/8
* Extract and add tests
This commit is contained in:
Abe Jellinek 2022-04-26 01:11:29 -07:00 committed by GitHub
parent 7a06671ecb
commit 89aaa10504
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 20 deletions

View file

@ -1354,25 +1354,7 @@ var ItemTree = class ItemTree extends LibraryTree {
}
if (sortField == 'callNumber') {
let deweyRe = /^(\d{3})(?:\.(\d+))?(?:\/([a-zA-Z]{3}))?$/;
let splitA = fieldA.toLowerCase().replace(/\s/g, '').match(deweyRe);
let splitB = fieldB.toLowerCase().replace(/\s/g, '').match(deweyRe);
if (splitA && splitB) {
// Looks like Dewey Decimal, so we'll compare by parts
let i;
for (i = 1; i < splitA.length && i < splitB.length; i++) {
if (splitA[i] < splitB[i]) {
return -1;
}
else if (splitA[i] > splitB[i]) {
return 1;
}
}
return (i < splitA.length) ? 1 : (i < splitB.length) ? -1 : 0;
}
else {
return (fieldA > fieldB) ? 1 : (fieldA < fieldB) ? -1 : 0;
}
return Zotero.Utilities.Item.compareCallNumbers(fieldA, fieldB);
}
return collation.compareString(1, fieldA, fieldB);

@ -1 +1 @@
Subproject commit df2dda23b7787215762d41fc654a8ab7fb99056a
Subproject commit 2d6e330e28f1c46482b93a37e8ceee53af74996c

View file

@ -279,4 +279,50 @@ describe("Zotero.Utilities.Item", function() {
assert.equal(title, 'Annotations');
});
});
describe("#compareCallNumbers()", function () {
function checkSort(numbersInOrder) {
let numbersResorted = [...numbersInOrder]
.sort(() => Math.random() - 0.5) // First shuffle
.sort(Zotero.Utilities.Item.compareCallNumbers); // Then re-sort
assert.deepEqual(numbersResorted, numbersInOrder);
}
it("should correctly order integer call numbers", function () {
let numbersInOrder = [
'1',
'2',
'12',
'20',
'21',
'100',
'101',
];
checkSort(numbersInOrder);
});
it("should correctly order Dewey Decimal call numbers", function () {
let numbersInOrder = [
'641.5/Cor',
'641.5/wol',
'641.55541/Ray',
'641.594/Mun',
'641.5945/Foo',
'641.596/Mon',
'642.000/ABC',
];
checkSort(numbersInOrder);
});
it("should correctly order LC call numbers", function () {
let numbersInOrder = [
'PJ403.B64 C666',
'PJ3930.S49 A53 2015',
'PJ4519 .B9798 A58 1999',
'PJ4519 .B99 A65 1976',
];
checkSort(numbersInOrder);
});
});
});