improve edited exam changes printing format

This commit is contained in:
Joao Correia
2025-02-09 21:12:29 +00:00
parent ca2cf739ee
commit c14f16c97a

View File

@@ -1,7 +1,7 @@
import { Exam } from "@/interfaces/exam"; import { Exam } from "@/interfaces/exam";
import { diff, Diff } from "deep-diff"; import { diff, Diff } from "deep-diff";
const EXCLUDED_FIELDS = new Set(["_id", "id", "createdAt", "createdBy", "entities", "isDiagnostic", "private", "access", "requiresApproval", "score"]); const EXCLUDED_KEYS = new Set(["_id", "id", "createdAt", "createdBy", "entities", "isDiagnostic", "private", "access", "requiresApproval"]);
const PATH_LABELS: Record<string, string> = { const PATH_LABELS: Record<string, string> = {
parts: "Parts", parts: "Parts",
@@ -32,25 +32,21 @@ function formatDifference(change: Diff<any, any>): string | undefined {
return; return;
} }
if (change.path.some((segment) => EXCLUDED_FIELDS.has(segment))) { if (change.path.some((segment) => EXCLUDED_KEYS.has(segment))) {
return; return;
} }
const pathString = pathToHumanReadable(change.path); const pathString = pathToHumanReadable(change.path);
switch (change.kind) { switch (change.kind) {
case "N": // A new property/element was added case "N": // New property/element
return `• Added ${pathString} with value: ${formatValue(change.rhs)}\n`; return `• Added ${pathString} with value: ${formatValue(change.rhs)}\n`;
case "D": // Deleted property/element
case "D": // A property/element was deleted
return `• Removed ${pathString} which had value: ${formatValue(change.lhs)}\n`; return `• Removed ${pathString} which had value: ${formatValue(change.lhs)}\n`;
case "E": // Edited property/element
case "E": // A property/element was edited
return `• Changed ${pathString} from ${formatValue(change.lhs)} to ${formatValue(change.rhs)}\n`; return `• Changed ${pathString} from ${formatValue(change.lhs)} to ${formatValue(change.rhs)}\n`;
case "A": // Array change
case "A": // An array change; `change.item` describes what happened at array index `change.index`
return formatArrayChange(change); return formatArrayChange(change);
default: default:
return; return;
} }
@@ -59,7 +55,7 @@ function formatDifference(change: Diff<any, any>): string | undefined {
function formatArrayChange(change: Diff<any, any>): string | undefined { function formatArrayChange(change: Diff<any, any>): string | undefined {
if (!change.path) return; if (!change.path) return;
if (change.path.some((segment) => EXCLUDED_FIELDS.has(segment))) { if (change.path.some((segment) => EXCLUDED_KEYS.has(segment))) {
return; return;
} }
@@ -86,13 +82,24 @@ function formatArrayChange(change: Diff<any, any>): string | undefined {
function formatValue(value: any): string { function formatValue(value: any): string {
if (value === null) return "null"; if (value === null) return "null";
if (value === undefined) return "undefined"; if (value === undefined) return "undefined";
if (typeof value === "object") { if (typeof value === "object") {
try { try {
return JSON.stringify(value); return JSON.stringify(
value,
(key, val) => {
if (EXCLUDED_KEYS.has(key)) {
return undefined;
}
return val;
},
2 // optional indentation for readability
);
} catch { } catch {
return String(value); return String(value);
} }
} }
return JSON.stringify(value); return JSON.stringify(value);
} }