Use papaparse for parsing CSV

This commit is contained in:
2022-11-13 02:20:02 +01:00
parent 8de7ec1809
commit 2a5c158ac7
6 changed files with 53 additions and 76 deletions

View File

@ -24,59 +24,13 @@ class CsvxmlValidator {
this.fieldList = Object.freeze(fieldList);
const lines = csvRaw.trim().replace("\r\n", "\n").split("\n");
const data = Papa.parse(csvRaw.trim(), {header: true});
let separator;
let delimiter;
if (csvRaw.substr(0, 1) === '"') {
separator = '";"';
delimiter = '"'
}
else {
separator = ';';
delimiter = '';
if (data.errors.length !== 0) {
window.alert(data.errors);
}
// Gets first line
let headersFields = lines.shift();
let headers;
if (delimiter === "") headers = headersFields.split(separator);
else headers = headersFields.substr(1, headersFields.length - 2).split(separator);
let expectedFieldCount = headers.length;
let toValidate = [];
let lineCounter = 1;
for (let line of lines) {
if (delimiter !== '') {
line = line.substr(1, line.length - 2);
}
// Remove fully empty lines (both without content and those with fields set up,
// but without content there.
if (line.length <= headers.length) continue;
let lineContents = {};
let fields = line.split(separator);
if (fields.length !== headers.length) {
this.errors.parsing.push("Number of columns in line " + lineCounter + " does not match number of headers");
}
for (let i = 0, max = fields.length; i < max; i++) {
if (headers[i] === undefined || headers[i] === null) {
this.errors.parsing.push("ERROR parsing line " + lineCounter + "; column " + i);
continue;
}
lineContents[headers[i]] = fields[i];
}
// Skip totally empty lines
if (Object.values(lineContents).join("").length === 0) continue;
toValidate.push(lineContents);
lineCounter++;
}
let toValidate = data.data;
this.toValidate = toValidate;
@ -577,18 +531,38 @@ class CsvxmlPage {
reader.onload = function() {
// On loading success, check if the upload is valid JSON
console.log("Read file");
// Validate the file
let validator = new CsvxmlValidator(app.fieldListFlat, reader.result);
document.body.classList.remove("loading");
if (validator.isValid() === true) {
alert("Document is valid. Press ok to download.");
app.zipUploadToXml(validator);
function handleValidation() {
// On loading success, check if the upload is valid JSON
console.log("Read file");
// Validate the file
let validator = new CsvxmlValidator(app.fieldListFlat, reader.result);
document.body.classList.remove("loading");
if (validator.isValid() === true) {
alert("Document is valid. Press ok to download.");
app.zipUploadToXml(validator);
}
else {
console.log("Identified invalid upload document");
app.listValidationErrors(validator);
}
}
console.log("Postload papaparse");
if (typeof Papa === "undefined") {
const loadScript = document.createElement("script");
loadScript.setAttribute("src", "assets/js/papaparse/papaparse.min.js");
loadScript.addEventListener('load', function() {
// console.log("Post-loaded OpenLayers");
handleValidation();
}, {passive: true, once: true});
document.body.appendChild(loadScript);
}
else {
console.log("Identified invalid upload document");
app.listValidationErrors(validator);
handleValidation();
}
};