give out .json and .csv, not only .ndjson
This commit is contained in:
parent
2593be8911
commit
124507d074
2 changed files with 27 additions and 1 deletions
14
lib.ts
14
lib.ts
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue