Refactor JSON output
This commit is contained in:
parent
1adb4d55fd
commit
eb2af6ecad
@ -22,17 +22,6 @@ function isJunk(element) {
|
|||||||
return !rejectedParts.includes(element.Part);
|
return !rejectedParts.includes(element.Part);
|
||||||
}
|
}
|
||||||
|
|
||||||
function partFilter(parts, category) {
|
|
||||||
const regex = {
|
|
||||||
C: /^C\d/,
|
|
||||||
R: /^R\d/,
|
|
||||||
Q: /^((?!(R\d|C\d)).)*$/,
|
|
||||||
};
|
|
||||||
|
|
||||||
const filteredParts = parts.filter((part) => part.Part.match(regex[category]));
|
|
||||||
return filteredParts;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPartType(partName) {
|
function getPartType(partName) {
|
||||||
// console.log(partName.Part);
|
// console.log(partName.Part);
|
||||||
if (partName.Part.match(/^C\d/) != null) {
|
if (partName.Part.match(/^C\d/) != null) {
|
||||||
@ -114,7 +103,6 @@ function makeTable(csvString) {
|
|||||||
.then((jsonObj) => {
|
.then((jsonObj) => {
|
||||||
// Create array containing only relevant parts
|
// Create array containing only relevant parts
|
||||||
const parts = jsonObj.filter(isJunk);
|
const parts = jsonObj.filter(isJunk);
|
||||||
// console.log(parts)
|
|
||||||
const table = document.querySelector('table');
|
const table = document.querySelector('table');
|
||||||
const headerData = Object.keys(parts[0]);
|
const headerData = Object.keys(parts[0]);
|
||||||
generateTableBody(table, parts);
|
generateTableBody(table, parts);
|
||||||
@ -136,41 +124,13 @@ function makeJSON(csvString) {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
const object = res.filter(isJunk);
|
const object = res.filter(isJunk);
|
||||||
|
|
||||||
// Components
|
const parts = getJSONParts(object)
|
||||||
const C = {};
|
|
||||||
const R = {};
|
|
||||||
const Q = {};
|
|
||||||
|
|
||||||
// caps
|
document.getElementById('jsonObject').innerText = JSON.stringify(
|
||||||
let tempParts = partFilter(object, 'C');
|
parts,
|
||||||
tempParts.map((part) => {
|
null,
|
||||||
C[part.Part] = part.Value;
|
2,
|
||||||
});
|
);
|
||||||
// resistors
|
|
||||||
tempParts = partFilter(object, 'R');
|
|
||||||
tempParts.map((part) => {
|
|
||||||
R[part.Part] = part.Value;
|
|
||||||
});
|
|
||||||
// others
|
|
||||||
tempParts = partFilter(object, 'Q');
|
|
||||||
tempParts.map((part) => {
|
|
||||||
Q[part.Part] = part.Value;
|
|
||||||
});
|
|
||||||
|
|
||||||
const parts = {
|
|
||||||
C,
|
|
||||||
R,
|
|
||||||
Q,
|
|
||||||
};
|
|
||||||
|
|
||||||
// document.getElementById('jsonObject').innerText = JSON.stringify(
|
|
||||||
// parts,
|
|
||||||
// null,
|
|
||||||
// 2,
|
|
||||||
// );
|
|
||||||
// console.log(object);
|
|
||||||
// console.log(`partname test: ${getPartType(object[0])}`);
|
|
||||||
console.log(getJSONParts(object));
|
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@ -183,7 +143,7 @@ function handleFiles() {
|
|||||||
reader.readAsText(csvFilePath);
|
reader.readAsText(csvFilePath);
|
||||||
reader.onload = () => {
|
reader.onload = () => {
|
||||||
clearTable();
|
clearTable();
|
||||||
// makeTable(reader.result);
|
makeTable(reader.result);
|
||||||
makeJSON(reader.result);
|
makeJSON(reader.result);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user