Compare commits
No commits in common. "20557ccf58e4d473c287691030648e98a6d8483f" and "9faa6ea31adb37aa631a9ecacbab25433ef856fd" have entirely different histories.
20557ccf58
...
9faa6ea31a
15
index.html
15
index.html
@ -31,5 +31,20 @@
|
|||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
<script src="dist/bundle.js"></script>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,35 +1,10 @@
|
|||||||
import { CSVParseParam } from '../node_modules/csvtojson/v2/Parameters';
|
|
||||||
import { isJunk } from './utils';
|
import { isJunk } from './utils';
|
||||||
|
|
||||||
|
const csvBOM = '/tests/bom.csv';
|
||||||
const csv = require('csvtojson');
|
const csv = require('csvtojson');
|
||||||
|
|
||||||
//CSV Config for EAGLE BOM
|
export function csvToJSON(csvBOM: string): void {
|
||||||
const csvConfig = {
|
csv()
|
||||||
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)
|
.fromString(csvBOM)
|
||||||
.then((jsonObj: object) => {
|
.then((jsonObj: object) => {
|
||||||
console.log(jsonObj);
|
console.log(jsonObj);
|
||||||
|
13
src/index.ts
13
src/index.ts
@ -1,25 +1,18 @@
|
|||||||
//Modules
|
// Modules
|
||||||
import { csvToJSON } from './csvToJSON';
|
import { csvToJSON } from './csvToJSON';
|
||||||
|
|
||||||
//DOM elements
|
|
||||||
const input = document.getElementById('csvInput') as HTMLInputElement;
|
const input = document.getElementById('csvInput') as HTMLInputElement;
|
||||||
const csvTextOutput = document.getElementById('csvText') as HTMLPreElement;
|
const csvText = document.getElementById('csvText') as HTMLPreElement;
|
||||||
let rawCSV: string;
|
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
function handleUpload(event: Event) {
|
function handleUpload(event: Event) {
|
||||||
const file = (event.target as HTMLInputElement).files![0];
|
const file = (event.target as HTMLInputElement).files![0];
|
||||||
if (file) {
|
if (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;
|
const content = (e.target as FileReader).result;
|
||||||
if (content === null) throw new Error('CSV Cannot be null.');
|
if (content === null) throw new Error('CSV Cannot be null.');
|
||||||
const csvString = content.toString();
|
csvText.innerText = content.toString();
|
||||||
//Display the CSV contents
|
|
||||||
csvTextOutput.innerText = csvString;
|
|
||||||
//Get JSON object from the file
|
|
||||||
console.log();
|
|
||||||
};
|
};
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user