Skip to content

Commit 4277d24

Browse files
committed
simplify checkSelectorNames per maintainer feedback
Address review comment to simplify nested sapply calls and fix test typo.
1 parent a53f143 commit 4277d24

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

R/z_animintHelpers.R

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -651,29 +651,23 @@ checkSelectorNames <- function(selectors){
651651
invalid.chars <- c("#", "!", "@", "$", "%", "^", "&", "*", "=", "|", "\\", "/", "'", "\"", "`", "?", "<", ">", "(", ")", "[", "]")
652652

653653
## Check each selector name for invalid characters
654-
has.invalid <- sapply(selector.names, function(name) {
655-
any(sapply(invalid.chars, function(char) {
656-
grepl(char, name, fixed = TRUE)
657-
}))
658-
})
654+
## Check if any invalid character appears in each selector name
655+
has.invalid <- vapply(selector.names, function(name) {
656+
any(vapply(invalid.chars, grepl, logical(1), x = name, fixed = TRUE))
657+
}, logical(1))
659658

660659
if(any(has.invalid)){
661660
invalid.names <- selector.names[has.invalid]
662661
## Find which character(s) are problematic for each name
663662
problematic <- sapply(invalid.names, function(name) {
664-
found.chars <- invalid.chars[sapply(invalid.chars, function(char) {
665-
grepl(char, name, fixed = TRUE)
666-
})]
663+
found.chars <- invalid.chars[sapply(invalid.chars, grepl, name, fixed = TRUE)]
667664
paste0("'", found.chars, "'", collapse = ", ")
668665
})
669666

670-
invalid.list <- paste0(" - '", invalid.names, "' contains ", problematic, collapse = "\n")
671-
error.msg <- paste0(
672-
"Invalid character(s) in selector name(s). ",
673-
"Selector names cannot contain special characters that interfere with CSS selectors.\n",
674-
"The following selector(s) contain invalid characters:\n",
675-
invalid.list,
676-
"\n\nPlease remove or replace these characters in your variable names."
667+
invalid.list <- paste(sprintf(" - '%s' contains %s", invalid.names, problematic), collapse = "\n")
668+
error.msg <- sprintf(
669+
"Invalid character(s) in selector name(s). Selector names cannot contain special characters that interfere with CSS selectors.\nThe following selector(s) contain invalid characters:\n%s\n\nPlease remove or replace these characters in your variable names.",
670+
invalid.list
677671
)
678672

679673
stop(error.msg)

tests/testthat/test-compiler-invalid-selector-characters.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
acontext("Invalid selector character validation")
1+
context("Invalid selector character validation")
22

33
# Test that selector names with special characters cause errors
44
# Note: Selector names come from the VALUES in the data when using .variable/.value pattern

0 commit comments

Comments
 (0)