Skip to content

Commit

Permalink
replaced @tscircuit/soup to circuit-json, refactor tests from ava to …
Browse files Browse the repository at this point in the history
…bun:test (#16)

* replaced @tscircuit/soup to circuit-json

and AnySoupElement to AnyCircuitElement

* convert to esm

* now tests with bun

* test script

* moduleResolution to Bundler
  • Loading branch information
ShiboSoftwareDev authored Sep 21, 2024
1 parent f3798eb commit 4d23c8f
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.vscode
dist
.aider*
package-lock.json
Binary file modified bun.lockb
Binary file not shown.
20 changes: 10 additions & 10 deletions lib/apply-selector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as parsel from "parsel-js"
import { convertAbbrToType } from "./convert-abbreviation-to-soup-element-type"
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"

const filterByType = (
elements: AnySoupElement[],
elements: AnyCircuitElement[],
type: string
): AnySoupElement[] => {
): AnyCircuitElement[] => {
type = convertAbbrToType(type)
return elements.filter(
(elm) => ("ftype" in elm && elm.ftype === type) || elm.type === type
Expand All @@ -17,21 +17,21 @@ const filterByType = (
* resistor you can do ".R1 > port.left"
*/
export const applySelector = (
elements: AnySoupElement[],
elements: AnyCircuitElement[],
selectorRaw: string
): AnySoupElement[] => {
): AnyCircuitElement[] => {
const selectorAST = parsel.parse(selectorRaw)
return applySelectorAST(elements, selectorAST!)
}

const doesElmMatchClassName = (elm: AnySoupElement, className: string) =>
const doesElmMatchClassName = (elm: AnyCircuitElement, className: string) =>
("name" in elm && elm.name === className) ||
("port_hints" in elm && elm.port_hints?.includes(className))

export const applySelectorAST = (
elements: AnySoupElement[],
elements: AnyCircuitElement[],
selectorAST: parsel.AST
): AnySoupElement[] => {
): AnyCircuitElement[] => {
switch (selectorAST.type) {
case "complex": {
switch (selectorAST.combinator) {
Expand All @@ -40,7 +40,7 @@ export const applySelectorAST = (
const { left, right } = selectorAST
if (left.type === "class" || left.type === "type") {
// TODO should also check if content matches any element tags
let matchElms: AnySoupElement[]
let matchElms: AnyCircuitElement[]
if (left.type === "class") {
matchElms = elements.filter((elm) =>
doesElmMatchClassName(elm, left.name)
Expand Down Expand Up @@ -89,7 +89,7 @@ export const applySelectorAST = (
)
}
case "type": {
return filterByType(elements, selectorAST.name) as AnySoupElement[]
return filterByType(elements, selectorAST.name) as AnyCircuitElement[]
}
case "class": {
return elements.filter((elm) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/get-bounds-of-pcb-elements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyCircuitElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"

export const getBoundsOfPcbElements = (
elements: AnyCircuitElement[],
Expand Down
6 changes: 3 additions & 3 deletions lib/get-element-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { getElementId } from "./get-element-id"

export const getElementById = (
soup: AnySoupElement[],
soup: AnyCircuitElement[],
id: string,
): AnySoupElement | null => {
): AnyCircuitElement | null => {
return soup.find((elm) => getElementId(elm) === id) ?? null
}
4 changes: 2 additions & 2 deletions lib/get-element-id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"

export const getElementId = (elm: AnySoupElement): string => {
export const getElementId = (elm: AnyCircuitElement): string => {
const type = elm.type
const id = (elm as any)[`${type}_id`]
return id
Expand Down
6 changes: 3 additions & 3 deletions lib/readable-name-functions/get-readable-name-for-element.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { getReadableNameForPcbTrace } from "./get-readable-name-for-pcb-trace"
import { getReadableNameForPcbSmtpad } from "./get-readable-name-for-pcb-smtpad"
import { getReadableNameForPcbPort } from "./get-readable-name-for-pcb-port"
import { getElementById } from "lib/get-element-by-id"
import { getElementId } from "lib/get-element-id"

export const getReadableNameForElement = (
soup: AnySoupElement[],
elm: AnySoupElement | string,
soup: AnyCircuitElement[],
elm: AnyCircuitElement | string,
): string => {
if (typeof elm === "string") {
const elmObj = getElementById(soup, elm)
Expand Down
4 changes: 2 additions & 2 deletions lib/readable-name-functions/get-readable-name-for-pcb-port.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { su } from "../su"

export const getReadableNameForPcbPort = (
soup: AnySoupElement[],
soup: AnyCircuitElement[],
pcb_port_id: string,
): string => {
const pcbPort = su(soup).pcb_port.get(pcb_port_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { su } from "../su"
import { getReadableNameForPcbPort } from "./get-readable-name-for-pcb-port"

export function getReadableNameForPcbSmtpad(
soup: AnySoupElement[],
soup: AnyCircuitElement[],
pcb_smtpad_id: string,
): string {
// Find the pcb_smtpad object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { su } from "../su"

export function getReadableNameForPcbTrace(
soup: AnySoupElement[],
soup: AnyCircuitElement[],
pcb_trace_id: string,
) {
// Find the pcb_trace object
Expand Down
24 changes: 12 additions & 12 deletions lib/su.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
AnySoupElement,
AnySoupElementInput,
AnyCircuitElement,
AnyCircuitElementInput,
SourceComponentBase,
SourcePort,
} from "@tscircuit/soup"
import * as Soup from "@tscircuit/soup"
} from "circuit-json"
import * as Soup from "circuit-json"

type SoupOps<
K extends AnySoupElement["type"],
T extends AnySoupElement | AnySoupElementInput,
K extends AnyCircuitElement["type"],
T extends AnyCircuitElement | AnyCircuitElementInput,
> = {
get: (id: string) => Extract<T, { type: K }> | null
select: (selector: string) => Extract<T, { type: K }> | null
Expand All @@ -28,31 +28,31 @@ type SoupOps<
}

export type SoupUtilObjects = {
[K in AnySoupElement["type"]]: SoupOps<K, AnySoupElement>
[K in AnyCircuitElement["type"]]: SoupOps<K, AnyCircuitElement>
} & {
toArray: () => AnySoupElement[]
toArray: () => AnyCircuitElement[]
}
export type SoupInputUtilObjects = {
[K in AnySoupElementInput["type"]]: SoupOps<K, AnySoupElementInput>
[K in AnyCircuitElementInput["type"]]: SoupOps<K, AnyCircuitElementInput>
}

export type SoupUtilOptions = {
validateInserts?: boolean
}

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

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

export const su: GetSoupUtilFn = ((
soup: AnySoupElement[],
soup: AnyCircuitElement[],
options: SoupUtilOptions = {},
) => {
let internalStore: InternalStore = (soup as any)._internal_store
Expand Down
10 changes: 5 additions & 5 deletions lib/transform-soup-elements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { type Matrix, applyToPoint, decomposeTSR } from "transformation-matrix"
import {
directionToVec,
Expand All @@ -7,7 +7,7 @@ import {
} from "./direction-to-vec"

export const transformSchematicElement = (
elm: AnySoupElement,
elm: AnyCircuitElement,
matrix: Matrix,
) => {
if (elm.type === "schematic_component") {
Expand Down Expand Up @@ -43,13 +43,13 @@ export const transformSchematicElement = (
}

export const transformSchematicElements = (
elms: AnySoupElement[],
elms: AnyCircuitElement[],
matrix: Matrix,
) => {
return elms.map((elm) => transformSchematicElement(elm, matrix))
}

export const transformPCBElement = (elm: AnySoupElement, matrix: Matrix) => {
export const transformPCBElement = (elm: AnyCircuitElement, matrix: Matrix) => {
if (
elm.type === "pcb_plated_hole" ||
elm.type === "pcb_hole" ||
Expand Down Expand Up @@ -106,7 +106,7 @@ export const transformPCBElement = (elm: AnySoupElement, matrix: Matrix) => {
}

export const transformPCBElements = (
elms: AnySoupElement[],
elms: AnyCircuitElement[],
matrix: Matrix,
) => {
const tsr = decomposeTSR(matrix)
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
"files": [
"dist"
],
"main": "dist/index.cjs",
"main": "dist/index.js",
"scripts": {
"test": "ava",
"test": "bun test",
"prepublish": "npm run build",
"build": "tsup-node ./index.ts --dts --sourcemap"
"build": "tsup-node ./index.ts --format esm --dts --sourcemap"
},
"keywords": [],
"author": "",
"license": "ISC",
"peerDependencies": {
"@tscircuit/soup": "*",
"circuit-json": "*",
"transformation-matrix": "*",
"zod": "*"
},
"devDependencies": {
"@biomejs/biome": "^1.9.2",
"@tscircuit/soup": "^0.0.71",
"ava": "^6.1.2",
"circuit-json": "^0.0.78",
"esbuild": "^0.20.2",
"esbuild-register": "^3.5.0",
"transformation-matrix": "^2.16.1",
Expand All @@ -32,6 +31,7 @@
"zod": "^3.23.6"
},
"dependencies": {
"@types/bun": "^1.1.10",
"parsel-js": "^1.1.2"
}
}
10 changes: 5 additions & 5 deletions tests/delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { su } from "../index"
import test from "ava"
import { test, expect } from "bun:test"

test("delete", (t) => {
const soup: AnySoupElement[] = [
test("delete", () => {
const soup: AnyCircuitElement[] = [
{
type: "source_component",
source_component_id: "simple_resistor_0",
Expand All @@ -26,5 +26,5 @@ test("delete", (t) => {
.toArray()
.find((e) => e.type === "source_port")

t.falsy(sp)
expect(sp).toBeFalsy()
})
9 changes: 5 additions & 4 deletions tests/get-bounds-of-pcb-elements.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import test from "ava"
import { getBoundsOfPcbElements } from "../lib/get-bounds-of-pcb-elements"
import type { AnyCircuitElement } from "circuit-json"
import { expect, test } from "bun:test"

test("getBoundsOfPcbElements", (t) => {
const elements: AnySoupElement[] = [
test("getBoundsOfPcbElements", () => {
const elements: AnyCircuitElement[] = [
{
type: "pcb_component",
pcb_component_id: "comp1",
Expand Down Expand Up @@ -35,5 +36,5 @@ test("getBoundsOfPcbElements", (t) => {

const bounds = getBoundsOfPcbElements(elements)

t.deepEqual(bounds, { minX: -5, minY: -5, maxX: 20, maxY: 20 })
expect(bounds).toEqual({ minX: -5, minY: -5, maxX: 20, maxY: 20 })
})
24 changes: 7 additions & 17 deletions tests/get-readable-name-for-element.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import test from "ava"
import { getReadableNameForElement } from "../lib/readable-name-functions/get-readable-name-for-element"
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import { expect, test } from "bun:test"

test("getReadableNameForElement for pcb_port, pcb_smtpad, and pcb_trace", (t) => {
const soup: AnySoupElement[] = [
test("getReadableNameForElement for pcb_port, pcb_smtpad, and pcb_trace", () => {
const soup: AnyCircuitElement[] = [
{
type: "source_component",
source_component_id: "sc1",
Expand Down Expand Up @@ -106,23 +106,13 @@ test("getReadableNameForElement for pcb_port, pcb_smtpad, and pcb_trace", (t) =>
]

// Test pcb_port
t.is(
getReadableNameForElement(soup, "pp1"),
"pcb_port[.R1 > .1]",
"PCB port readable name",
)
expect(getReadableNameForElement(soup, "pp1")).toBe("pcb_port[.R1 > .1]")

// Test pcb_smtpad
t.is(
getReadableNameForElement(soup, "ps1"),
"pcb_port[.R1 > .1]",
"PCB SMT pad readable name",
)
expect(getReadableNameForElement(soup, "ps1")).toBe("pcb_port[.R1 > .1]")

// Test pcb_trace
t.is(
getReadableNameForElement(soup, "pt1"),
expect(getReadableNameForElement(soup, "pt1")).toBe(
"trace[.R1 > port.left, .C1 > port.positive]",
"PCB trace readable name",
)
})
10 changes: 5 additions & 5 deletions tests/get-using.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { AnySoupElementInput } from "@tscircuit/soup"
import type { AnyCircuitElementInput } from "circuit-json"
import { su } from "../index"
import test from "ava"
import { test, expect } from "bun:test"

test("getUsing", (t) => {
const soup: AnySoupElementInput[] = [
test("getUsing", () => {
const soup: AnyCircuitElementInput[] = [
{
type: "source_component",
source_component_id: "simple_resistor_0",
Expand All @@ -23,5 +23,5 @@ test("getUsing", (t) => {
const sc = su.unparsed(soup).source_component.getUsing({
source_port_id: "source_port_0",
})
t.is(sc?.name, "R1")
expect(sc?.name).toBe("R1")
})
Loading

0 comments on commit 4d23c8f

Please sign in to comment.