Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Last active December 18, 2021 12:44
Show Gist options
  • Save smitroshin/be4c840cf67a851a9fde25f9578f030b to your computer and use it in GitHub Desktop.
Save smitroshin/be4c840cf67a851a9fde25f9578f030b to your computer and use it in GitHub Desktop.
Parse string to spinal case format
/**
* Transforms string from any format case in spinal case (ex. spinal-case)
*
* Source: https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-spinal-tap-case/16078
*
* @param {string} str
*/
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str
.split(/[\W_]|(?=[A-Z])/)
.join('-')
.toLowerCase();
}
// spinalCase('thisIsSpinalTap');
// Output: this-is-spinal-tap
// The main focus is (?=[A-Z]) that split the array
// but don't excludes matched symbols.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment