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 { v4 as uuidv4 } from 'uuid';
import { assertDefined } from './assert';
export default class Item { export default class Item {
page: number; page: number;
data: object; data: object;
uuid?: string; uuid: string;
constructor(page: number, data: object, uuid: string = uuidv4()) { constructor(page: number, data: object, uuid: string = uuidv4()) {
this.page = page; this.page = page;

View File

@ -18,7 +18,7 @@ export default class ChangeTracker implements ChangeIndex {
private changes: Map<string, Change> = new Map(); private changes: Map<string, Change> = new Map();
private addChange(item: Item, change: Change) { private addChange(item: Item, change: Change) {
const uuid = _uuid(item); const uuid = item.uuid;
assertNot( assertNot(
this.changes.has(uuid), this.changes.has(uuid),
`Change for item ${uuid} already defined! (old: ${JSON.stringify(this.changes.get(uuid))}, new: ${JSON.stringify( `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 { change(item: Item): Change | undefined {
return this.changes.get(_uuid(item)); return this.changes.get(item.uuid);
} }
hasChanged(item: Item): boolean { hasChanged(item: Item): boolean {
return this.changes.has(_uuid(item)); return this.changes.has(item.uuid);
} }
isPlusChange(item: Item): boolean { isPlusChange(item: Item): boolean {
@ -76,7 +76,3 @@ export default class ChangeTracker implements ChangeIndex {
return this.change(item)?.constructor.name === REMOVAL.constructor.name; 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 { evaluated(item: Item): boolean {
return this.evaluations.has(_uuid(item)); return this.evaluations.has(item.uuid);
} }
evaluationScore(item: Item) { evaluationScore(item: Item) {
return this.evaluations.get(_uuid(item)); return this.evaluations.get(item.uuid);
} }
trackEvaluation(item: Item, score: any = undefined) { trackEvaluation(item: Item, score: any = undefined) {
if (typeof score !== 'undefined') { if (typeof score !== 'undefined') {
this.scored = true; 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; let outputIndex = 0;
for (let inputIdx = 0; inputIdx < inputItems.length; inputIdx++) { for (let inputIdx = 0; inputIdx < inputItems.length; inputIdx++) {
const inputItem = inputItems[inputIdx]; const inputItem = inputItems[inputIdx];
if (addedItems.has(_uuid(inputItem))) { if (addedItems.has(inputItem.uuid)) {
continue; continue;
} }
const positionInOutput = outputItems.findIndex((item) => item.uuid === inputItem.uuid); const positionInOutput = outputItems.findIndex((item) => item.uuid === inputItem.uuid);
if (positionInOutput < 0) { if (positionInOutput < 0) {
tracker.trackRemoval(inputItem); tracker.trackRemoval(inputItem);
mergedItems.push(inputItem); mergedItems.push(inputItem);
addedItems.add(_uuid(inputItem)); addedItems.add(inputItem.uuid);
removals++; removals++;
} else if (positionInOutput === inputIdx + additions - removals) { } else if (positionInOutput === inputIdx + additions - removals) {
mergedItems.push(outputItems[positionInOutput]); mergedItems.push(outputItems[positionInOutput]);
addedItems.add(_uuid(outputItems[positionInOutput])); addedItems.add(outputItems[positionInOutput].uuid);
outputIndex++; outputIndex++;
//TODO check for content change ? //TODO check for content change ?
} else { } else {
@ -52,19 +52,19 @@ function detectPageChanges(tracker: ChangeTracker, inputItems: Item[], outputIte
if (positionInInput < 0) { if (positionInInput < 0) {
tracker.trackAddition(outputItem); tracker.trackAddition(outputItem);
mergedItems.push(outputItem); mergedItems.push(outputItem);
addedItems.add(_uuid(outputItem)); addedItems.add(outputItem.uuid);
additions++; additions++;
outputIndex++; outputIndex++;
} else { } else {
tracker.trackPositionalChange(outputItem, positionInInput - removals, intermediateOutputIdx - additions); tracker.trackPositionalChange(outputItem, positionInInput - removals, intermediateOutputIdx - additions);
mergedItems.push(outputItem); mergedItems.push(outputItem);
addedItems.add(_uuid(outputItem)); addedItems.add(outputItem.uuid);
outputIndex++; outputIndex++;
} }
} }
tracker.trackPositionalChange(outputItems[positionInOutput], inputIdx - removals, positionInOutput - additions); tracker.trackPositionalChange(outputItems[positionInOutput], inputIdx - removals, positionInOutput - additions);
mergedItems.push(outputItems[positionInOutput]); mergedItems.push(outputItems[positionInOutput]);
addedItems.add(_uuid(outputItems[positionInOutput])); addedItems.add(outputItems[positionInOutput].uuid);
outputIndex++; outputIndex++;
//TODO check for content change ? //TODO check for content change ?
} }
@ -76,7 +76,3 @@ function detectPageChanges(tracker: ChangeTracker, inputItems: Item[], outputIte
} }
return mergedItems; return mergedItems;
} }
function _uuid(item: Item): string {
return assertDefined(item.uuid, 'UUID is not set');
}