-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSDoc example for translatePlatformKeys, rename translatePlatformKey
- Loading branch information
Showing
3 changed files
with
52 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { Key } = require('selenium-webdriver'); | ||
const isMacOS = require('./isMacOS'); | ||
|
||
const MAC_KEY_MAPPINGS = { | ||
[Key.CONTROL]: Key.META, | ||
}; | ||
|
||
/** | ||
* Translates a key or key combination for the current OS | ||
* | ||
* @param {string|string[]} keys - The key(s) to translate | ||
* @returns {string[]} - The translated key(s) as a flat array ready for spreading | ||
* | ||
* @example | ||
* // On macOS, translates CONTROL to META (Command key) | ||
* translatePlatformKey(Key.CONTROL) | ||
* // Returns: [Key.META] | ||
* | ||
* // On non-macOS systems, returns key unchanged | ||
* translatePlatformKey(Key.CONTROL) | ||
* // Returns: [Key.CONTROL] | ||
* | ||
* // Works with arrays of keys for key combinations | ||
* translatePlatformKey([Key.CONTROL, 'a']) | ||
* // Returns on macOS: [Key.META, 'a'] | ||
* // Returns on Windows/Linux: [Key.CONTROL, 'a'] | ||
* | ||
* // Usage with Selenium WebDriver: | ||
* const selectAllKeys = translatePlatformKey([Key.CONTROL, 'a']); | ||
* const selectAllChord = Key.chord(...selectAllKeys); | ||
* await element.sendKeys(selectAllChord); | ||
*/ | ||
function translatePlatformKeys(keys) { | ||
const keyArray = Array.isArray(keys) ? keys : [keys]; | ||
if (!isMacOS()) { | ||
return keyArray; | ||
} | ||
|
||
return keyArray.reduce((acc, key) => { | ||
const mappedKey = MAC_KEY_MAPPINGS[key] || key; | ||
return acc.concat(mappedKey); | ||
}, []); | ||
} | ||
|
||
module.exports = translatePlatformKeys; |