1
0
Fork 0
fahrPool/web/lib/audit/csv.ts

37 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { aenderungZeilen, aktionText, bereichText, datensatzText, formatiereZeitpunkt, type AuditEintrag, type NamenMap } from "./bezeichnungen";
const KOPF = ["Zeitpunkt", "Akteur", "Rolle", "Bereich", "Aktion", "Auftragsnummer", "Datensatz", "Änderungen"];
// Semikolon fuer deutsches Excel; Formel-Praefix gegen CSV-Injection.
export function csvFeld(wert: string): string {
let w = /^(\s*[=+\-@]|[\t\r])/.test(wert) ? `'${wert}` : wert;
if (/[;"\r\n]/.test(w)) {
w = `"${w.replace(/"/g, '""')}"`;
}
return w;
}
export function auditCsv(eintraege: AuditEintrag[], namen?: NamenMap): string {
const zeilen = [KOPF.join(";")];
for (const e of eintraege) {
const aenderungen = aenderungZeilen(e, namen)
.map((z) => `${z.feld}: ${z.alt} → ${z.neu}`)
.join(" | ");
zeilen.push(
[
formatiereZeitpunkt(e.zeitpunkt),
e.akteur_name ?? "System",
e.akteur_rolle ?? "",
bereichText(e.bereich),
aktionText(e.aktion),
e.auftragsnummer ?? "",
datensatzText(e, namen) ?? "",
aenderungen,
]
.map(csvFeld)
.join(";")
);
}
return `${zeilen.join("\r\n")}`;
}