mirror of
https://github.com/ae-utbm/sith.git
synced 2025-01-03 13:41:15 +00:00
fix sanitization of the csv content
This commit is contained in:
parent
180bae59c8
commit
6953eaa9d0
@ -19,6 +19,15 @@ function getNested<T extends object>(obj: T, key: NestedKeyOf<T>) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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, '""')}"`;
|
||||||
|
}
|
||||||
|
|
||||||
export const csv = {
|
export const csv = {
|
||||||
stringify: <T extends object>(objs: T[], options?: StringifyOptions<T>) => {
|
stringify: <T extends object>(objs: T[], options?: StringifyOptions<T>) => {
|
||||||
const columns = options.columns;
|
const columns = options.columns;
|
||||||
@ -26,10 +35,7 @@ export const csv = {
|
|||||||
.map((obj) => {
|
.map((obj) => {
|
||||||
return columns
|
return columns
|
||||||
.map((col) => {
|
.map((col) => {
|
||||||
return (getNested(obj, col) ?? "")
|
return sanitizeCell((getNested(obj, col) ?? "").toString());
|
||||||
.toString()
|
|
||||||
.replace(/,/g, ",")
|
|
||||||
.replace(/\n/g, " ");
|
|
||||||
})
|
})
|
||||||
.join(",");
|
.join(",");
|
||||||
})
|
})
|
||||||
@ -37,7 +43,7 @@ export const csv = {
|
|||||||
if (!options.titleRow) {
|
if (!options.titleRow) {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
const firstRow = options.titleRow.map((s) => s.replace(/,/g, ",")).join(",");
|
const firstRow = options.titleRow.map(sanitizeCell).join(",");
|
||||||
return `${firstRow}\n${content}`;
|
return `${firstRow}\n${content}`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user