give out .json and .csv, not only .ndjson

This commit is contained in:
Soph :3 2025-12-15 22:46:49 +02:00
parent 2593be8911
commit 124507d074
2 changed files with 27 additions and 1 deletions

14
lib.ts
View file

@ -17,6 +17,20 @@ export const thIgnore = [
"bpmkeytracker",
]
export function jsonToCsv(data: unknown|unknown[]) {
const arr = Array.isArray(data) ? data : [data];
const headers = [...new Set(arr.flatMap(o => Object.keys(o)))];
const escape = (v: string )=>
`"${String(v ?? "").replace(/"/g, '""')}"`;
const rows = arr.map(o =>
headers.map(h => escape(o[h])).join(",")
);
return [headers.join(","), ...rows].join("\n");
}
export enum TripleBool {
MOSTLY = 2,
YES = 1,