Move event listener to typescript file

This commit is contained in:
Nick Playfair 2025-06-10 15:44:48 +01:00
parent c9fded6aec
commit 9faa6ea31a
3 changed files with 37 additions and 5 deletions

View File

@ -31,7 +31,7 @@
</section>
</main>
<script src="dist/bundle.js"></script>
<script>
<!-- <script>
document
.getElementById('csvInput')
.addEventListener('change', function (event) {
@ -45,6 +45,6 @@
reader.readAsText(file);
}
});
</script>
</script> -->
</body>
</html>

12
src/csvToJSON.ts Normal file
View File

@ -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);
});
}

View File

@ -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