Skip to content

Instantly share code, notes, and snippets.

@liamnewmarch
Created March 27, 2018 15:30
Show Gist options
  • Save liamnewmarch/98824715f178ded52169fd3e57c22c62 to your computer and use it in GitHub Desktop.
Save liamnewmarch/98824715f178ded52169fd3e57c22c62 to your computer and use it in GitHub Desktop.
Conversion between camelCase and kebab-case, using the DOM.
/**
* Convert kebab-case to camelCase.
* @param {string} kebabString The kebab-case string to convert.
* @return {string} The string converted to camelCase.
*/
function camelCase(kebabString) {
const div = document.createElement('div');
div.setAttribute(`data-${kebabString}`, '');
return Object.keys(div.dataset)[0];
}
/**
* Convert camelCase to kebab-case.
* @param {string} camelString The camelCase string to convert.
* @return {string} The string converted to kebab-case.
*/
function kebabCase(camelString) {
const div = document.createElement('div');
div.dataset[camelString] = '';
return div.attributes[0].name.substr(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment