-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#' Ensure an argument is a length-1 character | ||
#' | ||
#' Calls to APIs often require a string argument. This function ensures that | ||
#' those arguments are length-1, non-`NA` character vectors, or length-1, | ||
#' non-`NA` vectors that can be coerced to character vectors. This is intended | ||
#' to ensure that calls to the API will not fail with predictable errors, thus | ||
#' avoiding unnecessary internet traffic. | ||
#' | ||
#' @inheritParams rlang::args_error_context | ||
#' @inheritParams stbl::stabilize_chr_scalar | ||
#' @inheritDotParams stbl::stabilize_chr_scalar x_class | ||
#' | ||
#' @return `x` coerced to a length-1 character vector, if possible. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' stabilize_string("a") | ||
#' stabilize_string(1.1) | ||
#' x <- letters | ||
#' try(stabilize_string(x)) | ||
#' x <- NULL | ||
#' try(stabilize_string(x)) | ||
#' x <- character() | ||
#' try(stabilize_string(x)) | ||
#' x <- NA | ||
#' try(stabilize_string(x)) | ||
stabilize_string <- function(x, | ||
..., | ||
regex = NULL, | ||
arg = rlang::caller_arg(x), | ||
call = rlang::caller_env()) { | ||
stbl::stabilize_chr_scalar( | ||
x, | ||
allow_null = FALSE, | ||
allow_zero_length = FALSE, | ||
allow_na = FALSE, | ||
regex = regex, | ||
x_arg = arg, | ||
call = call, | ||
... | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
test_that("stabilize_string() checks strings", { | ||
expect_error(stabilize_string(letters), class = "stbl_error_non_scalar") | ||
expect_error(stabilize_string(NULL), class = "stbl_error_bad_null") | ||
expect_error(stabilize_string(character()), class = "stbl_error_bad_empty") | ||
expect_error(stabilize_string(NA), class = "stbl_error_bad_na") | ||
expect_identical( | ||
stabilize_string("a"), | ||
"a" | ||
) | ||
}) |