Make Zotero.ItemFields.getBaseIDFromTypeAndField() more resilient

- Return base field instead of throwing if base field is passed for a
  type that has a base-mapped field
- Return false instead of throwing for invalid type-field combination
This commit is contained in:
Dan Stillman 2024-08-14 03:12:19 -04:00
parent 53ad856ef4
commit ec50539e80
2 changed files with 18 additions and 8 deletions

View file

@ -299,6 +299,7 @@ Zotero.ItemFields = new function() {
* 'audioRecording' and 'label' returns publisher's fieldID
* 'book' and 'publisher' returns publisher's fieldID
* 'audioRecording' and 'runningTime' returns false
* 'note' and 'runningTime' returns false
*
* Accepts names or ids
*/
@ -312,12 +313,8 @@ Zotero.ItemFields = new function() {
_fieldCheck(typeField);
if (!this.isValidForType(typeFieldID, itemTypeID)) {
throw new Error("'" + typeField + "' is not a valid field for '" + itemType + "'");
}
// If typeField is already a base field, just return that
if (this.isBaseField(typeFieldID)) {
if (_baseTypeFields[itemTypeID][typeFieldID]) {
return typeFieldID;
}

View file

@ -23,12 +23,25 @@ describe("Zotero.ItemFields", function () {
Zotero.ItemFields.getBaseIDFromTypeAndField('book', 'publisher'),
Zotero.ItemFields.getID('publisher')
);
})
});
it("should return the base field id for an item type and base field when type has a base-mapped field", function () {
assert.equal(
Zotero.ItemFields.getBaseIDFromTypeAndField('hearing', 'number'),
Zotero.ItemFields.getID('number')
);
});
it("should return false for an item type and non-base-mapped field", function* () {
assert.isFalse(
Zotero.ItemFields.getBaseIDFromTypeAndField('audioRecording', 'runningTime')
);
})
})
});
it("should return false for invalid type-field combination", function () {
assert.isFalse(
Zotero.ItemFields.getBaseIDFromTypeAndField('note', 'runningTime')
);
});
});
})