Skip to content

Commit

Permalink
Merge pull request #8 from tscircuit/up-method
Browse files Browse the repository at this point in the history
add update method
  • Loading branch information
seveibar authored Aug 20, 2024
2 parents 4150732 + b7b0bff commit 5ed65e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/su.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type SoupOps<
insert: (
elm: Omit<Extract<T, { type: K }>, "type" | `${K}_id`>,
) => Extract<T, { type: K }>
update: (
id: string,
newProps: Partial<Extract<T, { type: K }>>,
) => Extract<T, { type: K }>
delete: (id: string) => void
list: (where?: any) => Extract<T, { type: K }>[]
}
Expand Down Expand Up @@ -128,6 +132,14 @@ export const su: GetSoupUtilFn = ((soup: AnySoupElement[]) => {
if (!elm) return
soup.splice(soup.indexOf(elm), 1)
},
update: (id: string, newProps: any) => {
const elm = soup.find(
(e) => (e as any)[`${component_type}_id`] === id,
)
if (!elm) return
Object.assign(elm, newProps)
return elm
},
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
Expand Down
28 changes: 28 additions & 0 deletions tests/update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { AnySoupElement } from "@tscircuit/soup"
import { su } from "../index"
import test from "ava"

test("update", (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",
},
]

const updatedPort = su(soup).source_port.update("source_port_0", {
name: "right",
})

t.is(updatedPort?.name, "right")
})

0 comments on commit 5ed65e6

Please sign in to comment.