Skip to content

Commit

Permalink
Merge pull request #9 from tscircuit/insert-validation
Browse files Browse the repository at this point in the history
allow creating soup util with validation turned on
  • Loading branch information
seveibar authored Aug 24, 2024
2 parents f9a986a + 5fd4316 commit 301718a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
22 changes: 20 additions & 2 deletions lib/su.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
SourceComponentBase,
SourcePort,
} from "@tscircuit/soup"
import * as Soup from "@tscircuit/soup"

type SoupOps<
K extends AnySoupElement["type"],
Expand Down Expand Up @@ -35,15 +36,25 @@ export type SoupInputUtilObjects = {
[K in AnySoupElementInput["type"]]: SoupOps<K, AnySoupElementInput>
}

export type GetSoupUtilFn = ((soup: AnySoupElement[]) => SoupUtilObjects) & {
export type SoupUtilOptions = {
validateInserts?: boolean
}

export type GetSoupUtilFn = ((
soup: AnySoupElement[],
options?: SoupUtilOptions,
) => SoupUtilObjects) & {
unparsed: (soup: AnySoupElementInput[]) => SoupInputUtilObjects
}

interface InternalStore {
counts: Record<string, number>
}

export const su: GetSoupUtilFn = ((soup: AnySoupElement[]) => {
export const su: GetSoupUtilFn = ((
soup: AnySoupElement[],
options: SoupUtilOptions = {},
) => {
let internalStore: InternalStore = (soup as any)._internal_store
if (!internalStore) {
internalStore = {
Expand Down Expand Up @@ -124,6 +135,13 @@ export const su: GetSoupUtilFn = ((soup: AnySoupElement[]) => {
[`${component_type}_id`]: `${component_type}_${index}`,
...elm,
}

if (options.validateInserts) {
const parser =
(Soup as any)[component_type] ?? Soup.any_soup_element
parser.parse(newElm)
}

soup.push(newElm)
return newElm
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"zod": "*"
},
"devDependencies": {
"@tscircuit/soup": "^0.0.39",
"ava": "^6.1.2",
"esbuild": "^0.20.2",
"esbuild-register": "^3.5.0",
Expand All @@ -31,6 +30,7 @@
"zod": "^3.23.6"
},
"dependencies": {
"@tscircuit/soup": "^0.0.39",
"parsel-js": "^1.1.2"
}
}
33 changes: 33 additions & 0 deletions tests/insert-with-validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { AnySoupElement } from "@tscircuit/soup"
import { su } from "../index"
import test from "ava"

test("insert with validation", (t) => {
const soup: AnySoupElement[] = [
{
type: "source_component",
source_component_id: "simple_resistor_0",
name: "R1",
supplier_part_numbers: {},
ftype: "simple_resistor",
resistance: 10_000,
},
{
type: "source_port",
name: "left",
source_port_id: "source_port_0",
source_component_id: "simple_resistor_0",
},
]

t.throws(() => {
su(soup, { validateInserts: true }).pcb_port.insert({
// @ts-expect-error - this is the error, "top" should be in an array
layers: "top",
pcb_component_id: "",
source_port_id: "source_port_0",
x: 0,
y: 0,
})
})
})

0 comments on commit 301718a

Please sign in to comment.