export type CycleDirection = -1 | 1; export function cycleOption(values: readonly T[], current: T, direction: CycleDirection): T | undefined { if (values.length === 0) return undefined; const currentIndex = values.indexOf(current); const startIndex = currentIndex >= 0 ? currentIndex : direction === 1 ? -1 : 0; return values[(startIndex + direction + values.length) % values.length]; } export function cycleNumberOption(values: readonly number[], current: number, direction: CycleDirection): number | undefined { if (values.length === 0) return undefined; if (direction === 1) return values.find((value) => value > current) ?? values[0]; return values.findLast((value) => value < current) ?? values[values.length - 1]; }