Item::fromJSON(): Remove invalid-for-type Type-mapped fields

"Type: [val]` in Extra means Item Type to citeproc-js, and Type values
from translators mostly aren't going to be useful if the item type
doesn't have a Type-mapped field.

https://forums.zotero.org/discussion/comment/348864/#Comment_348864
This commit is contained in:
Dan Stillman 2020-02-17 11:35:39 -05:00
parent b2bf60e1d7
commit a9dee2f487
2 changed files with 13 additions and 67 deletions

View file

@ -4408,27 +4408,17 @@ Zotero.Item.prototype.fromJSON = function (json, options = {}) {
}
}
// Special handling for Type fields, since Type isn't a base field and therefore isn't set
// in translators that are doing this
if (!extraFields.has('type')) {
let typeFieldNames = Zotero.ItemFields.getTypeFieldsFromBase('type', true)
// This is actually 'medium' but as of 2/2020 the Embedded Metadata translator
// assigns it along with the other 'type' fields.
.concat('audioFileType');
let typeValue = false;
for (let typeFieldName of typeFieldNames) {
let value = extraFields.get(typeFieldName);
if (value !== undefined) {
if (typeValue === false) {
typeValue = value;
extraFields.set('type', value);
}
if (typeValue == value) {
Zotero.warn(`Removing redundant Extra field '${typeFieldName}' for item `
+ this.libraryKey);
extraFields.delete(typeFieldName);
}
}
// Remove Type-mapped fields from Extra, since 'Type' is mapped to Item Type by citeproc-js
// and Type values mostly aren't going to be useful for item types without a Type-mapped field.
let typeFieldNames = Zotero.ItemFields.getTypeFieldsFromBase('type', true)
// This is actually 'medium' but as of 2/2020 the Embedded Metadata translator
// assigns it along with the other 'type' fields.
.concat('audioFileType');
for (let typeFieldName of typeFieldNames) {
if (extraFields.has(typeFieldName)) {
Zotero.warn(`Removing invalid-for-type Type field '${typeFieldName}' from Extra for item `
+ this.libraryKey);
extraFields.delete(typeFieldName);
}
}
}

View file

@ -1898,24 +1898,7 @@ describe("Zotero.Item", function () {
assert.equal(item.getField('extra'), 'Publisher: Foo');
});
it("should deduplicate invalid-for-type base-mapped Type fields with same values and keep Type when storing in Extra", function () {
var json = {
itemType: "document",
type: "Foo", // Invalid base field
reportType: "Foo", // Invalid base-mapped field
websiteType: "Foo" // Invaid base-mapped field
};
// Confirm that 'type' is still invalid for 'document', in case this changes
assert.isFalse(Zotero.ItemFields.isValidForType(
Zotero.ItemFields.getID('type'),
Zotero.ItemTypes.getID('document')
));
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('extra'), 'Type: Foo');
});
it("should remove invalid-for-type base-mapped Type fields with same values and use Type if not present when storing in Extra", function () {
it("should remove invalid-for-type base-mapped Type fields when storing in Extra", function () {
var json = {
itemType: "document",
reportType: "Foo", // Invalid base-mapped field
@ -1928,34 +1911,7 @@ describe("Zotero.Item", function () {
));
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('extra'), 'Type: Foo');
});
it("shouldn't deduplicate invalid-for-type base-mapped Type fields with different values when storing in Extra", function () {
var json = {
itemType: "document",
reportType: "Foo", // Invalid base-mapped field
type: "Foo", // Invalid base field
websiteType: "Bar" // Invaid base-mapped field with different value
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('extra'), 'Type: Foo\nWebsite Type: Bar');
});
it("shouldn't deduplicate invalid-for-type base-mapped Type fields with different values when storing in Extra", function () {
var json = {
itemType: "artwork",
publisher: "Foo", // Invalid base field
company: "Foo", // Invalid base-mapped field
label: "Bar" // Invaid base-mapped field with different value
};
var item = new Zotero.Item;
item.fromJSON(json);
var parts = item.getField('extra').split('\n');
assert.lengthOf(parts, 2);
assert.include(parts, 'Publisher: Foo');
assert.include(parts, 'Label: Bar');
assert.equal(item.getField('extra'), '');
});
});