Line data Source code
1 : import type { NodeId } from './nodeId'
2 : import { toNodeId } from './nodeId'
3 :
4 : export type SlotId = string & { readonly __brand: 'SlotId' }
5 : export type SlotIndex = number
6 : export type SlotDirection = 'input' | 'output'
7 :
8 152895 : function toSlotId(value: string): SlotId {
9 152895 : return value as SlotId
10 : }
11 :
12 36666 : export function slotId(
13 : nodeId: NodeId,
14 : direction: SlotDirection,
15 : index: SlotIndex
16 : ): SlotId {
17 : const type = direction === 'input' ? 'in' : 'out'
18 36666 : return toSlotId(`${String(nodeId)}-${type}-${index}`)
19 : }
20 :
21 116229 : export function parseSlotId(value: string): {
22 : key: SlotId
23 : nodeId: NodeId
24 : direction: SlotDirection
25 : index: SlotIndex
26 : } | null {
27 116229 : const match = /^(.*)-(in|out)-(\d+)$/.exec(value)
28 116229 : if (!match?.[1] || !match[2] || !match[3]) return null
29 116229 : const index = Number(match[3])
30 116229 : if (!Number.isSafeInteger(index)) return null
31 :
32 116229 : return {
33 : key: toSlotId(value),
34 : nodeId: toNodeId(match[1]),
35 : direction: match[2] === 'in' ? 'input' : 'output',
36 : index
37 : }
38 : }
|