The snakecase package contains five specific case converter functions and one more general high level function with added functionality.
# devtools::install_github("Tazinho/snakecase")
library(snakecase)
strings <- c("this Is a Strange_string", "AND THIS ANOTHER_One")
to_parsed_case(strings)
## [1] "this_Is_a_Strange_string" "AND_THIS_ANOTHER_One"
to_snake_case(strings)
## [1] "this_is_a_strange_string" "and_this_another_one"
to_small_camel_case(strings)
## [1] "thisIsAStrangeString" "andThisAnotherOne"
to_big_camel_case(strings)
## [1] "ThisIsAStrangeString" "AndThisAnotherOne"
to_screaming_snake_case(strings)
## [1] "THIS_IS_A_STRANGE_STRING" "AND_THIS_ANOTHER_ONE"
The function to_any_case()
can do everything that the others can and also adds some extra high level functionality.
to_any_case(strings, case = "parsed")
## [1] "this_Is_a_Strange_string" "AND_THIS_ANOTHER_One"
to_any_case(strings, case = "snake")
## [1] "this_is_a_strange_string" "and_this_another_one"
to_any_case(strings, case = "small_camel")
## [1] "thisIsAStrangeString" "andThisAnotherOne"
to_any_case(strings, case = "big_camel")
## [1] "ThisIsAStrangeString" "AndThisAnotherOne"
to_any_case(strings, case = "screaming_snake")
## [1] "THIS_IS_A_STRANGE_STRING" "AND_THIS_ANOTHER_ONE"
By default only whitespaces, underscores and some patterns of mixed lower/upper case letter combinations are recognized as word boundaries. You can specify anything as a word boundary which is a valid stringr regular expression:
strings2 <- c("this - Is_-: a Strange_string", "ÄND THIS ANOTHER_One")
to_snake_case(strings2)
## [1] "this_-_is_-_:_a_strange_string" "änd_this_another_one"
to_any_case(strings2, case = "snake", preprocess = "-|\\:")
## [1] "this_is_a_strange_string" "änd_this_another_one"
You can also specify a different separator than "_"
or ""
via postprocess
:
to_any_case(strings2, case = "snake", preprocess = "-|\\:", postprocess = " ")
## [1] "this is a strange string" "änd this another one"
to_any_case(strings2, case = "big_camel", preprocess = "-|\\:", postprocess = "//")
## [1] "This//Is//A//Strange//String" "Änd//This//Another//One"
You can set pre -and suffixes:
to_any_case(strings2, case = "big_camel", preprocess = "-|\\:", postprocess = "//",
prefix = "USER://", postfix = ".exe")
## [1] "USER://This//Is//A//Strange//String.exe"
## [2] "USER://Änd//This//Another//One.exe"
If you have problems with special characters on your platform, you can replace them via replace_special_characters = TRUE
:
strings3 <- c("ßüss üß ä stränge sträng", "unrealistisch aber nützich")
to_any_case(strings3, case = "screaming_snake", replace_special_characters = TRUE)
## [1] "SSUESS_UESS_AE_STRAENGE_STRAENG" "UNREALISTISCH_ABER_NUETZICH"
If you see unwanted underscores around specific pattern, which you don’t want to delete via preprocess
, just use protect
:
strings4 <- c("var12", "var1.2", "va.r.1.2")
to_any_case(strings4, case = "snake")
## [1] "var_12" "var_1_._2" "va_._r_._1_._2"
to_any_case(strings4, case = "snake", protect = "\\d")
## [1] "var12" "var1.2" "va_._r_.1.2"
to_any_case(strings4, case = "snake", protect = "\\d|\\.")
## [1] "var12" "var1.2" "va.r.1.2"
to_any_case()
is vectorised over string
, preprocess
, postprocess
, prefix
, postfix
and protect
.
If you are interested in the design of this package, you can find more information on its github page.