From 9faa6ea31adb37aa631a9ecacbab25433ef856fd Mon Sep 17 00:00:00 2001 From: Nick Playfair <842413+nplayfair@users.noreply.github.com> Date: Tue, 10 Jun 2025 15:44:48 +0100 Subject: [PATCH] Move event listener to typescript file --- index.html | 4 ++-- src/csvToJSON.ts | 12 ++++++++++++ src/index.ts | 26 +++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/csvToJSON.ts diff --git a/index.html b/index.html index f245ec3..305603a 100644 --- a/index.html +++ b/index.html @@ -31,7 +31,7 @@ - --> diff --git a/src/csvToJSON.ts b/src/csvToJSON.ts new file mode 100644 index 0000000..59bcc8e --- /dev/null +++ b/src/csvToJSON.ts @@ -0,0 +1,12 @@ +import { isJunk } from './utils'; + +const csvBOM = '/tests/bom.csv'; +const csv = require('csvtojson'); + +export function csvToJSON(csvBOM: string): void { + csv() + .fromString(csvBOM) + .then((jsonObj: object) => { + console.log(jsonObj); + }); +} diff --git a/src/index.ts b/src/index.ts index 6b730f2..6ff8ebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,27 @@ // Modules -// import csv from 'csvtojson'; -// import { Converter } from 'csvtojson/v2/Converter'; -import { isJunk, getPartType } from './utils'; +import { csvToJSON } from './csvToJSON'; + +const input = document.getElementById('csvInput') as HTMLInputElement; +const csvText = document.getElementById('csvText') as HTMLPreElement; + +//Functions +function handleUpload(event: Event) { + const file = (event.target as HTMLInputElement).files![0]; + if (file) { + const reader = new FileReader(); + reader.onload = function (e) { + const content = (e.target as FileReader).result; + if (content === null) throw new Error('CSV Cannot be null.'); + csvText.innerText = content.toString(); + }; + reader.readAsText(file); + } +} + +//Add event listener +input.addEventListener('change', handleUpload); + +// csvToJSON(); /* TODO // Format the HTML nicely and output to a pre code block