-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathswitch.ts
More file actions
92 lines (85 loc) · 2.69 KB
/
Copy pathswitch.ts
File metadata and controls
92 lines (85 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { ms, frequency } from "circuit-json"
import {
type CommonComponentProps,
commonComponentProps,
} from "lib/common/layout"
import { connectionTarget } from "lib/common/connectionsProp"
import type { SchematicPinLabel } from "lib/common/schematicPinLabel"
import {
noConnectProp,
type PinLabelsProp,
pinLabelsProp,
} from "lib/components/chip"
import type { Connections } from "lib/utility-types/connections-and-selectors"
import { expectTypesMatch } from "lib/typecheck"
import { z } from "zod"
export interface SwitchProps extends CommonComponentProps {
type?: "spst" | "spdt" | "dpst" | "dpdt"
pinLabels?: PinLabelsProp<SchematicPinLabel>
/**
* Pin names that should not be connected to anything. Marks the
* corresponding source ports with do_not_connect so downstream DRC checks
* skip them (see tscircuit/tscircuit#4443).
*/
noConnect?: readonly SchematicPinLabel[] | SchematicPinLabel[]
isNormallyClosed?: boolean
spdt?: boolean
spst?: boolean
dpst?: boolean
dpdt?: boolean
simSwitchFrequency?: number | string
simCloseAt?: number | string
simOpenAt?: number | string
simStartClosed?: boolean
simStartOpen?: boolean
connections?: Connections<string>
}
export const switchProps = commonComponentProps
.extend({
type: z.enum(["spst", "spdt", "dpst", "dpdt"]).optional(),
isNormallyClosed: z.boolean().optional().default(false),
spst: z.boolean().optional(),
spdt: z.boolean().optional(),
dpst: z.boolean().optional(),
dpdt: z.boolean().optional(),
pinLabels: pinLabelsProp.optional(),
noConnect: noConnectProp.optional(),
simSwitchFrequency: frequency.optional(),
simCloseAt: ms.optional(),
simOpenAt: ms.optional(),
simStartClosed: z.boolean().optional(),
simStartOpen: z.boolean().optional(),
connections: z
.custom<Connections<string>>()
.pipe(z.record(z.string(), connectionTarget))
.optional(),
})
.transform((props) => {
const updatedProps: SwitchProps = { ...props }
if (updatedProps.dpdt) {
updatedProps.type = "dpdt"
} else if (updatedProps.spst) {
updatedProps.type = "spst"
} else if (updatedProps.spdt) {
updatedProps.type = "spdt"
} else if (updatedProps.dpst) {
updatedProps.type = "dpst"
}
switch (updatedProps.type) {
case "spdt":
updatedProps.spdt = true
break
case "spst":
updatedProps.spst = true
break
case "dpst":
updatedProps.dpst = true
break
case "dpdt":
updatedProps.dpdt = true
break
}
return updatedProps
})
export type InferredSwitchProps = z.infer<typeof switchProps>
expectTypesMatch<SwitchProps, InferredSwitchProps>(true)