Convert strings to kebab-case, matching WordPress Core’s _wp_to_kebab_case().
This package exists because no off-the-shelf library matches the semantics of WordPress Core’s _wp_to_kebab_case() function, which WordPress needs to preserve for backwards compatibility.
Installation
Install the module
npm install @wordpress/kebab-case --save
Usage
kebabCase
Converts any string to kebab-case.
The conversion is designed to match WordPress Core’s _wp_to_kebab_case() function (and, transitively, Lodash’s _.kebabCase()): the package’s test suite covers every test case of the Core function. These semantics differ from other kebab-case implementations in how numbers are handled ('white23' becomes 'white-23', 'font2xl' becomes 'font-2-xl', yet 'white4th' stays 'white-4th').
import { kebabCase } from '@wordpress/kebab-case';
kebabCase( 'whiteToWhite' ); // 'white-to-white'
kebabCase( 'white2white' ); // 'white-2-white'
kebabCase( 'font2xl' ); // 'font-2-xl'
kebabCase( '#ffffff' ); // 'ffffff'
kebabCase( "i've done" ); // 'ive-done'
API
kebabCase
Converts any string to kebab case. Backwards compatible with Lodash’s _.kebabCase(). Backwards compatible with _wp_to_kebab_case().
Related
- https://lodash.com/docs/4.17.15#kebabCase
- https://developer.wordpress.org/reference/functions/_wp_to_kebab_case/
Parameters
- str
unknown: String to convert.
Returns
- Kebab-cased string
Contributing to this package
This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.
To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.