@ou-imdt/utils
Version:
Utility library for interactive media development
14 lines • 566 B
JavaScript
/**
* Converts a string to camel case, leaving existing uppercase.
* @param {string} str - The string to convert.
* @returns {string} The camel case version of the string.
*/
export default function toCamelCase(str) {
return str
// matches non alpha-numeric chars (ignoring first) and uppercases following char
.replace(/(?!^)[^a-zA-Z0-9]+(.)/g, (_, char) => char.toUpperCase())
// removes any non alphanumeric characters
.replace( /[^\w\s]/g, '')
// matches first char to lowercase
.replace(/^(.)/, (_, char) => char.toLowerCase());
}