Skip to content

Commit

Permalink
fix: allow for vector inputs and integers
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Oct 24, 2024
1 parent 67f57c6 commit 91f2576
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
25 changes: 16 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ make_c_call <- function(choices) {
#'
#' @keywords internal
sanitize_id <- function(id) {
id_converted <- make.names(id)
if (identical(make.names(id), id)) {
return(gsub("\\.", "_", id))
}
id_converted <- gsub("\\.", "_", id_converted)
if (!grepl("^X", id)) {
id_converted <- gsub("^X", "", id_converted)
}
paste0("h", substr(rlang::hash(as.character(id)), 1, 4), "_", id_converted)
escape_characters <- " !\"#$%&'()*+,./:;<=>?@[\\]^`{|}~"
pattern <- paste(
sep = "", collapse = "|", "\\", strsplit(escape_characters, "")[[1]]
)
pattern <- gsub("\\<", "<", pattern, fixed = TRUE)
pattern <- gsub("\\>", ">", pattern, fixed = TRUE)

id_new <- gsub(pattern, "_", id)
hashes <- vapply(
id[id != id_new],
rlang::hash, character(1),
USE.NAMES = FALSE
)

id[id != id_new] <- paste0("h", substr(hashes, 1, 4), "_", id_new[id != id_new])
id
}

#' `NS` wrapper to sanitize ids for shiny
Expand Down
40 changes: 32 additions & 8 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ testthat::test_that("make_c_call", {

# sanitize_id ----
testthat::describe("sanitize_id", {
testthat::it("should replace dots with `_` when id is otherwise valid", {
id <- "a.b"
ns <- NS("app")
testthat::expect_identical(
ns(id),
paste0("app-h", substr(rlang::hash(id), 1, 4), "_a_b")
)
})

testthat::it("should take vector input", {
id <- c("a.b", "a", "b", " c")
ns <- NS("app")
testthat::expect_identical(
ns(id),
c(
paste0("app-h", substr(rlang::hash(id[1]), 1, 4), "_a_b"),
"app-a",
"app-b",
paste0("app-h", substr(rlang::hash(id[4]), 1, 4), "__c")
)
)
})

testthat::it("should allow for integer input", {
id <- c(1L, 2L, 3L)
ns <- NS("app")
testthat::expect_identical(
ns(id),
c("app-1", "app-2", "app-3")
)
})

testthat::it("should replace non-ASCII characters in middle of id with `_`", {
id <- "a$b"
ns <- NS("app")
Expand Down Expand Up @@ -37,14 +69,6 @@ testthat::describe("sanitize_id", {
)
})

testthat::it("should replace UTF-8 special characters with `_`", {
id <- "a\U1F643"
testthat::expect_identical(
NS("app", id),
paste0("app-h", substr(rlang::hash(id), 1, 4), "_a_")
)
})

testthat::it("should replace all escape characters from JQuery selectors", {
forbidden <- " !\"#$%&'()*+,./:;<=>?@[\\]^`{|}~]"
testthat::expect_identical(
Expand Down

0 comments on commit 91f2576

Please sign in to comment.