Sith/core/static/bundled/utils/csv.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-12-13 23:08:11 +00:00
import type { NestedKeyOf } from "#core:utils/types";
interface StringifyOptions<T extends object> {
/** The columns to include in the resulting CSV. */
columns: readonly NestedKeyOf<T>[];
/** Content of the first row */
titleRow?: readonly string[];
}
function getNested<T extends object>(obj: T, key: NestedKeyOf<T>) {
const path: (keyof object)[] = key.split(".") as (keyof unknown)[];
let res = obj[path.shift() as keyof T];
for (const node of path) {
if (res === null) {
break;
}
res = res[node];
}
return res;
}
2024-12-18 16:11:20 +00:00
/**
* Convert the content the string to make sure it won't break
* the resulting csv.
* cf. https://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules
*/
function sanitizeCell(content: string): string {
return `"${content.replace(/"/g, '""')}"`;
}
2024-12-13 23:08:11 +00:00
export const csv = {
stringify: <T extends object>(objs: T[], options?: StringifyOptions<T>) => {
const columns = options.columns;
const content = objs
.map((obj) => {
return columns
.map((col) => {
2024-12-18 16:11:20 +00:00
return sanitizeCell((getNested(obj, col) ?? "").toString());
2024-12-13 23:08:11 +00:00
})
.join(",");
})
.join("\n");
if (!options.titleRow) {
return content;
}
2024-12-18 16:11:20 +00:00
const firstRow = options.titleRow.map(sanitizeCell).join(",");
2024-12-13 23:08:11 +00:00
return `${firstRow}\n${content}`;
},
};