Make item#uuid required (just set blank for tests)

This commit is contained in:
Johannes Zillmann 2021-04-11 18:30:32 +02:00
parent a427806f68
commit 9fcb431a64
4 changed files with 14 additions and 25 deletions

View File

@ -1,9 +1,10 @@
import { v4 as uuidv4 } from 'uuid';
import { assertDefined } from './assert';
export default class Item {
page: number;
data: object;
uuid?: string;
uuid: string;
constructor(page: number, data: object, uuid: string = uuidv4()) {
this.page = page;

View File

@ -18,7 +18,7 @@ export default class ChangeTracker implements ChangeIndex {
private changes: Map<string, Change> = new Map();
private addChange(item: Item, change: Change) {
const uuid = _uuid(item);
const uuid = item.uuid;
assertNot(
this.changes.has(uuid),
`Change for item ${uuid} already defined! (old: ${JSON.stringify(this.changes.get(uuid))}, new: ${JSON.stringify(
@ -53,11 +53,11 @@ export default class ChangeTracker implements ChangeIndex {
}
change(item: Item): Change | undefined {
return this.changes.get(_uuid(item));
return this.changes.get(item.uuid);
}
hasChanged(item: Item): boolean {
return this.changes.has(_uuid(item));
return this.changes.has(item.uuid);
}
isPlusChange(item: Item): boolean {
@ -76,7 +76,3 @@ export default class ChangeTracker implements ChangeIndex {
return this.change(item)?.constructor.name === REMOVAL.constructor.name;
}
}
function _uuid(item: Item): string {
return assertDefined(item.uuid, 'UUID is not set');
}

View File

@ -15,21 +15,17 @@ export default class EvaluationTracker implements EvaluationIndex {
}
evaluated(item: Item): boolean {
return this.evaluations.has(_uuid(item));
return this.evaluations.has(item.uuid);
}
evaluationScore(item: Item) {
return this.evaluations.get(_uuid(item));
return this.evaluations.get(item.uuid);
}
trackEvaluation(item: Item, score: any = undefined) {
if (typeof score !== 'undefined') {
this.scored = true;
}
this.evaluations.set(_uuid(item), score);
this.evaluations.set(item.uuid, score);
}
}
function _uuid(item: Item): string {
return assertDefined(item.uuid, 'UUID is not set');
}

View File

@ -31,18 +31,18 @@ function detectPageChanges(tracker: ChangeTracker, inputItems: Item[], outputIte
let outputIndex = 0;
for (let inputIdx = 0; inputIdx < inputItems.length; inputIdx++) {
const inputItem = inputItems[inputIdx];
if (addedItems.has(_uuid(inputItem))) {
if (addedItems.has(inputItem.uuid)) {
continue;
}
const positionInOutput = outputItems.findIndex((item) => item.uuid === inputItem.uuid);
if (positionInOutput < 0) {
tracker.trackRemoval(inputItem);
mergedItems.push(inputItem);
addedItems.add(_uuid(inputItem));
addedItems.add(inputItem.uuid);
removals++;
} else if (positionInOutput === inputIdx + additions - removals) {
mergedItems.push(outputItems[positionInOutput]);
addedItems.add(_uuid(outputItems[positionInOutput]));
addedItems.add(outputItems[positionInOutput].uuid);
outputIndex++;
//TODO check for content change ?
} else {
@ -52,19 +52,19 @@ function detectPageChanges(tracker: ChangeTracker, inputItems: Item[], outputIte
if (positionInInput < 0) {
tracker.trackAddition(outputItem);
mergedItems.push(outputItem);
addedItems.add(_uuid(outputItem));
addedItems.add(outputItem.uuid);
additions++;
outputIndex++;
} else {
tracker.trackPositionalChange(outputItem, positionInInput - removals, intermediateOutputIdx - additions);
mergedItems.push(outputItem);
addedItems.add(_uuid(outputItem));
addedItems.add(outputItem.uuid);
outputIndex++;
}
}
tracker.trackPositionalChange(outputItems[positionInOutput], inputIdx - removals, positionInOutput - additions);
mergedItems.push(outputItems[positionInOutput]);
addedItems.add(_uuid(outputItems[positionInOutput]));
addedItems.add(outputItems[positionInOutput].uuid);
outputIndex++;
//TODO check for content change ?
}
@ -76,7 +76,3 @@ function detectPageChanges(tracker: ChangeTracker, inputItems: Item[], outputIte
}
return mergedItems;
}
function _uuid(item: Item): string {
return assertDefined(item.uuid, 'UUID is not set');
}