From c9fded6aec2ffa3f510443a6c1d6bdcc323fe711 Mon Sep 17 00:00:00 2001 From: Nick Playfair <842413+nplayfair@users.noreply.github.com> Date: Tue, 10 Jun 2025 13:57:07 +0100 Subject: [PATCH] Refactor and new part interface --- src/index.ts | 39 +----------------------- src/parts.d.ts | 18 +++++++++++- src/rejectedParts.ts | 22 -------------- src/utils.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 61 deletions(-) delete mode 100644 src/rejectedParts.ts create mode 100644 src/utils.ts diff --git a/src/index.ts b/src/index.ts index c787b51..6b730f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,44 +1,7 @@ // Modules // import csv from 'csvtojson'; // import { Converter } from 'csvtojson/v2/Converter'; -import { rejectedParts } from './rejectedParts'; - -// 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; -} +import { isJunk, getPartType } from './utils'; /* TODO // Format the HTML nicely and output to a pre code block diff --git a/src/parts.d.ts b/src/parts.d.ts index 991a03c..bc821ad 100644 --- a/src/parts.d.ts +++ b/src/parts.d.ts @@ -1,5 +1,15 @@ +enum PartType { + R, + C, + D, + Q, + IC, + COMPONENT, +} + declare interface part { Part: string; + PartType: PartType; Value: string; } @@ -10,7 +20,13 @@ declare interface structuredParts { R: { [key: string]: string; }; - Q: { + Q?: { + [key: string]: string; + }; + IC?: { + [key: string]: string; + }; + COMPONENT?: { [key: string]: string; }; } diff --git a/src/rejectedParts.ts b/src/rejectedParts.ts deleted file mode 100644 index be33092..0000000 --- a/src/rejectedParts.ts +++ /dev/null @@ -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', -]; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..55c753f --- /dev/null +++ b/src/utils.ts @@ -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', +];