@ou-imdt/utils
Version:
Utility library for interactive media development
14 lines • 525 B
JavaScript
/**
* Converts a given string to slug case.
* @param {string} str - The string to convert.
* @returns {string} The slug case version of the string.
*/
export default function toSlugCase(str) {
return str
.replace(/ /g,'-') // space to hyphen
.replace(/([A-Z])/g, '-$1') // caps to hyphen
.replace(/[^A-Za-z0-9-]+/g, '') // remove non-alpha/numeric or hyphen
.replace(/[-]+/g, '-') // one or more hyphens to hyphen
.replace(/^-+|-+$/g, '') // remove leading and trailing hyphens
.toLowerCase();
}