mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-23 00:01:16 +00:00
add csv converter
This commit is contained in:
parent
1696a2f579
commit
3fc260a12c
43
core/static/bundled/utils/csv.ts
Normal file
43
core/static/bundled/utils/csv.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
return (getNested(obj, col) ?? "")
|
||||||
|
.toString()
|
||||||
|
.replace(/,/g, ",")
|
||||||
|
.replace(/\n/g, " ");
|
||||||
|
})
|
||||||
|
.join(",");
|
||||||
|
})
|
||||||
|
.join("\n");
|
||||||
|
if (!options.titleRow) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
const firstRow = options.titleRow.map((s) => s.replace(/,/g, ",")).join(",");
|
||||||
|
return `${firstRow}\n${content}`;
|
||||||
|
},
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user