|
| 1 | +import { customAlphabet } from "nanoid"; |
| 2 | +import type { SwitchBranch } from "./types"; |
| 3 | + |
| 4 | +const nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 8); |
| 5 | + |
| 6 | +export function createSwitchBranchId() { |
| 7 | + return `switch-branch-${nanoid(8)}`; |
| 8 | +} |
| 9 | + |
| 10 | +export function createSwitchBranch(branch: Partial<SwitchBranch> = {}): SwitchBranch { |
| 11 | + return { |
| 12 | + id: branch.id ?? createSwitchBranchId(), |
| 13 | + label: branch.label ?? "", |
| 14 | + condition: branch.condition ?? "", |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +export function createDefaultSwitchBranches(): SwitchBranch[] { |
| 19 | + return [ |
| 20 | + createSwitchBranch({ label: "Case 1", condition: "" }), |
| 21 | + createSwitchBranch({ label: "Case 2", condition: "" }), |
| 22 | + createSwitchBranch({ label: "default", condition: "Other cases" }), |
| 23 | + ]; |
| 24 | +} |
| 25 | + |
| 26 | +export function normalizeSwitchBranches(branches?: SwitchBranch[]): SwitchBranch[] { |
| 27 | + if (!branches?.length) return createDefaultSwitchBranches(); |
| 28 | + return branches.map((branch) => createSwitchBranch(branch)); |
| 29 | +} |
| 30 | + |
| 31 | +export function isDefaultSwitchBranch(branch: Pick<SwitchBranch, "label">, index: number, total: number) { |
| 32 | + return index === total - 1 || branch.label === "default"; |
| 33 | +} |
| 34 | + |
| 35 | +export function getSwitchBranchHandleId( |
| 36 | + branch: Pick<SwitchBranch, "id" | "label">, |
| 37 | + index: number, |
| 38 | + total: number, |
| 39 | +) { |
| 40 | + if (branch.id) return branch.id; |
| 41 | + if (isDefaultSwitchBranch(branch, index, total)) return "default"; |
| 42 | + return `branch-${index}`; |
| 43 | +} |
| 44 | + |
| 45 | +export function getSwitchBranchHandleAliases( |
| 46 | + branch: Pick<SwitchBranch, "id" | "label">, |
| 47 | + index: number, |
| 48 | + total: number, |
| 49 | +) { |
| 50 | + const aliases = new Set<string>(); |
| 51 | + const primary = getSwitchBranchHandleId(branch, index, total); |
| 52 | + const indexHandle = `branch-${index}`; |
| 53 | + |
| 54 | + if (indexHandle !== primary) aliases.add(indexHandle); |
| 55 | + if (isDefaultSwitchBranch(branch, index, total) && primary !== "default") aliases.add("default"); |
| 56 | + |
| 57 | + const trimmedLabel = branch.label.trim(); |
| 58 | + if (trimmedLabel && trimmedLabel !== primary) aliases.add(trimmedLabel); |
| 59 | + |
| 60 | + return [...aliases]; |
| 61 | +} |
| 62 | + |
| 63 | +export function findSwitchBranchIndexByHandle( |
| 64 | + branches: Array<Pick<SwitchBranch, "id" | "label">>, |
| 65 | + handle: string | null | undefined, |
| 66 | +) { |
| 67 | + if (!handle) return -1; |
| 68 | + |
| 69 | + return branches.findIndex((branch, index) => { |
| 70 | + const primary = getSwitchBranchHandleId(branch, index, branches.length); |
| 71 | + if (handle === primary) return true; |
| 72 | + return getSwitchBranchHandleAliases(branch, index, branches.length).includes(handle); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +export function getSwitchBranchLabelFromHandle( |
| 77 | + branches: Array<Pick<SwitchBranch, "id" | "label">>, |
| 78 | + handle: string | null | undefined, |
| 79 | +) { |
| 80 | + const index = findSwitchBranchIndexByHandle(branches, handle); |
| 81 | + if (index === -1) return null; |
| 82 | + |
| 83 | + const branch = branches[index]; |
| 84 | + if (branch.label.trim()) return branch.label; |
| 85 | + return isDefaultSwitchBranch(branch, index, branches.length) ? "default" : `Case ${index + 1}`; |
| 86 | +} |
| 87 | + |
0 commit comments