-
Notifications
You must be signed in to change notification settings - Fork 192
Add functions: str_to_pascal()
, str_to_camel()
, str_to_kebab()
, str_to_snake()
#577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
CC: @kbodwin |
Sorry for missing this. Are you still interested in finishing it off? No problems if not, as I'm happy to do it myself. |
We're happy to make changes if needed! But I'm not sure I see what's missing - do you see something more you'd want for this to be ready for review? |
@@ -12,11 +12,13 @@ S3method(type,stringr_fixed) | |||
S3method(type,stringr_regex) | |||
export("%>%") | |||
export("str_sub<-") | |||
export(StrToPascal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I see them, I don't think it's worth having the camel case variants; it feels a bit too clever.
#' | ||
#' * `str_to_upper()` converts to upper case. | ||
#' * `str_to_lower()` converts to lower case. | ||
#' * `str_to_title()` converts to title case, where only the first letter of | ||
#' each word is capitalized. | ||
#' * `str_to_sentence()` convert to sentence case, where only the first letter | ||
#' of sentence is capitalized. | ||
#' * `str_to_pascal()` converts to pascal case, where only the first letter of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure Pascal case is particularly well known (at least I didn't recognise it) so how about it make it an argument to str_camel_case()
, e.g. first_upper = TRUE
?
stopifnot(is.character(string)) | ||
string <- string |> | ||
str_replace_all("([a-z])([A-Z])", "\\1 \\2") |> | ||
str_replace_all("([a-zA-Z])([0-9])", "\\1 \\2") |> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense to extract out this code for "break into words" into a separate function since it's reasonably complex and giving it a name would be useful.
strToCamel <- str_to_camel | ||
#' @export | ||
#' @rdname case | ||
str_to_snake <- function(string, separator = "_", locale = "en") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could eliminate the separator
argument by creating an internal function that both snake and kebab case uses.
@@ -1,21 +1,41 @@ | |||
#' Convert string to upper case, lower case, title case, or sentence case | |||
#' Convert string to upper case, lower case, title case, sentence case, pascal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to document these new functions in a new documentation topic? Yes, they're are changing case, but you're more likely to be apply them to function names, not regular sentences.
expect_identical(str_to_sentence("a Test"), "A test") | ||
}) | ||
|
||
test_that("to_pascal converts to pascal case", { | ||
expect_identical(str_to_pascal("This is a sentence."), "ThisIsASentence") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest testing cases the exercise each of the different regular expressions. I think that will be easier once you extract out a common "break into words" function.
Added 4 functions for common programming use. Closes #573 .
All functions call an existing
str_to_*()
so locale argument respects the behavior of those functionsAliases
StrToPascal()
andstrToCamel()
exist (str-to-kebab()
is not a valid name)