Skip to content

Commit

Permalink
Add sep argument to str_dup() (#564)
Browse files Browse the repository at this point in the history
Fixes #487
  • Loading branch information
edward-burn authored Aug 20, 2024
1 parent b511603 commit 0b92cfb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# stringr (development version)

* Add `sep` argument to `str_dup()` so that it is possible to repeat a string and
add a separator between every repeated value (@edward-burn, #564).
* `str_*` now errors if `pattern` includes any `NA`s (@nash-delcamp-slp, #546).
* `str_view()` now displays a message when called with a zero-length character
vector (@LouisMPenrod, #497).
Expand Down
16 changes: 13 additions & 3 deletions R/dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
#'
#' @inheritParams str_detect
#' @param times Number of times to duplicate each string.
#' @param sep String to insert between each duplicate.
#' @return A character vector the same length as `string`/`times`.
#' @export
#' @examples
#' fruit <- c("apple", "pear", "banana")
#' str_dup(fruit, 2)
#' str_dup(fruit, 2, sep = " ")
#' str_dup(fruit, 1:3)
#' str_c("ba", str_dup("na", 0:5))
str_dup <- function(string, times) {
vctrs::vec_size_common(string = string, times = times)
stri_dup(string, times)
str_dup <- function(string, times, sep = NULL) {
input <- vctrs::vec_recycle_common(string = string, times = times)
check_string(sep, allow_null = TRUE)

if (is.null(sep)) {
stri_dup(input$string, input$times)
} else {
map_chr(seq_along(input$string), function(i) {
paste(rep(string[[i]], input$times[[i]]), collapse = sep)
})
}
}
5 changes: 4 additions & 1 deletion man/str_dup.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/_snaps/dup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# separator must be a single string

Code
str_dup("a", 3, sep = 1)
Condition
Error in `str_dup()`:
! `sep` must be a single string or `NULL`, not the number 1.
Code
str_dup("a", 3, sep = c("-", ";"))
Condition
Error in `str_dup()`:
! `sep` must be a single string or `NULL`, not a character vector.

18 changes: 18 additions & 0 deletions tests/testthat/test-dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ test_that("0 duplicates equals empty string", {
test_that("uses tidyverse recycling rules", {
expect_error(str_dup(1:2, 1:3), class = "vctrs_error_incompatible_size")
})

test_that("uses sep argument", {
expect_equal(str_dup("abc", 1, sep = "-"), "abc")
expect_equal(str_dup("abc", 2, sep = "-"), "abc-abc")

expect_equal(str_dup(c("a", "b"), 2, sep = "-"), c("a-a", "b-b"))
expect_equal(str_dup(c("a", "b"), c(1, 2), sep = "-"), c("a", "b-b"))

expect_equal(str_dup(character(), 1, sep = "-"), character())
expect_equal(str_dup(character(), 2, sep = "-"), character())
})

test_that("separator must be a single string", {
expect_snapshot(error = TRUE, {
str_dup("a", 3, sep = 1)
str_dup("a", 3, sep = c("-", ";"))
})
})

0 comments on commit 0b92cfb

Please sign in to comment.