adds checks for column headers

This commit is contained in:
mikesealey 2025-01-29 15:42:33 +00:00
parent cb331f0d5f
commit d02eea5770
2 changed files with 41 additions and 19 deletions

View File

@ -58,7 +58,7 @@ export const parseFile = e => {
resolveRows(rows) resolveRows(rows)
}) })
.catch(() => { .catch(() => {
reject("cannot parse csv") reject("cannot parse csv.")
}) })
} }
}) })

View File

@ -1,25 +1,47 @@
import csv from "csvtojson" import csv from "csvtojson"
export async function jsonFromCsvString(csvString: string) { export async function jsonFromCsvString(csvString: string) {
const castedWithEmptyValues = await csv({ ignoreEmpty: true }).fromString( const possibleDelimeters = [",", ";", ":", "|", "~", "\t", " "]
csvString
)
// By default the csvtojson library casts empty values as empty strings. This for (let i = 0; i < possibleDelimeters.length; i++) {
// is causing issues on conversion. ignoreEmpty will remove the key completly let numOfHeaders: number | undefined = undefined
// if empty, so creating this empty object will ensure we return the values let headerMismatch = false
// with the keys but empty values
const result = await csv({ const castedWithEmptyValues = await csv({
ignoreEmpty: false, ignoreEmpty: true,
delimiter: [",", ";", ":", "|", "~", "\t", " "], delimiter: possibleDelimeters[i],
}).fromString(csvString) }).fromString(csvString)
result.forEach((r, i) => {
for (const [key] of Object.entries(r).filter(([, value]) => value === "")) { // By default the csvtojson library casts empty values as empty strings. This
if (castedWithEmptyValues[i][key] === undefined) { // is causing issues on conversion. ignoreEmpty will remove the key completly
r[key] = null // 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,
delimiter: possibleDelimeters[i],
}).fromString(csvString)
for (const [i, r] of result.entries()) {
const columns = Object.keys(r)
if (numOfHeaders == null) {
numOfHeaders = columns.length
}
if (numOfHeaders !== columns.length) {
headerMismatch = true
break
}
for (const [key] of Object.entries(r).filter(
([, value]) => value === ""
)) {
// if (castedWithEmptyValues[i][key] === undefined) {
// r[key] = null
// }
} }
} }
}) if (headerMismatch) {
continue
return result } else {
return result
}
}
throw new Error("Unable to determine delimiter")
} }