Skip to content

Commit

Permalink
fix #2044: in combine_words(), when words vector is of length 2 and `…
Browse files Browse the repository at this point in the history
…and` is blank, use `sep` (#2045)
  • Loading branch information
cderv authored Sep 21, 2021
1 parent 0c90228 commit ef4a475
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGES IN knitr VERSION 1.35

- In `knitr::combine_words()`, when `words` is length 2 and `and = ""`, `sep` will now be used (thanks, @eitsupi, #2044).

# CHANGES IN knitr VERSION 1.34

Expand Down
9 changes: 5 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,10 @@ create_label = function(..., latex = FALSE) {
#'
#' If the length of the input \code{words} is smaller than or equal to 1,
#' \code{words} is returned. When \code{words} is of length 2, the first word
#' and second word are combined using the \code{and} string. When the length is
#' greater than 2, \code{sep} is used to separate all words, and the \code{and}
#' string is prepended to the last word.
#' and second word are combined using the \code{and} string, or if blank,
#' \code{sep} if is used. When the length is greater than 2, \code{sep} is used
#' to separate all words, and the \code{and} string is prepended to the last
#' word.
#' @param words A character vector.
#' @param sep Separator to be inserted between words.
#' @param and Character string to be prepended to the last word.
Expand All @@ -866,7 +867,7 @@ combine_words = function(
if (n == 0) return(words)
words = paste0(before, words, after)
if (n == 1) return(rs(words))
if (n == 2) return(rs(paste(words, collapse = and)))
if (n == 2) return(rs(paste(words, collapse = if (is_blank(and)) sep else and)))
if (oxford_comma && grepl('^ ', and) && grepl(' $', sep)) and = gsub('^ ', '', and)
words[n] = paste0(and, words[n])
# combine the last two words directly without the comma
Expand Down
7 changes: 4 additions & 3 deletions man/combine_words.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ assert('combine_words() combines multiple words into a single string', {
(cw(NULL) %==% NULL)
(cw(c('a')) %==% 'a')
(cw(c('a', 'b')) %==% 'a and b')
(cw(c('a', 'b'), and = "") %==% 'a, b')
(cw(c('a', 'b', 'c')) %==% 'a, b, and c')
(cw(c('a', 'b', 'c'), and = '') %==% 'a, b, c')
(cw(c('a', 'b', 'c'), ' / ', '') %==% 'a / b / c')
Expand Down

0 comments on commit ef4a475

Please sign in to comment.