improve edited exam changes printing format
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Exam } from "@/interfaces/exam";
|
||||
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> = {
|
||||
parts: "Parts",
|
||||
@@ -32,25 +32,21 @@ function formatDifference(change: Diff<any, any>): string | undefined {
|
||||
return;
|
||||
}
|
||||
|
||||
if (change.path.some((segment) => EXCLUDED_FIELDS.has(segment))) {
|
||||
if (change.path.some((segment) => EXCLUDED_KEYS.has(segment))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pathString = pathToHumanReadable(change.path);
|
||||
|
||||
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`;
|
||||
|
||||
case "D": // A property/element was deleted
|
||||
case "D": // Deleted property/element
|
||||
return `• Removed ${pathString} which had value: ${formatValue(change.lhs)}\n`;
|
||||
|
||||
case "E": // A property/element was edited
|
||||
case "E": // Edited property/element
|
||||
return `• Changed ${pathString} from ${formatValue(change.lhs)} to ${formatValue(change.rhs)}\n`;
|
||||
|
||||
case "A": // An array change; `change.item` describes what happened at array index `change.index`
|
||||
case "A": // Array change
|
||||
return formatArrayChange(change);
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
@@ -59,7 +55,7 @@ function formatDifference(change: Diff<any, any>): string | undefined {
|
||||
function formatArrayChange(change: Diff<any, any>): string | undefined {
|
||||
if (!change.path) return;
|
||||
|
||||
if (change.path.some((segment) => EXCLUDED_FIELDS.has(segment))) {
|
||||
if (change.path.some((segment) => EXCLUDED_KEYS.has(segment))) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,13 +82,24 @@ function formatArrayChange(change: Diff<any, any>): string | undefined {
|
||||
function formatValue(value: any): string {
|
||||
if (value === null) return "null";
|
||||
if (value === undefined) return "undefined";
|
||||
|
||||
if (typeof value === "object") {
|
||||
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 {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user