Created
March 27, 2018 15:30
-
-
Save liamnewmarch/98824715f178ded52169fd3e57c22c62 to your computer and use it in GitHub Desktop.
Conversion between camelCase and kebab-case, using the DOM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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