Created
November 9, 2019 03:57
-
-
Save yanil3500/a59c4df6689302d4bbe90a7dae96cf3b to your computer and use it in GitHub Desktop.
[Decamelize] Decamelizes a string with/without a custom separator (underscore by default) #javascript #camelcase #utility
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
/** | |
* Decamelizes a string with/without a custom separator (underscore by default). | |
* | |
* @param str String in camelcase | |
* @param separator Separator for the new decamelized string. | |
*/ | |
function decamelize(str, separator){ | |
separator = typeof separator === 'undefined' ? '_' : separator; | |
return str | |
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2') | |
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2') | |
.toLowerCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment