adds checks for column headers
This commit is contained in:
parent
cb331f0d5f
commit
d02eea5770
|
@ -58,7 +58,7 @@ export const parseFile = e => {
|
|||
resolveRows(rows)
|
||||
})
|
||||
.catch(() => {
|
||||
reject("cannot parse csv")
|
||||
reject("cannot parse csv.")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
import csv from "csvtojson"
|
||||
|
||||
export async function jsonFromCsvString(csvString: string) {
|
||||
const castedWithEmptyValues = await csv({ ignoreEmpty: true }).fromString(
|
||||
csvString
|
||||
)
|
||||
const possibleDelimeters = [",", ";", ":", "|", "~", "\t", " "]
|
||||
|
||||
for (let i = 0; i < possibleDelimeters.length; i++) {
|
||||
let numOfHeaders: number | undefined = undefined
|
||||
let headerMismatch = false
|
||||
|
||||
const castedWithEmptyValues = await csv({
|
||||
ignoreEmpty: true,
|
||||
delimiter: possibleDelimeters[i],
|
||||
}).fromString(csvString)
|
||||
|
||||
// By default the csvtojson library casts empty values as empty strings. This
|
||||
// is causing issues on conversion. ignoreEmpty will remove the key completly
|
||||
|
@ -11,15 +18,30 @@ export async function jsonFromCsvString(csvString: string) {
|
|||
// with the keys but empty values
|
||||
const result = await csv({
|
||||
ignoreEmpty: false,
|
||||
delimiter: [",", ";", ":", "|", "~", "\t", " "],
|
||||
delimiter: possibleDelimeters[i],
|
||||
}).fromString(csvString)
|
||||
result.forEach((r, i) => {
|
||||
for (const [key] of Object.entries(r).filter(([, value]) => value === "")) {
|
||||
if (castedWithEmptyValues[i][key] === undefined) {
|
||||
r[key] = null
|
||||
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
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
||||
throw new Error("Unable to determine delimiter")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue