Skip to content

Commit 585e7cd

Browse files
refactor xp_and/xp_or to match common usage (#961)
1 parent b659d91 commit 585e7cd

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

R/xp_utils.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ xml_nodes_to_lint <- function(xml, source_file, lint_message,
3131
))
3232
}
3333

34+
paren_wrap <- function(..., sep) {
35+
sep <- paste(")", sep, "(")
36+
dots <- list(...)
37+
if (length(dots) == 1L && length(dots[[1L]]) > 1L) {
38+
inner <- paste(dots[[1L]], collapse = sep)
39+
} else {
40+
inner <- paste(..., sep = sep)
41+
}
42+
paste0("(", inner, ")")
43+
}
44+
3445
#' Safer wrapper for paste(..., sep = " and ")
3546
#'
3647
#' The intent is to use this for readability when combining XPath conditions so
@@ -40,7 +51,7 @@ xml_nodes_to_lint <- function(xml, source_file, lint_message,
4051
#'
4152
#' @param ... Series of conditions
4253
#' @noRd
43-
xp_and <- function(...) sprintf("(%s)", paste(..., sep = ") and ("))
54+
xp_and <- function(...) paren_wrap(..., sep = "and")
4455

4556
#' Safer wrapper for paste(..., sep = " or ")
4657
#'
@@ -51,4 +62,4 @@ xp_and <- function(...) sprintf("(%s)", paste(..., sep = ") and ("))
5162
#'
5263
#' @param ... Series of conditions
5364
#' @noRd
54-
xp_or <- function(...) sprintf("(%s)", paste(..., sep = ") or ("))
65+
xp_or <- function(...) paren_wrap(..., sep = "or")

0 commit comments

Comments
 (0)