Skip to content

Commit

Permalink
Cover all _wp_to_kebab_case() scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jun 20, 2023
1 parent 5caa886 commit 2deb924
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/block-editor/src/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ function normalizePath( path ) {
/**
* Converts any string to kebab case.
* Backwards compatible with Lodash's `_.kebabCase()`.
* Backwards compatible with `_wp_to_kebab_case()`.
*
* @see https://lodash.com/docs/4.17.15#kebabCase
* @see https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/`
*
* @param {string} str String to convert.
* @return {string} Kebab-cased string
Expand All @@ -39,12 +41,15 @@ export function kebabCase( str ) {
input = str?.toString?.() ?? '';
}

// See https://github.com/lodash/lodash/blob/b185fcee26b2133bd071f4aaca14b455c2ed1008/lodash.js#L4970
input = input.replace( /['\u2019]/, '' );

return paramCase( input, {
splitRegexp: [
/([a-z0-9])([A-Z])/g, // fooBar => foo-bar, 3Bar => 3-bar
/([0-9])([a-z])/g, // 3bar => 3-bar
/(?!(?:1ST|2ND|3RD|[4-9]TH)(?![a-z]))([a-z0-9])([A-Z])/g, // fooBar => foo-bar, 3Bar => 3-bar
/(?!(?:1st|2nd|3rd|[4-9]th)(?![a-z]))([0-9])([a-z])/g, // 3bar => 3-bar
/([A-Za-z])([0-9])/g, // Foo3 => foo-3, foo3 => foo-3
/([A-Z])([A-Z][a-z])/g, // FOOBar => foo-bar
/([A-Z])([A-Z][a-z])/g, // FOOBar => foo-bar,
],
} );
}
Expand Down
36 changes: 36 additions & 0 deletions packages/block-editor/src/utils/test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ describe( 'kebabCase', () => {
expect( kebabCase( [] ) ).toEqual( '' );
expect( kebabCase( {} ) ).toEqual( 'object-object' );
} );

/**
* Should cover all test cases of `_wp_to_kebab_case()`
*
* @see https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/
* @see https://github.com/WordPress/wordpress-develop/blob/76376fdbc3dc0b3261de377dffc350677345e7ba/tests/phpunit/tests/functions/wpToKebabCase.php#L35-L62
*/
it.each( [
[ 'white', 'white' ],
[ 'white+black', 'white-black' ],
[ 'white:black', 'white-black' ],
[ 'white*black', 'white-black' ],
[ 'white.black', 'white-black' ],
[ 'white black', 'white-black' ],
[ 'white black', 'white-black' ],
[ 'white-to-black', 'white-to-black' ],
[ 'white2white', 'white-2-white' ],
[ 'white2nd', 'white-2nd' ],
[ 'white2ndcolor', 'white-2-ndcolor' ],
[ 'white2ndColor', 'white-2nd-color' ],
[ 'white2nd_color', 'white-2nd-color' ],
[ 'white23color', 'white-23-color' ],
[ 'white23', 'white-23' ],
[ '23color', '23-color' ],
[ 'white4th', 'white-4th' ],
[ 'font2xl', 'font-2-xl' ],
[ 'whiteToWhite', 'white-to-white' ],
[ 'whiteTOwhite', 'white-t-owhite' ],
[ 'WHITEtoWHITE', 'whit-eto-white' ],
[ 42, '42' ],
[ "i've done", 'ive-done' ],
[ '#ffffff', 'ffffff' ],
[ '$ffffff', 'ffffff' ],
] )( 'converts %s properly to %s', ( input, expected ) => {
expect( kebabCase( input ) ).toEqual( expected );
} );
} );

describe( 'setImmutably', () => {
Expand Down

0 comments on commit 2deb924

Please sign in to comment.