diff --git a/packages/builder/src/components/backend/TableNavigator/utils.js b/packages/builder/src/components/backend/TableNavigator/utils.js index 2889d90cc0..5f13f9df33 100644 --- a/packages/builder/src/components/backend/TableNavigator/utils.js +++ b/packages/builder/src/components/backend/TableNavigator/utils.js @@ -58,7 +58,7 @@ export const parseFile = e => { resolveRows(rows) }) .catch(() => { - reject("cannot parse csv") + reject("cannot parse csv.") }) } }) diff --git a/packages/server/src/utilities/csv.ts b/packages/server/src/utilities/csv.ts index 4ecdbba37e..8540b62941 100644 --- a/packages/server/src/utilities/csv.ts +++ b/packages/server/src/utilities/csv.ts @@ -1,25 +1,47 @@ import csv from "csvtojson" export async function jsonFromCsvString(csvString: string) { - const castedWithEmptyValues = await csv({ ignoreEmpty: true }).fromString( - csvString - ) + const possibleDelimeters = [",", ";", ":", "|", "~", "\t", " "] - // 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 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) { - r[key] = null + 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 + // 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 + // } } } - }) - - return result + if (headerMismatch) { + continue + } else { + return result + } + } + throw new Error("Unable to determine delimiter") }