Skip to content

Commit

Permalink
add select
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed May 6, 2024
1 parent 0b6cfd3 commit 2b5c2a9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
54 changes: 53 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { AnySoupElement, AnySoupElementInput } from "@tscircuit/soup"
import type {
AnySoupElement,
AnySoupElementInput,
SourceComponentBase,
SourcePort,
} from "@tscircuit/soup"

type SoupOps<
K extends AnySoupElement["type"],
T extends AnySoupElement | AnySoupElementInput
> = {
get: (id: string) => Extract<T, { type: K }> | null
select: (selector: string) => Extract<T, { type: K }> | null
getWhere: (where: any) => Extract<T, { type: K }> | null
getUsing: (using: {
[key: `${string}_id`]: string
Expand Down Expand Up @@ -70,6 +76,52 @@ export const su: GetSoupUtilObject = ((soup: AnySoupElement[]) => {
keys.every((key) => e[key] === where[key])
)
},
select: (selector: string) => {
// TODO when applySelector is isolated we can use it, until then we
// do a poor man's selector implementation for two common cases
if (component_type === "source_component") {
return soup.find(
(e) =>
e.type === "source_component" &&
e.name === selector.replace(/\./g, "")
)
} else if (
component_type === "pcb_port" ||
component_type === "source_port" ||
component_type === "schematic_port"
) {
const [component_name, port_selector] = selector.split(/[ \>]/)
const source_component = soup.find(
(e) =>
e.type === "source_component" && e.name === component_name
) as SourceComponentBase
if (!source_component) return null
const source_port = soup.find(
(e) =>
e.type === "source_port" &&
e.source_component_id ===
source_component.source_component_id &&
(e.name === port_selector ||
(e.port_hints ?? []).includes(port_selector!))
) as SourcePort
if (!source_port) return null
if (component_type === "source_port") return source_port

if (component_type === "pcb_port") {
return soup.find(
(e) =>
e.type === "pcb_port" &&
e.source_port_id === source_port.source_port_id
)
} else if (component_type === "schematic_port") {
return soup.find(
(e) =>
e.type === "schematic_port" &&
e.source_port_id === source_port.source_port_id
)
}
}
},
}
},
}
Expand Down
34 changes: 34 additions & 0 deletions tests/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { AnySoupElement } from "@tscircuit/soup"
import su from "../index"
import test from "ava"

test("select", (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",
},
{
type: "pcb_port",
pcb_port_id: "pcb_port_0",
source_port_id: "source_port_0",
layers: ["top"],
pcb_component_id: "pcb_component_simple_resistor_0",
x: 0,
y: 0,
},
]

const pp = su(soup).pcb_port.select(".R1 > .left")
t.is(pp?.pcb_port_id, "pcb_port_0")
})

0 comments on commit 2b5c2a9

Please sign in to comment.