Skip to content

Commit

Permalink
Keycodes: avoid regex for capital case (WordPress#56822)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 7, 2023
1 parent f6adf1e commit f48ac05
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
6 changes: 2 additions & 4 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ exports[`KeyboardShortcutHelpModal should match snapshot when the modal is activ
class="edit-post-keyboard-shortcut-help-modal__shortcut-term"
>
<kbd
aria-label="Shift + Alt + 1 6"
aria-label="Shift + Alt + 1-6"
class="edit-post-keyboard-shortcut-help-modal__shortcut-key-combination"
>
<kbd
Expand Down
3 changes: 1 addition & 2 deletions packages/keycodes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/i18n": "file:../i18n",
"change-case": "^4.1.2"
"@wordpress/i18n": "file:../i18n"
},
"publishConfig": {
"access": "public"
Expand Down
27 changes: 13 additions & 14 deletions packages/keycodes/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
* shortcut combos directly to keyboardShortcut().
*/

/**
* External dependencies
*/
import { capitalCase } from 'change-case';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -148,6 +143,17 @@ export const ZERO = 48;

export { isAppleOS };

/**
* Capitalise the first character of a string.
* @param {string} string String to capitalise.
* @return {string} Capitalised string.
*/
function capitaliseFirstCharacter( string ) {
return string.length < 2
? string.toUpperCase()
: string.charAt( 0 ).toUpperCase() + string.slice( 1 );
}

/**
* Map the values of an object with a specified callback and return the result object.
*
Expand Down Expand Up @@ -260,14 +266,7 @@ export const displayShortcutList = mapValues(
/** @type {string[]} */ ( [] )
);

// Symbols (~`,.) are removed by the default regular expression,
// so override the rule to allow symbols used for shortcuts.
// see: https://github.com/blakeembrey/change-case#options
const capitalizedCharacter = capitalCase( character, {
stripRegexp: /[^A-Z0-9~`,\.\\\-]/gi,
} );

return [ ...modifierKeys, capitalizedCharacter ];
return [ ...modifierKeys, capitaliseFirstCharacter( character ) ];
};
}
);
Expand Down Expand Up @@ -335,7 +334,7 @@ export const shortcutAriaLabel = mapValues(

return [ ...modifier( _isApple ), character ]
.map( ( key ) =>
capitalCase( replacementKeyMap[ key ] ?? key )
capitaliseFirstCharacter( replacementKeyMap[ key ] ?? key )
)
.join( isApple ? ' ' : ' + ' );
};
Expand Down

0 comments on commit f48ac05

Please sign in to comment.