Skip to content

Instantly share code, notes, and snippets.

@rigwild
Created November 26, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rigwild/0ec9fa6ace5efb5575e807b71d3f2f05 to your computer and use it in GitHub Desktop.
Save rigwild/0ec9fa6ace5efb5575e807b71d3f2f05 to your computer and use it in GitHub Desktop.
/**
* Convert any string to kebab-case.
* Removes accents, multiples whitespaces and symbols to one "-" and converts to lower case.
* Removes any leading and leading "-".
*
* @param {string} str string to convert
* @returns {string} string in kebab-case
* @author rigwild <me@rigwild.dev>
* @see https://gist.github.com/rigwild/3e4d30bd269535b7508926c8beaeef90
*/
const toKebabCase = str => str
.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s+]/gi, '-')
.replace(/\-+/g, '-')
.replace(/^\-|\-$/g, '')
.toLowerCase()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment