Rearrange script

This commit is contained in:
Nick Playfair 2021-02-22 22:00:36 +00:00
parent babc178e84
commit 73b4deec6b

View File

@ -1,85 +1,72 @@
const beautify = require('js-beautify').html // Modules
const csv = require('csvtojson') const pretty = require('pretty');
const csvPicker = document.getElementById('csvFile') const csv = require('csvtojson');
csvPicker.addEventListener("change", handleFiles, false);
function handleFiles() {
const csvFilePath = this.files[0]
let reader = new FileReader()
reader.readAsText(csvFilePath)
reader.onload = () => makeTable(reader.result)
}
// Configuration
// Which components should we remove from the BOM? // Which components should we remove from the BOM?
const rejectedParts = [ const rejectedParts = ['TP1', 'TP2', 'TP3', 'G', 'U$1', 'S1', 'J1', 'J2', 'INPUT'];
'TP1',
'TP2',
'TP3',
'G',
'U$1',
'J1',
'J2',
'INPUT'
]
function generateTableHead(table, data) {
let thead = table.createTHead();
let row = thead.insertRow();
// Populate Header row
for (let key of data) {
let th = document.createElement("th");
let text = document. createTextNode(key);
th.appendChild(text);
row.appendChild(th);
}
}
function generateTableBody (table, data) {
for (let element of data) {
let row = table.insertRow();
for (key in element) {
let cell = row.insertCell();
let text = document.createTextNode(element[key]);
cell.appendChild(text);
}
}
}
// Return false if the Part value of the object passed in is in the list to remove // Return false if the Part value of the object passed in is in the list to remove
function isJunk(element) { function isJunk(element) {
return !rejectedParts.includes(element.Part); return !rejectedParts.includes(element.Part);
} }
function makeTable(csvString) { function generateTableHead(table, data) {
csv({ const thead = table.createTHead();
delimiter: ";", const row = thead.insertRow();
includeColumns: /(Part|Value)/, // Populate Header row
ignoreEmpty: true for (const key of data) {
}) const th = document.createElement('th');
.fromString(csvString) const text = document.createTextNode(key);
.then(jsonObj => { th.appendChild(text);
// Create array containing only relevant parts row.appendChild(th);
let parts = jsonObj.filter(isJunk) }
// console.log(parts) }
let table = document.querySelector("table")
let headerData = Object.keys(parts[0]) function generateTableBody(table, data) {
generateTableBody(table, parts) for (const element of data) {
generateTableHead(table, headerData) const row = table.insertRow();
displayMarkup() for (const key in element) {
}); const cell = row.insertCell();
const text = document.createTextNode(element[key]);
cell.appendChild(text);
}
}
} }
// Format the HTML nicely and output to a pre code block // Format the HTML nicely and output to a pre code block
function displayMarkup() { function displayMarkup() {
let tableCode = document.querySelector("table").outerHTML const tableCode = document.querySelector('table').outerHTML;
let markup = document.getElementById("markup") const markup = document.getElementById('markup');
beautifulCode = beautify(tableCode, { const beautifulCode = pretty(tableCode);
indent_size: 2, markup.innerText = beautifulCode;
indent_char: ' ',
indent_with_tabs: false,
wrap_line_length: 40
})
markup.innerText = beautifulCode
} }
function makeTable(csvString) {
csv({
delimiter: ';',
includeColumns: /(Part|Value)/,
ignoreEmpty: true,
})
.fromString(csvString)
.then((jsonObj) => {
// Create array containing only relevant parts
const parts = jsonObj.filter(isJunk);
// console.log(parts)
const table = document.querySelector('table');
const headerData = Object.keys(parts[0]);
generateTableBody(table, parts);
generateTableHead(table, headerData);
displayMarkup();
});
}
function handleFiles() {
const csvFilePath = this.files[0];
const reader = new FileReader();
reader.readAsText(csvFilePath);
reader.onload = () => makeTable(reader.result);
}
const csvPicker = document.getElementById('csvFile');
csvPicker.addEventListener('change', handleFiles, false);