(keyInput: string)
| 278 | * Returns the primary key, followed by modifiers in original order. |
| 279 | */ |
| 280 | export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] { |
| 281 | let key = ''; |
| 282 | const result: KeyInput[] = []; |
| 283 | for (const ch of keyInput) { |
| 284 | // Handle cases like Shift++. |
| 285 | if (ch === '+' && key) { |
| 286 | result.push(throwIfInvalidKey(key)); |
| 287 | key = ''; |
| 288 | } else { |
| 289 | key += ch; |
| 290 | } |
| 291 | } |
| 292 | if (key) { |
| 293 | result.push(throwIfInvalidKey(key)); |
| 294 | } |
| 295 | |
| 296 | if (result.length === 0) { |
| 297 | throw new Error(`Key ${keyInput} could not be parsed.`); |
| 298 | } |
| 299 | |
| 300 | if (new Set(result).size !== result.length) { |
| 301 | throw new Error(`Key ${keyInput} contains duplicate keys.`); |
| 302 | } |
| 303 | |
| 304 | return [result.at(-1), ...result.slice(0, -1)] as [KeyInput, ...KeyInput[]]; |
| 305 | } |
no test coverage detected