Created
September 28, 2016 00:12
-
-
Save doublejosh/437353bef204aa42a943f9059a83f262 to your computer and use it in GitHub Desktop.
Remove specific prefix word from camelCase strings
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
/** | |
* Remove prefix word from camelCase strings. | |
* | |
* @example stripCamelPrefix('myOldThing'); // oldThing | |
* | |
* @param {string} str | |
* @param {string} prefix | |
* | |
* @return {string} | |
*/ | |
function stripCamelPrefix (str, prefix) { | |
var words = str.split(/(?=[A-Z])/); | |
// Found and more words. | |
if (words.length > 0 && words[0] === prefix) { | |
// Remove prefix (destructive), adjust new first word. | |
words.shift(); | |
words[0] = words[0].toLowerCase(); | |
// Create new string. | |
return words.join(''); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment