Trim spaces from values in getFileBaseNameFromItem (#3711)

This commit is contained in:
Tom Najdek 2024-02-19 10:43:19 +01:00 committed by GitHub
parent 61d3c5c85c
commit 1b751d675b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -2187,6 +2187,9 @@ Zotero.Attachments = new function () {
if (truncate) {
value = value.substr(0, truncate);
}
value = value.trim();
if (prefix) {
value = prefix + value;
}

View file

@ -1290,7 +1290,7 @@ describe("Zotero.Attachments", function() {
});
describe("#getFileBaseNameFromItem()", function () {
var item, itemManyAuthors, itemPatent, itemIncomplete, itemBookSection;
var item, itemManyAuthors, itemPatent, itemIncomplete, itemBookSection, itemSpaces;
before(() => {
item = createUnsavedDataObject('item', { title: 'Lorem Ipsum', itemType: 'journalArticle' });
@ -1331,6 +1331,7 @@ describe("Zotero.Attachments", function() {
itemIncomplete = createUnsavedDataObject('item', { title: 'Incomplete', itemType: 'preprint' });
itemBookSection = createUnsavedDataObject('item', { title: 'Book Section', itemType: 'bookSection' });
itemBookSection.setField('bookTitle', 'Book Title');
itemSpaces = createUnsavedDataObject('item', { title: ' Spaces! ', itemType: 'book' });
});
@ -1363,6 +1364,26 @@ describe("Zotero.Attachments", function() {
);
});
it('should trim whitespaces from a value', function () {
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(itemSpaces, '{{ title }}'),
'Spaces!'
);
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{title truncate="6"}}'),
'Lorem'
);
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{firstCreator truncate="7"}}'),
'Barius'
);
// but preserve if it's configured as a prefix or suffix
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{title prefix=" " suffix=" "}}'),
' Lorem Ipsum '
);
});
it('should offer a range of options for composing creators', function () {
assert.equal(
Zotero.Attachments.getFileBaseNameFromItem(item, '{{ authors max="1" }}'),