From ef9269d6a43618996653ec55b3010ceee500c4cb Mon Sep 17 00:00:00 2001 From: Severin Ibarluzea Date: Thu, 17 Oct 2024 09:48:54 -0700 Subject: [PATCH] Battery types, batteryProps, set default unit to mAh (#63) * Battery types, batteryProps, set default unit to mAh * Update lib/components/battery.ts --- lib/components/battery.ts | 36 ++++++++++++++++++++++++++++++++++++ lib/index.ts | 1 + 2 files changed, 37 insertions(+) create mode 100644 lib/components/battery.ts diff --git a/lib/components/battery.ts b/lib/components/battery.ts new file mode 100644 index 0000000..b8b84a9 --- /dev/null +++ b/lib/components/battery.ts @@ -0,0 +1,36 @@ +import { z } from "zod" +import { + commonComponentProps, + lrPins, + lrPolarPins, + type CommonComponentProps, +} from "lib/common/layout" +import { expectTypesMatch } from "lib/typecheck" + +/** @deprecated use battery_capacity from circuit-json when circuit-json is updated */ +const 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 interface BatteryProps extends CommonComponentProps { + capacity?: number | string +} + +export const batteryProps = commonComponentProps.extend({ + capacity: capacity.optional(), +}) +export const batteryPins = lrPolarPins + +expectTypesMatch>(true) diff --git a/lib/index.ts b/lib/index.ts index 8f459ba..4f786f3 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -61,6 +61,7 @@ export * from "./components/solderpaste" export * from "./components/hole" export * from "./components/trace" export * from "./components/footprint" +export * from "./components/battery" export const inductorProps = commonComponentProps.extend({ inductance,