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