Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keycodes: avoid regex for capital case #56822

Merged
merged 2 commits into from
Dec 7, 2023
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
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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Minor language nit: since in web we generally go with the US conventions, I'd alter capitalise to capitalize.

Suggested change
function capitaliseFirstCharacter( string ) {
function capitalizeFirstCharacter( string ) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. Although I personally favour the s spelling, we should keep names consistent and greppable, @ellatrix

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, looks like I forgot to fix it

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
Loading