Skip to content

Commit

Permalink
Do not do the same job again in the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jul 22, 2024
1 parent 9b21c9b commit 71f3b5e
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ export function convertKey(e: KeyboardEvent): string {
return e.key.toLowerCase()
}

function compareHotkey(hotkey: string, e: KeyboardEvent): boolean {
export function isHotkey(hotkeys: string[], e: KeyboardEvent): boolean {
let prefix = ''
if (e.metaKey) prefix += 'meta+'
if (e.ctrlKey) prefix += 'ctrl+'
if (e.altKey) prefix += 'alt+'
if (e.shiftKey) prefix += 'shift+'

let code = prefix + convertKey(e)

return code === hotkey
}

export function isHotkey(hotkeys: string[], e: KeyboardEvent): boolean {
return hotkeys.some(k => compareHotkey(k, e))
return hotkeys.some(hotkey => code === hotkey)
}

0 comments on commit 71f3b5e

Please sign in to comment.