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