Last active
December 18, 2021 12:44
-
-
Save smitroshin/be4c840cf67a851a9fde25f9578f030b to your computer and use it in GitHub Desktop.
Parse string to spinal case format
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
/** | |
* 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