Skip to content

Commit

Permalink
Handle errors in prefix in both functions
Browse files Browse the repository at this point in the history
  • Loading branch information
caldwellst committed May 29, 2024
1 parent 52e5d52 commit 83d2ac5
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions R/label_number_hdx.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@
#'
#' @export
label_number_hdx <- function(additional_prefix = "") {
if (!is.character(additional_prefix) || length(additional_prefix) > 1) {
stop(
"`additional_prefix` should be a character string of length 1, not a ",
class(additional_prefix)[1],
".",
call. = FALSE
)
}

assert_prefix(additional_prefix)
function(x) number_hdx(x = x, additional_prefix = additional_prefix)
}

Expand All @@ -87,6 +79,8 @@ label_number_hdx <- function(additional_prefix = "") {
#'
#' @export
number_hdx <- function(x, additional_prefix = "") {
assert_prefix(additional_prefix)

if (!is.numeric(x)) {
stop(
"`x` must be numeric, not class ",
Expand All @@ -112,3 +106,15 @@ number_hdx <- function(x, additional_prefix = "") {
abs_x >= 1e13 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e12, digits = 0), "T")
)
}

#' @noRd
assert_prefix(additional_prefix) {
if (!is.character(additional_prefix) || length(additional_prefix) > 1) {
stop(
"`additional_prefix` should be a character string of length 1, not a ",
class(additional_prefix)[1],
".",
call. = FALSE
)
}
}

0 comments on commit 83d2ac5

Please sign in to comment.