allows additional CSV delimiters

This commit is contained in:
mikesealey 2024-11-19 17:18:11 +00:00
parent fd1dee993a
commit a52cdbe54d
2 changed files with 19 additions and 1 deletions

View File

@ -1511,5 +1511,20 @@ datasourceDescribe(
})
})
})
describe("csvToJson", () => {
const delimiters = [",", ";", ":", "|", "~", "\t", " "]
it.only.each(delimiters)(
"can parse CSVs with delimiter; %s",
async delimiter => {
const rows = await config.api.table.csvToJson({
csvString: [
["a", "b", "c", "d", "e"].join(delimiter),
["1", "2", "3", "4", "5"].join(delimiter),
].join("\n"),
})
expect(rows).toEqual([{ a: "1", b: "2", c: "3", d: "4", e: "5" }])
}
)
})
}
)

View File

@ -9,7 +9,10 @@ export async function jsonFromCsvString(csvString: string) {
// is causing issues on conversion. ignoreEmpty will remove the key completly
// if empty, so creating this empty object will ensure we return the values
// with the keys but empty values
const result = await csv({ ignoreEmpty: false }).fromString(csvString)
const result = await csv({
ignoreEmpty: false,
delimiter: [",", ";", ":", "|", "~", "\t", " "],
}).fromString(csvString)
result.forEach((r, i) => {
for (const [key] of Object.entries(r).filter(([, value]) => value === "")) {
if (castedWithEmptyValues[i][key] === undefined) {