Parse Keyboard Shortcuts into Typed Keys

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write parseShortcut(text) for shortcuts like ctrl+k or Shift+Alt+P: split on +, ignore case, and return a Shortcut with the key (the last part, lowercased) and a true or false flag for each of the modifiers ctrl, shift and alt, in any order. Return null when the text isn't a valid shortcut: no key, a key that is itself a modifier, an unknown or repeated modifier, or more than one key. Export the Shortcut type, and type the result so callers must check for null before reading key.

Your code must pass

  • Parses a key with modifiers
  • Parses a key on its own
  • Returns null for invalid shortcuts
  • Has no type errors
  • Shortcut has a flag for each modifier
  • Returns a shortcut or null
  • Must check for null before reading key