Separate out handler functions

This commit is contained in:
Nick Playfair 2025-06-11 13:41:32 +01:00
parent 5378da4fe2
commit 29ef5e92bf

View File

@ -1,5 +1,5 @@
//Modules //Modules
import { csvToJSON } from './csvToJSON'; import { getBOM } from './csvToJSON';
//DOM elements //DOM elements
const input = document.getElementById('csvInput') as HTMLInputElement; const input = document.getElementById('csvInput') as HTMLInputElement;
@ -13,18 +13,27 @@ function handleUpload(event: Event) {
//Read in from the file //Read in from the file
const reader = new FileReader(); const reader = new FileReader();
reader.onload = function (e) { reader.onload = function (e) {
const content = (e.target as FileReader).result; processCSV(e);
if (content === null) throw new Error('CSV Cannot be null.');
const csvString = content.toString();
//Display the CSV contents
csvTextOutput.innerText = csvString;
//Get JSON object from the file
console.log();
}; };
reader.readAsText(file); reader.readAsText(file);
} }
} }
function processCSV(fileReader: Event) {
const content = (fileReader.target as FileReader).result;
if (content === null) throw new Error('CSV Cannot be null.');
const csvString = content.toString();
//Display the CSV contents
csvTextOutput.innerText = csvString;
const bomJSON = createJSONbom(csvString);
}
//TODO
async function createJSONbom(csvString: string) {
const bomJSON = await getBOM(csvString);
console.log(bomJSON);
}
//Add event listener //Add event listener
input.addEventListener('change', handleUpload); input.addEventListener('change', handleUpload);