(
hotkey: Hotkey | (string & {}),
)
| 22 | * ``` |
| 23 | */ |
| 24 | export function validateHotkey( |
| 25 | hotkey: Hotkey | (string & {}), |
| 26 | ): ValidationResult { |
| 27 | const warnings: Array<string> = [] |
| 28 | const errors: Array<string> = [] |
| 29 | |
| 30 | // Check for empty string |
| 31 | if (!hotkey || hotkey.trim() === '') { |
| 32 | return { |
| 33 | valid: false, |
| 34 | warnings: [], |
| 35 | errors: ['Hotkey cannot be empty'], |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const parts = hotkey.split('+').map((p) => p.trim()) |
| 40 | |
| 41 | // Must have at least one part (the key) |
| 42 | if (parts.length === 0 || parts.some((p) => p === '')) { |
| 43 | return { |
| 44 | valid: false, |
| 45 | warnings: [], |
| 46 | errors: ['Invalid hotkey format: empty parts detected'], |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Validate modifiers (all parts except the last) |
| 51 | const modifierParts = parts.slice(0, -1) |
| 52 | const keyPart = parts[parts.length - 1]! |
| 53 | |
| 54 | // Check for unknown modifiers |
| 55 | for (const modifier of modifierParts) { |
| 56 | const normalized = |
| 57 | MODIFIER_ALIASES[modifier] ?? MODIFIER_ALIASES[modifier.toLowerCase()] |
| 58 | if (!normalized) { |
| 59 | errors.push(`Unknown modifier: '${modifier}'`) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Check if key is known |
| 64 | const normalizedKey = normalizeKeyForValidation(keyPart) |
| 65 | if (!isKnownKey(normalizedKey) && !isKnownKey(keyPart)) { |
| 66 | warnings.push( |
| 67 | `Unknown key: '${keyPart}'. This may still work but won't have type-safe autocomplete.`, |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | return { |
| 72 | valid: errors.length === 0, |
| 73 | warnings, |
| 74 | errors, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Normalizes a key for validation checking. |
no test coverage detected
searching dependent graphs…