Skip to content

Commit

Permalink
Merge pull request #3 from tscircuit/mixed-supply
Browse files Browse the repository at this point in the history
support for mixed-supplier bom configuration
  • Loading branch information
seveibar authored Oct 15, 2024
2 parents 214f690 + ee7c8e8 commit 0d8b97b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,37 @@ This package provides two main functions: `convertCircuitJsonToBomRows` and `con
### Converting Circuit JSON to BOM Rows

```typescript
import { convertCircuitJsonToBomRows } from 'circuit-json-to-bom-csv';
import type { AnyCircuitElement } from 'circuit-json';
import { convertCircuitJsonToBomRows } from "circuit-json-to-bom-csv"
import type { AnyCircuitElement } from "circuit-json"

const circuitJson: AnyCircuitElement[] = [
// Your circuit JSON data here
];
]

const bomRows = await convertCircuitJsonToBomRows({ circuitJson });
console.log(bomRows);
const bomRows = await convertCircuitJsonToBomRows({ circuitJson })
console.log(bomRows)
```

### Converting BOM Rows to CSV

```typescript
import { convertBomRowsToCsv } from 'circuit-json-to-bom-csv';
import { convertBomRowsToCsv } from "circuit-json-to-bom-csv"

const bomRows = [
{
designator: 'R1',
comment: '1k',
value: '1k',
footprint: '0805',
designator: "R1",
comment: "1k",
value: "1k",
footprint: "0805",
supplier_part_number_columns: {
'JLCPCB Part#': 'C17513',
"JLCPCB Part #": "C17513",
},
},
// More BOM rows...
];
]

const csv = convertBomRowsToCsv(bomRows);
console.log(csv);
const csv = convertBomRowsToCsv(bomRows)
console.log(csv)
```

## API Reference
Expand Down
4 changes: 2 additions & 2 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ describe("convertBomRowsToCsv", () => {
value: "1k",
footprint: "0805",
supplier_part_number_columns: {
"JLCPCB Part#": "C17513",
"JLCPCB Part #": "C17513",
},
},
]

const csv = convertBomRowsToCsv(bomRows)

expect(csv).toBe(
"Designator,Comment,Value,Footprint,JLCPCB Part#\r\nR1,1k,1k,0805,C17513",
"Designator,Comment,Value,Footprint,JLCPCB Part #\r\nR1,1k,1k,0805,C17513",
)
})
})
22 changes: 18 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { formatSI } from "format-si-prefix"

import Papa from "papaparse"

type SupplierPartNumberColumn = "JLCPCB Part#"
type SupplierPartNumberColumn = "JLCPCB Part #"

interface BomRow {
designator: string
Expand Down Expand Up @@ -41,7 +41,7 @@ interface ResolvedPart {
}

// HEADERS FROM DIFFERENT bom.csv FILES
// Comment Designator Footprint "JLCPCB Part#(optional)"
// Comment Designator Footprint "JLCPCB Part #(optional)"
// Designator Value Footprint Populate MPN Manufacturer MPN Manufacturer MPN Manufacturer MPN Manufacturer MPN Manufacturer

export const convertCircuitJsonToBomRows = async ({
Expand Down Expand Up @@ -103,7 +103,7 @@ function convertSupplierPartNumbersIntoColumns(
> = {}

if (supplier_part_numbers?.jlcpcb) {
supplier_part_number_columns["JLCPCB Part#"] =
supplier_part_number_columns["JLCPCB Part #"] =
supplier_part_numbers.jlcpcb[0]
}

Expand Down Expand Up @@ -135,5 +135,19 @@ export const convertBomRowsToCsv = (bom_rows: BomRow[]): string => {
}
})

return Papa.unparse(csv_data)
const columnHeaders: string[] = [
"Designator",
"Comment",
"Value",
"Footprint",
]
for (const row of csv_data) {
for (const key in row) {
if (!columnHeaders.includes(key)) {
columnHeaders.push(key)
}
}
}

return Papa.unparse(csv_data, { columns: columnHeaders })
}
11 changes: 6 additions & 5 deletions tests/get-bom-for-nine-key-keyboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ test("get-bom-for-nine-key-keyboard", async () => {
expect(key?.comment).toBe("")
expect(key?.value).toBe("")
expect(key?.supplier_part_number_columns).toBeDefined()
expect(key?.supplier_part_number_columns?.["JLCPCB Part#"]).toBe("C5184526")
expect(key?.supplier_part_number_columns?.["JLCPCB Part #"]).toBe("C5184526")

// Check a diode
const diode = bomRows.find((row) => row.designator === "D1")
expect(diode).toBeDefined()
expect(diode?.comment).toBe("")
expect(diode?.value).toBe("")
expect(diode?.supplier_part_number_columns).toBeDefined()
expect(diode?.supplier_part_number_columns?.["JLCPCB Part#"]).toBe("C57759")
expect(diode?.supplier_part_number_columns?.["JLCPCB Part #"]).toBe("C57759")

// Convert to CSV
const csv = convertBomRowsToCsv(bomRows)
expect(csv).toContain("Designator,Comment,Value,Footprint")
expect(csv).toContain("U1,,,")
expect(csv).toContain("K1,,,")
expect(csv).toContain("Designator,Comment,Value,Footprint,JLCPCB Part #")
expect(csv).toContain("U1,,,,")
expect(csv).toContain("K1,,,,C5184526")
expect(csv).toContain("D1,,,,C57759")
})

0 comments on commit 0d8b97b

Please sign in to comment.