Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In combine_words, when words vector is length 2 and and is blank, use sep #2045

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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