Created
June 17, 2020 08:57
-
-
Save sandrabosk/8addb6b05d71cc6da7f6ae5638049af7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ************************************************************************************ | |
// https://www.codewars.com/kata/camelcase-to-underscore/train/javascript | |
// You wrote all your unit test names in camelCase. But some of your colleagues have | |
// troubles reading these long test names. So you make a compromise to switch to | |
// underscore separation. To make these changes fast you wrote a class to translate | |
// a camelCase name into an underscore separated name. | |
// Implement the ToUnderscore() method. | |
// Example: | |
// "ThisIsAUnitTest" => "This_Is_A_Unit_Test" | |
// But of course there are always special cases... | |
// You also have some calculation tests. Make sure the results don't get split by underscores. | |
// So only add an underscore in front of the first number. | |
// Also Some people already used underscore names in their tests. You don't want to change them. | |
// But if they are not split correct you should adjust them. | |
// Some of your colleagues mark their tests with a leading and trailing underscore. Don't remove this. | |
// And of course you should handle empty strings to avoid unnecessary errors. Just return an empty string then. | |
// Example: | |
// "Calculate15Plus5Equals20" => "Calculate_15_Plus_5_Equals_20" | |
// "This_Is_Already_Split_Correct" => "This_Is_Already_Split_Correct" | |
// "ThisIs_Not_SplitCorrect" => "This_Is_Not_Split_Correct" | |
// "_UnderscoreMarked_Test_Name_" => _Underscore_Marked_Test_Name_" | |
// ************************************************************************************ | |
let toUnderScore = (name) => { | |
return(name.replace(/\B([A-Z]|[0-9]+)/g, '_$1')).replace(/__/g,'_'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment