Case converters

Malte Grosser

2018-01-21

Basic examples

Default case is snake case

library(snakecase)
to_any_case("veryComplicatedString")
## [1] "very_complicated_string"

Dots and other special characters may have a special intention

to_any_case("malte.grosser@gmail.com")
## [1] "malte.grosser@gmail.com"

When it is clear that they are separators, you can supply them as a regex to the sep_in argument

to_any_case(names(iris), sep_in = "\\.")
## [1] "sepal_length" "sepal_width"  "petal_length" "petal_width" 
## [5] "species"

This is especially handy, when special characters have a meaning as a separator or for example as a decimal mark

to_any_case("Pi.Value:3.14", sep_in = ":|(?<!\\d)\\.")
## [1] "pi_value_3.14"

Of course other cases are supported (case) and separators can be adjusted (sep_out)

to_any_case(names(iris), sep_in = "\\.", case = "upper_camel", sep_out = " ")
## [1] "Sepal Length" "Sepal Width"  "Petal Length" "Petal Width" 
## [5] "Species"

And you might want to remove special characters along the way

to_any_case("Doppelgänger is originally german", 
            transliterations = "german", case = "upper_camel")
## [1] "DoppelgaengerIsOriginallyGerman"

All of the cases like: snake, lower_camel, upper_camel, all_caps, lower_upper, upper_lower and mixed are based on parsed case

to_any_case("THISIsHOW IAmPARSED!", case = "parsed")
## [1] "THIS_Is_HOW_I_Am_PARSED!"

Shortcut wrappers like to_snake_case, to_lower_camel_case etc. are available.

Be aware that automatic case conversion depends on the input string and it is recommended to verify the results. So you might want to pipe these into dput() (and hardcode name changes instead of blindly trusting to_any_case()’s output)

dput(to_any_case(c("SomeBAdInput", "someGoodInput")))
## c("some_b_ad_input", "some_good_input")

If you are interested in the design of this package, you can find more information on its github page.