Improve boolean parsing and ignore empty fields

This commit is contained in:
Julia Friesel
2018-08-07 17:22:38 +02:00
parent a948cf716b
commit 949fe91e9e
2 changed files with 16 additions and 13 deletions
+1 -1
View File
@@ -85,7 +85,7 @@ async function getFileContentAndImport() {
} }
try { try {
importCsv(fileContent) importCsv(fileContent, false)
} catch(err) { } catch(err) {
//TODO //TODO
} }
+6 -3
View File
@@ -230,7 +230,11 @@ function getDbType(modelProperties, path) {
async function importCsv(csv, deleteFirst) { async function importCsv(csv, deleteFirst) {
const cycleDayProperties = db.schema.find(x => x.name === 'CycleDay').properties const cycleDayProperties = db.schema.find(x => x.name === 'CycleDay').properties
const parseFuncs = { const parseFuncs = {
bool: val => val.toLowerCase() === 'false' ? false : true, bool: val => {
if (val.toLowerCase() === 'true') return true
if (val.toLowerCase() === 'false') return false
return val
},
int: parseNumberIfPossible, int: parseNumberIfPossible,
float: parseNumberIfPossible, float: parseNumberIfPossible,
double: parseNumberIfPossible, double: parseNumberIfPossible,
@@ -245,6 +249,7 @@ async function importCsv(csv, deleteFirst) {
} }
const config = { const config = {
ignoreEmpty: true,
colParser: getColumnNamesForCsv().reduce((acc, colName) => { colParser: getColumnNamesForCsv().reduce((acc, colName) => {
const path = colName.split('.') const path = colName.split('.')
const dbType = getDbType(cycleDayProperties, path) const dbType = getDbType(cycleDayProperties, path)
@@ -294,8 +299,6 @@ function tryToCreateCycleDay(day, i) {
const msg = `Error for line ${i + 1}(${day.date}): ${err.message}` const msg = `Error for line ${i + 1}(${day.date}): ${err.message}`
throw new Error(msg) throw new Error(msg)
} }
})
})
} }
function validateHeaders(headers) { function validateHeaders(headers) {