Refactor and new part interface

This commit is contained in:
Nick Playfair 2025-06-10 13:57:07 +01:00
parent 0790309216
commit c9fded6aec
4 changed files with 88 additions and 61 deletions

View File

@ -1,44 +1,7 @@
// Modules // Modules
// import csv from 'csvtojson'; // import csv from 'csvtojson';
// import { Converter } from 'csvtojson/v2/Converter'; // import { Converter } from 'csvtojson/v2/Converter';
import { rejectedParts } from './rejectedParts'; import { isJunk, getPartType } from './utils';
// Return false if the Part value of the object passed in is in the list to remove
function isJunk(element: part) {
return !rejectedParts.includes(element.Part);
}
function getPartType(component: part) {
if (component.Part.match(/^C\d/) != null) {
return 'C';
} else if (component.Part.match(/^R\d/) != null) {
return 'R';
}
return 'Q';
}
function getJSONParts(allParts: part[]) {
const jsonParts: structuredParts = {
C: {},
R: {},
Q: {},
};
allParts.map((partEntry: part) => {
// Sort parts into capacitors, resistors and others
switch (getPartType(partEntry)) {
case 'C':
jsonParts.C[partEntry.Part] = partEntry.Value;
break;
case 'R':
jsonParts.R[partEntry.Part] = partEntry.Value;
break;
default:
jsonParts.Q[partEntry.Part] = partEntry.Value;
}
});
return jsonParts;
}
/* TODO /* TODO
// Format the HTML nicely and output to a pre code block // Format the HTML nicely and output to a pre code block

18
src/parts.d.ts vendored
View File

@ -1,5 +1,15 @@
enum PartType {
R,
C,
D,
Q,
IC,
COMPONENT,
}
declare interface part { declare interface part {
Part: string; Part: string;
PartType: PartType;
Value: string; Value: string;
} }
@ -10,7 +20,13 @@ declare interface structuredParts {
R: { R: {
[key: string]: string; [key: string]: string;
}; };
Q: { Q?: {
[key: string]: string;
};
IC?: {
[key: string]: string;
};
COMPONENT?: {
[key: string]: string; [key: string]: string;
}; };
} }

View File

@ -1,22 +0,0 @@
// Which components should we remove from the BOM?
export const rejectedParts = [
'TP1',
'TP2',
'TP3',
'G',
'U$1',
'S1',
'J1',
'J2',
'JP1',
'JP2',
'V',
'I',
'O',
'T1',
'T2',
'T3',
'INPUT',
'IN',
'OUT',
];

70
src/utils.ts Normal file
View File

@ -0,0 +1,70 @@
export function isJunk(element: part): boolean {
// Returns true if element is in the rejected list
return rejectedParts.includes(element.Part);
}
export function getPartType(component: part) {
if (component.Part.match(/^C\d/) != null) {
return 'C';
} else if (component.Part.match(/^R\d/) != null) {
return 'R';
} else if (component.Part.match(/^Q\d/) != null) {
return 'Q';
} else if (component.Part.match(/^IC\d/) != null) {
return 'IC';
}
return 'COMPONENT';
}
// TODO
/*
function getJSONParts(allParts: part[]) {
const jsonParts: structuredParts = {
C: {},
R: {},
};
allParts.map((partEntry: part) => {
// Sort parts into capacitors, resistors and others
switch (getPartType(partEntry)) {
case 'C':
jsonParts.C[partEntry.Part] = partEntry.Value;
break;
case 'R':
jsonParts.R[partEntry.Part] = partEntry.Value;
break;
case 'Q':
jsonParts.Q?[partEntry.Part] = partEntry.Value;
break;
case 'IC':
jsonParts.IC?[partEntry.Part] = partEntry.Value;
break;
default:
jsonParts.COMPONENT?[partEntry.Part] = partEntry.Value;
}
});
return jsonParts;
}
*/
const rejectedParts = [
'TP1',
'TP2',
'TP3',
'G',
'U$1',
'S1',
'J1',
'J2',
'JP1',
'JP2',
'V',
'I',
'O',
'T1',
'T2',
'T3',
'INPUT',
'IN',
'OUT',
];