Fix Item::multiDiff()
This commit is contained in:
parent
277ddc39f8
commit
ae6d560a66
|
@ -3623,27 +3623,23 @@ Zotero.Item.prototype.multiDiff = function (otherItems, ignoreFields) {
|
||||||
var hasDiffs = false;
|
var hasDiffs = false;
|
||||||
|
|
||||||
for (let i = 0; i < otherItems.length; i++) {
|
for (let i = 0; i < otherItems.length; i++) {
|
||||||
let otherItem = otherItems[i];
|
let otherData = otherItems[i].toJSON();
|
||||||
let diff = [];
|
let changeset = Zotero.DataObjectUtilities.diff(thisData, otherData, ignoreFields);
|
||||||
let otherData = otherItem.toJSON();
|
|
||||||
let numDiffs = this.ObjectsClass.diff(thisData, otherData, diff);
|
|
||||||
|
|
||||||
if (numDiffs) {
|
for (let i = 0; i < changeset.length; i++) {
|
||||||
for (let field in diff[1]) {
|
let change = changeset[i];
|
||||||
if (ignoreFields && ignoreFields.indexOf(field) != -1) {
|
|
||||||
|
if (change.op == 'delete') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var value = diff[1][field];
|
if (!alternatives[change.field]) {
|
||||||
|
|
||||||
if (!alternatives[field]) {
|
|
||||||
hasDiffs = true;
|
hasDiffs = true;
|
||||||
alternatives[field] = [value];
|
alternatives[change.field] = [change.value];
|
||||||
}
|
}
|
||||||
else if (alternatives[field].indexOf(value) == -1) {
|
else if (alternatives[change.field].indexOf(change.value) == -1) {
|
||||||
hasDiffs = true;
|
hasDiffs = true;
|
||||||
alternatives[field].push(value);
|
alternatives[change.field].push(change.value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -853,6 +853,38 @@ describe("Zotero.Item", function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
describe("#multiDiff", function () {
|
||||||
|
it("should return set of alternatives for differing fields in other items", function* () {
|
||||||
|
var type = 'item';
|
||||||
|
|
||||||
|
var dates = ['2016-03-08 17:44:45'];
|
||||||
|
var accessDates = ['2016-03-08T18:44:45Z'];
|
||||||
|
var urls = ['http://www.example.com', 'http://example.net'];
|
||||||
|
|
||||||
|
var obj1 = createUnsavedDataObject(type);
|
||||||
|
obj1.setField('date', '2016-03-07 12:34:56'); // different in 1 and 3, not in 2
|
||||||
|
obj1.setField('url', 'http://example.com'); // different in all three
|
||||||
|
obj1.setField('title', 'Test'); // only in 1
|
||||||
|
|
||||||
|
var obj2 = createUnsavedDataObject(type);
|
||||||
|
obj2.setField('url', urls[0]);
|
||||||
|
obj2.setField('accessDate', accessDates[0]); // only in 2
|
||||||
|
|
||||||
|
var obj3 = createUnsavedDataObject(type);
|
||||||
|
obj3.setField('date', dates[0]);
|
||||||
|
obj3.setField('url', urls[1]);
|
||||||
|
|
||||||
|
var alternatives = obj1.multiDiff([obj2, obj3]);
|
||||||
|
|
||||||
|
assert.sameMembers(Object.keys(alternatives), ['url', 'date', 'accessDate']);
|
||||||
|
assert.sameMembers(alternatives.url, urls);
|
||||||
|
assert.sameMembers(alternatives.date, dates);
|
||||||
|
assert.sameMembers(alternatives.accessDate, accessDates);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("#clone()", function () {
|
describe("#clone()", function () {
|
||||||
// TODO: Expand to other data
|
// TODO: Expand to other data
|
||||||
it("should copy creators", function* () {
|
it("should copy creators", function* () {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user