budibase/packages/server/src/utilities/csv.ts

20 lines
746 B
TypeScript
Raw Normal View History

2023-05-02 11:34:45 +02:00
import csv from "csvtojson"
export async function jsonFromCsvString(csvString: string) {
2023-05-02 12:57:18 +02:00
const castedWithEmptyStrings = await csv().fromString(csvString)
if (!castedWithEmptyStrings.length) {
return []
}
// By default the csvtojson library casts empty values as empty strings. This 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 emptyObject = Object.keys(castedWithEmptyStrings[0]).reduce(
(p, v) => ({ ...p, [v]: undefined }),
{}
)
let result = await csv({ ignoreEmpty: true }).fromString(csvString)
result = result.map(r => ({ ...emptyObject, ...r }))
2023-05-02 11:34:45 +02:00
return result
}