Skip to content

Instantly share code, notes, and snippets.

@assembledadam
Last active July 24, 2020 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assembledadam/80c97eb03fdc35e1e92ae38537a02ac0 to your computer and use it in GitHub Desktop.
Save assembledadam/80c97eb03fdc35e1e92ae38537a02ac0 to your computer and use it in GitHub Desktop.
Function to convert snake case to camelCase (ES6+)
/**
 * Change string from snake case to camelCase
 *
 * @param {string} str Input snake case string
 * @return {string} Output camel case string
 */
function toCamelCase(str) {
  str = str.replace(/[-_\s]+(.)?/g, (match, ch) => // eslint-disable-line no-param-reassign
    (ch ? ch.toUpperCase() : ''),
  );

  // Ensure first chat is always lowercase
  return str.substr(0, 1).toLowerCase() + str.substr(1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment