Compare commits

..

2 Commits

Author SHA1 Message Date
Nick Playfair
20557ccf58 Remove extraneous quotes from CSV so it doesn't break the JSON 2025-06-10 18:06:32 +01:00
Nick Playfair
bf3dcc1b04 Reformat 2025-06-10 16:30:34 +01:00
3 changed files with 39 additions and 22 deletions

View File

@ -31,20 +31,5 @@
</section>
</main>
<script src="dist/bundle.js"></script>
<!-- <script>
document
.getElementById('csvInput')
.addEventListener('change', function (event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target.result;
document.getElementById('csvText').innerText = content;
};
reader.readAsText(file);
}
});
</script> -->
</body>
</html>

View File

@ -1,10 +1,35 @@
import { CSVParseParam } from '../node_modules/csvtojson/v2/Parameters';
import { isJunk } from './utils';
const csvBOM = '/tests/bom.csv';
const csv = require('csvtojson');
export function csvToJSON(csvBOM: string): void {
csv()
//CSV Config for EAGLE BOM
const csvConfig = {
delimiter: `;`,
// quote: 'off',
ignoreEmpty: true,
includeColumns: /(Part|Value)/,
};
export async function csvToJSON(csvBOM: string) {
// csv()
// .fromString(csvBOM)
// .then((jsonObj: object) => {
// console.log(jsonObj);
// });
const obj: object = await csv({ csvConfig })
.preRawData((csvRawData: string) => {
return new Promise((resolve, reject) => {
var newData = csvRawData.replace('"', '');
resolve(newData);
});
})
.fromString(csvBOM)
.then((jsonObj: object) => {
return jsonObj;
});
}
export function simpleCsvToJSON(csvBOM: string) {
csv({ csvConfig })
.fromString(csvBOM)
.then((jsonObj: object) => {
console.log(jsonObj);

View File

@ -1,18 +1,25 @@
//Modules
import { csvToJSON } from './csvToJSON';
//DOM elements
const input = document.getElementById('csvInput') as HTMLInputElement;
const csvText = document.getElementById('csvText') as HTMLPreElement;
const csvTextOutput = document.getElementById('csvText') as HTMLPreElement;
let rawCSV: string;
//Functions
function handleUpload(event: Event) {
const file = (event.target as HTMLInputElement).files![0];
if (file) {
//Read in from the 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();
const csvString = content.toString();
//Display the CSV contents
csvTextOutput.innerText = csvString;
//Get JSON object from the file
console.log();
};
reader.readAsText(file);
}