Skip to content

Commit

Permalink
add enclose-argument
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 24, 2022
1 parent cdc25ed commit e986a8d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling
Version: 0.4.1.3
Version: 0.4.1.4
Authors@R: c(
person("Dominique", "Makowski", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-5375-9967", Twitter = "@Dom_Makowski")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ MAJOR CHANGES
* Given his continued and significant contributions to the package,
Etienne Bacher (@etiennebacher) is now included as an author.

CHANGES

* Some of the text formatting helpers (like `text_concatenate()`) gain an
`enclose` argument, to wrap text elements with surrounding characters.

NEW FUNCTIONS

* `row_to_colnames()` and `colnames_to_row()` to move a row to column names,
Expand Down
33 changes: 27 additions & 6 deletions R/format_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' @param sep Separator.
#' @param last Last separator.
#' @param n The number of characters to find.
#' @param enclose Character that will be used to wrap elements of `text`, so
#' these can be, e.g., enclosed with quotes or backticks. If `NULL` (default),
#' text elements will not be enclosed.
#' @param ... Other arguments to be passed to or from other functions.
#'
#' @return A character string.
Expand All @@ -23,6 +26,7 @@
#'
#' # Smart concatenation
#' text_concatenate(c("First", "Second", "Last"))
#' text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`")
#'
#' # Remove parts of string
#' text_remove(c("one!", "two", "three!"), "!")
Expand All @@ -34,8 +38,8 @@
#' # Paste with optional separator
#' text_paste(c("A", "", "B"), c("42", "42", "42"))
#' @export
format_text <- function(text, sep = ", ", last = " and ", width = NULL, ...) {
text_wrap(text_concatenate(text, sep = sep, last = last), width = width)
format_text <- function(text, sep = ", ", last = " and ", width = NULL, enclose = NULL, ...) {
text_wrap(text_concatenate(text, sep = sep, last = last, enclose = enclose), width = width)
}


Expand All @@ -58,22 +62,39 @@ text_lastchar <- function(text, n = 1) {

#' @rdname format_text
#' @export
text_concatenate <- function(text, sep = ", ", last = " and ") {
text_concatenate <- function(text, sep = ", ", last = " and ", enclose = NULL) {
text <- text[text != ""]
if (length(text) && !is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) {
text <- paste0(enclose, text, enclose)
}
if (length(text) == 1) {
text
s <- text
} else {
s <- paste0(utils::head(text, -1), collapse = sep)
s <- paste0(c(s, utils::tail(text, 1)), collapse = last)
s
}
s
}


#' @rdname format_text
#' @export
text_paste <- function(text, text2 = NULL, sep = ", ", ...) {
text_paste <- function(text, text2 = NULL, sep = ", ", enclose = NULL, ...) {
if (!is.null(text2)) {
if (!is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) {
text <- sapply(text, function(i) {
if (i != "") {
i <- paste0(enclose, i, enclose)
}
i
})
text2 <- sapply(text2, function(i) {
if (i != "") {
i <- paste0(enclose, i, enclose)
}
i
})
}
paste0(text, ifelse(text == "" | text2 == "", "", sep), text2)
}
}
Expand Down
18 changes: 15 additions & 3 deletions man/format_text.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-format_text.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
test_that("text formatting helpers work as expected", {
expect_equal(
format_text(c("A very long First", "Some similar long Second", "Shorter Third", "More or less long Fourth", "And finally the Last"), width = 20),
"A very long First,\nSome similar long\nSecond, Shorter\nThird, More or less\nlong Fourth and And\nfinally the Last\n"
)

expect_equal(
format_text(c("A very long First", "Some similar long Second", "Shorter Third", "More or less long Fourth", "And finally the Last"), last = " or ", enclose = "`", width = 20),
"`A very long\nFirst`, `Some\nsimilar long\nSecond`, `Shorter\nThird`, `More or\nless long Fourth`\nor `And finally the\nLast`\n"
)

expect_equal(
text_fullstop(c("something", "something else.")),
c("something.", "something else.")
Expand All @@ -15,6 +25,11 @@ test_that("text formatting helpers work as expected", {
"First, Second and Last"
)

expect_equal(
text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`"),
"`First`, `Second` or `Last`"
)

expect_equal(
text_remove(c("one!", "two", "three!"), "!"),
c("one", "two", "three")
Expand All @@ -27,4 +42,9 @@ test_that("text formatting helpers work as expected", {
text_paste(c("A", "", "B"), c("42", "42", "42")),
c("A, 42", "42", "B, 42")
)

expect_equal(
text_paste(c("A", "", "B"), c("42", "42", "42"), enclose = "`"),
c("`A`, `42`", "`42`", "`B`, `42`")
)
})

0 comments on commit e986a8d

Please sign in to comment.