-
-
Save jasonsemko/a9880701aff706c79b52f3042003f515 to your computer and use it in GitHub Desktop.
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
function changeCase(identifier, targetCase){ | |
if(identifier.length === 0) { return "" } | |
if(!targetCase.match(/^(snake|kebab|camel)$/)) { return undefined } | |
var replaceCapsWithStars = identifier.replace(/[A-Z]/g, '*') | |
if(new Set(replaceCapsWithStars.match(/[_\-\*]/g)).size > 1) { return undefined } | |
identifier = identifier.replace(/[A-Z]/g, (match) => '-' + match.toLowerCase()) | |
identifierArray = identifier.split(/[_\-]/g) | |
if(targetCase === 'snake') { return identifierArray.join('_') } | |
if(targetCase === 'kebab') { return identifierArray.join('-') } | |
identifierArray = identifierArray.map(function(element, index) { | |
if(index === 0) { return element } | |
return element.replace(/[a-z]/, (match) => match.toUpperCase()) | |
}) | |
return identifierArray.join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment