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