Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions src/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ function localizeMod(hotkey: string, platform?: string | undefined): string {
return hotkey.replace('Mod', localModifier)
}

const orderedModifiers: Partial<Record<string, number>> = {
Control: 0,
Alt: 1,
Meta: 2,
Shift: 3
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a little simpler to just write an ordered array here and use indexOf for the numbers, but it would be slightly slower since the lookup would be $O(n)$, whereas the object lookup is $O(1)$.


function sortModifiers(hotkey: string): string {
const key = hotkey.split('+').pop()
const modifiers: string[] = []
for (const modifier of ['Control', 'Alt', 'Meta', 'Shift']) {
if (hotkey.includes(modifier)) {
modifiers.push(modifier)
}
}
if (key) modifiers.push(key)
return modifiers.join('+')
return hotkey
.split('+')
.sort((a, b) => (orderedModifiers[a] ?? Infinity) - (orderedModifiers[b] ?? Infinity))
.join('+')
}
5 changes: 4 additions & 1 deletion test/test-normalize-hotkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('normalizeHotkey', () => {
['Mod+a', 'Control+a', undefined],
// Modifier sorting
['Shift+Alt+Meta+Control+m', 'Control+Alt+Meta+Shift+m'],
['Shift+Alt+Mod+m', 'Control+Alt+Shift+m', 'win']
['Shift+Alt+Mod+m', 'Control+Alt+Shift+m', 'win'],
// Edge case: only modifiers
['Alt', 'Alt', 'win / linux'],
['Alt+Mod', 'Control+Alt', 'win / linux']
]

for (const [input, expected, platform = 'any platform'] of tests) {
Expand Down
Loading