Skip to content

Commit

Permalink
refactor xp_and/xp_or to match common usage (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Mar 20, 2022
1 parent b659d91 commit 585e7cd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions R/xp_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ xml_nodes_to_lint <- function(xml, source_file, lint_message,
))
}

paren_wrap <- function(..., sep) {
sep <- paste(")", sep, "(")
dots <- list(...)
if (length(dots) == 1L && length(dots[[1L]]) > 1L) {
inner <- paste(dots[[1L]], collapse = sep)
} else {
inner <- paste(..., sep = sep)
}
paste0("(", inner, ")")
}

#' Safer wrapper for paste(..., sep = " and ")
#'
#' The intent is to use this for readability when combining XPath conditions so
Expand All @@ -40,7 +51,7 @@ xml_nodes_to_lint <- function(xml, source_file, lint_message,
#'
#' @param ... Series of conditions
#' @noRd
xp_and <- function(...) sprintf("(%s)", paste(..., sep = ") and ("))
xp_and <- function(...) paren_wrap(..., sep = "and")

#' Safer wrapper for paste(..., sep = " or ")
#'
Expand All @@ -51,4 +62,4 @@ xp_and <- function(...) sprintf("(%s)", paste(..., sep = ") and ("))
#'
#' @param ... Series of conditions
#' @noRd
xp_or <- function(...) sprintf("(%s)", paste(..., sep = ") or ("))
xp_or <- function(...) paren_wrap(..., sep = "or")

0 comments on commit 585e7cd

Please sign in to comment.