FInish table class implementation
This commit is contained in:
parent
c1d38f4d0c
commit
744e48e812
@ -1,3 +1,4 @@
|
|||||||
|
//Array of part names to omit from the BOM
|
||||||
export const rejectedParts = [
|
export const rejectedParts = [
|
||||||
'TP1',
|
'TP1',
|
||||||
'TP2',
|
'TP2',
|
||||||
@ -19,3 +20,14 @@ export const rejectedParts = [
|
|||||||
'IN',
|
'IN',
|
||||||
'OUT',
|
'OUT',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
//Header titles for the table
|
||||||
|
export const headers: string[] = ['Part', 'Value'];
|
||||||
|
|
||||||
|
//Config object for csv library
|
||||||
|
export const csvConfig = {
|
||||||
|
delimiter: `;`,
|
||||||
|
quote: '"',
|
||||||
|
ignoreEmpty: true,
|
||||||
|
includeColumns: /(Part|Value)/,
|
||||||
|
};
|
||||||
|
@ -1,15 +1,7 @@
|
|||||||
import { CSVParseParam } from '../node_modules/csvtojson/v2/Parameters';
|
|
||||||
import { isJunk } from './utils';
|
import { isJunk } from './utils';
|
||||||
|
import { csvConfig } from './config';
|
||||||
const csv = require('csvtojson');
|
const csv = require('csvtojson');
|
||||||
|
|
||||||
//CSV Config for EAGLE BOM
|
|
||||||
const csvConfig = {
|
|
||||||
delimiter: `;`,
|
|
||||||
quote: '"',
|
|
||||||
ignoreEmpty: true,
|
|
||||||
includeColumns: /(Part|Value)/,
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function getBOM(csvBOM: string) {
|
export async function getBOM(csvBOM: string) {
|
||||||
const rawBOM = await csv(csvConfig).fromString(csvBOM);
|
const rawBOM = await csv(csvConfig).fromString(csvBOM);
|
||||||
const bom = rawBOM.filter((part: part) => !isJunk(part));
|
const bom = rawBOM.filter((part: part) => !isJunk(part));
|
||||||
|
19
src/index.ts
19
src/index.ts
@ -1,16 +1,13 @@
|
|||||||
//Modules
|
//Modules
|
||||||
import { getBOM } from './csvToJSON';
|
import { getBOM } from './csvToJSON';
|
||||||
import {
|
import { PartsTable } from './table';
|
||||||
createTableBody,
|
import { headers } from './config';
|
||||||
createTableHeader,
|
|
||||||
htmlTable,
|
|
||||||
clearTable,
|
|
||||||
} from './utils';
|
|
||||||
|
|
||||||
//DOM elements
|
//DOM elements
|
||||||
const input = document.getElementById('csvInput') as HTMLInputElement;
|
const input = document.getElementById('csvInput') as HTMLInputElement;
|
||||||
const csvTextOutput = document.getElementById('csvText') as HTMLPreElement;
|
const csvTextOutput = document.getElementById('csvText') as HTMLPreElement;
|
||||||
const jsonTextOutput = document.getElementById('partsJSON') as HTMLPreElement;
|
const jsonTextOutput = document.getElementById('partsJSON') as HTMLPreElement;
|
||||||
|
const htmlTable = document.getElementById('partsTable') as HTMLTableElement;
|
||||||
|
|
||||||
let csvBOM: string;
|
let csvBOM: string;
|
||||||
|
|
||||||
@ -37,22 +34,20 @@ function processCSV(fileReader: Event) {
|
|||||||
//JSON Object
|
//JSON Object
|
||||||
printJSONbom(csvString);
|
printJSONbom(csvString);
|
||||||
//Table
|
//Table
|
||||||
clearTable();
|
|
||||||
createTable();
|
createTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
//Print JSON
|
||||||
async function printJSONbom(csvString: string) {
|
async function printJSONbom(csvString: string) {
|
||||||
const bomJSON = await getBOM(csvString);
|
const bomJSON = await getBOM(csvString);
|
||||||
// console.log(bomJSON);
|
|
||||||
jsonTextOutput.innerText = JSON.stringify(bomJSON, null, 2);
|
jsonTextOutput.innerText = JSON.stringify(bomJSON, null, 2);
|
||||||
return bomJSON;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Construct table
|
||||||
async function createTable() {
|
async function createTable() {
|
||||||
const bomJSON = await getBOM(csvBOM);
|
const bomJSON = await getBOM(csvBOM);
|
||||||
createTableHeader();
|
const partsTable = new PartsTable(htmlTable, headers, bomJSON);
|
||||||
createTableBody(htmlTable, bomJSON);
|
partsTable.createTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add event listener
|
//Add event listener
|
||||||
|
@ -38,4 +38,11 @@ export class PartsTable {
|
|||||||
partValue.appendChild(partValText);
|
partValue.appendChild(partValText);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Create full table
|
||||||
|
public createTable(): void {
|
||||||
|
this.clearTable();
|
||||||
|
this.createTableHeader();
|
||||||
|
this.createTableBody();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
39
src/utils.ts
39
src/utils.ts
@ -1,47 +1,10 @@
|
|||||||
import { rejectedParts } from './config';
|
import { rejectedParts, headers } from './config';
|
||||||
|
|
||||||
export function isJunk(element: part): boolean {
|
export function isJunk(element: part): boolean {
|
||||||
// Returns true if element is in the rejected list
|
// Returns true if element is in the rejected list
|
||||||
return rejectedParts.includes(element.Part);
|
return rejectedParts.includes(element.Part);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Table functions
|
|
||||||
export function clearTable() {
|
|
||||||
document.querySelector('table')!.innerHTML = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
export const htmlTable = document.getElementById(
|
|
||||||
'partsTable',
|
|
||||||
) as HTMLTableElement;
|
|
||||||
const headers: string[] = ['Part', 'Value'];
|
|
||||||
|
|
||||||
export function createTableHeader(): void {
|
|
||||||
const tHead = htmlTable.createTHead();
|
|
||||||
const hRow = tHead.insertRow();
|
|
||||||
//Populate headers with text
|
|
||||||
for (const header of headers) {
|
|
||||||
const th = document.createElement('th');
|
|
||||||
const headerText = document.createTextNode(header);
|
|
||||||
th.appendChild(headerText);
|
|
||||||
hRow.appendChild(th);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createTableBody(table: HTMLTableElement, parts: part[]) {
|
|
||||||
parts.map((component) => {
|
|
||||||
//Create a row
|
|
||||||
const tRow = table.insertRow();
|
|
||||||
//Insert part name
|
|
||||||
const partName = tRow.insertCell();
|
|
||||||
const partNameText = document.createTextNode(component.Part);
|
|
||||||
partName.appendChild(partNameText);
|
|
||||||
//Insert part value
|
|
||||||
const partValue = tRow.insertCell();
|
|
||||||
const partValText = document.createTextNode(component.Value);
|
|
||||||
partValue.appendChild(partValText);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user