Skip to content

Commit

Permalink
add 'h' character before hash in case id is used in top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Oct 24, 2024
1 parent b24da53 commit 28d2246
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sanitize_id <- function(id) {
if (!grepl("^X", id)) {
id_converted <- gsub("^X", "", id_converted)
}
paste0(substr(rlang::hash(as.character(id)), 1, 4), "_", id_converted)
paste0("h", substr(rlang::hash(as.character(id)), 1, 4), "_", id_converted)
}

#' `NS` wrapper to sanitize ids for shiny
Expand Down
20 changes: 13 additions & 7 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,37 @@ testthat::describe("sanitize_id", {
ns <- NS("app")
testthat::expect_identical(
ns(id),
paste0("app-", substr(rlang::hash(id), 1, 4), "_a_b")
paste0("app-h", substr(rlang::hash(id), 1, 4), "_a_b")
)
})

testthat::it("should replace non-ASCII characters in the start/end of id with `_`", {
id <- "%a.b%c$"
id <- "%a bad symbol$"
id2 <- "a&b#"
id_from_module <- shiny::withReactiveDomain(
MockShinySession$new(),
moduleServer(id, function(input, output, session) session$ns("a_good_name"))
)

testthat::expect_identical(
NS("app", id),
paste0("app-", substr(rlang::hash(id), 1, 4), "__a_b_c_")
id_from_module,
paste0("h", substr(rlang::hash(id), 1, 4), "__a_bad_symbol_-a_good_name")
)
})

testthat::it("should replace all quotes characters with `_`", {
id <- " a.b.c\"d`e'j"
testthat::expect_identical(
NS("app", id),
paste0("app-", substr(rlang::hash(id), 1, 4), "__a_b_c_d_e_j")
paste0("app-h", substr(rlang::hash(id), 1, 4), "__a_b_c_d_e_j")
)
})

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

Expand All @@ -44,7 +50,7 @@ testthat::describe("sanitize_id", {
testthat::expect_identical(
NS("app", forbidden),
paste0(
"app-",
"app-h",
substr(rlang::hash(forbidden), 1, 4),
paste(rep("_", nchar(forbidden) + 1), collapse = "")
)
Expand Down

0 comments on commit 28d2246

Please sign in to comment.