Skip to content

Commit

Permalink
Merge pull request #59 from tscircuit/simple-battery
Browse files Browse the repository at this point in the history
add simple_battery
  • Loading branch information
seveibar authored Oct 17, 2024
2 parents 3b13f97 + b80de73 commit cc362ae
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions src/any_circuit_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const any_circuit_element = z.union([
src.source_simple_diode,
src.source_simple_resistor,
src.source_simple_power_source,
src.source_simple_battery,
pcb.pcb_component,
pcb.pcb_hole,
pcb.pcb_plated_hole,
Expand Down
2 changes: 2 additions & 0 deletions src/source/any_source_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { source_simple_bug } from "./source_simple_bug"
import { source_simple_chip } from "./source_simple_chip"
import { source_led } from "./source_led"
import { source_simple_power_source } from "./source_simple_power_source"
import { source_simple_battery } from "./source_simple_battery"

export const any_source_component = z.union([
source_simple_resistor,
Expand All @@ -17,6 +18,7 @@ export const any_source_component = z.union([
source_simple_bug,
source_led,
source_simple_power_source,
source_simple_battery,
])

export type AnySourceComponent = z.infer<typeof any_source_component>
1 change: 1 addition & 0 deletions src/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./source_trace"
export * from "./base/source_component_base"
export * from "./source_group"
export * from "./source_net"
export * from "./source_simple_battery"
11 changes: 11 additions & 0 deletions src/source/source_simple_battery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod"
import { source_component_base } from "src/source/base/source_component_base"
import { battery_capacity } from "src/units"

export const source_simple_battery = source_component_base.extend({
ftype: z.literal("simple_battery"),
capacity: battery_capacity,
})

export type SourceSimpleBattery = z.infer<typeof source_simple_battery>
export type SourceSimpleBatteryInput = z.input<typeof source_simple_battery>
16 changes: 16 additions & 0 deletions src/units/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,21 @@ export const rotation = z
return Number.parseFloat(arg)
})

export const battery_capacity = z
.number()
.or(z.string().endsWith("mAh"))
.transform((v) => {
if (typeof v === "string") {
const valString = v.replace("mAh", "")
const num = Number.parseFloat(valString)
if (Number.isNaN(num)) {
throw new Error("Invalid capacity")
}
return num
}
return v
})
.describe("Battery capacity in mAh")

export type InputRotation = number | string
export type Rotation = number

0 comments on commit cc362ae

Please sign in to comment.