Skip to content

Commit e986a8d

Browse files
committed
add enclose-argument
1 parent cdc25ed commit e986a8d

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling
4-
Version: 0.4.1.3
4+
Version: 0.4.1.4
55
Authors@R: c(
66
person("Dominique", "Makowski", , "[email protected]", role = "aut",
77
comment = c(ORCID = "0000-0001-5375-9967", Twitter = "@Dom_Makowski")),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ MAJOR CHANGES
1515
* Given his continued and significant contributions to the package,
1616
Etienne Bacher (@etiennebacher) is now included as an author.
1717

18+
CHANGES
19+
20+
* Some of the text formatting helpers (like `text_concatenate()`) gain an
21+
`enclose` argument, to wrap text elements with surrounding characters.
22+
1823
NEW FUNCTIONS
1924

2025
* `row_to_colnames()` and `colnames_to_row()` to move a row to column names,

R/format_text.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#' @param sep Separator.
1111
#' @param last Last separator.
1212
#' @param n The number of characters to find.
13+
#' @param enclose Character that will be used to wrap elements of `text`, so
14+
#' these can be, e.g., enclosed with quotes or backticks. If `NULL` (default),
15+
#' text elements will not be enclosed.
1316
#' @param ... Other arguments to be passed to or from other functions.
1417
#'
1518
#' @return A character string.
@@ -23,6 +26,7 @@
2326
#'
2427
#' # Smart concatenation
2528
#' text_concatenate(c("First", "Second", "Last"))
29+
#' text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`")
2630
#'
2731
#' # Remove parts of string
2832
#' text_remove(c("one!", "two", "three!"), "!")
@@ -34,8 +38,8 @@
3438
#' # Paste with optional separator
3539
#' text_paste(c("A", "", "B"), c("42", "42", "42"))
3640
#' @export
37-
format_text <- function(text, sep = ", ", last = " and ", width = NULL, ...) {
38-
text_wrap(text_concatenate(text, sep = sep, last = last), width = width)
41+
format_text <- function(text, sep = ", ", last = " and ", width = NULL, enclose = NULL, ...) {
42+
text_wrap(text_concatenate(text, sep = sep, last = last, enclose = enclose), width = width)
3943
}
4044

4145

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

5963
#' @rdname format_text
6064
#' @export
61-
text_concatenate <- function(text, sep = ", ", last = " and ") {
65+
text_concatenate <- function(text, sep = ", ", last = " and ", enclose = NULL) {
6266
text <- text[text != ""]
67+
if (length(text) && !is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) {
68+
text <- paste0(enclose, text, enclose)
69+
}
6370
if (length(text) == 1) {
64-
text
71+
s <- text
6572
} else {
6673
s <- paste0(utils::head(text, -1), collapse = sep)
6774
s <- paste0(c(s, utils::tail(text, 1)), collapse = last)
68-
s
6975
}
76+
s
7077
}
7178

7279

7380
#' @rdname format_text
7481
#' @export
75-
text_paste <- function(text, text2 = NULL, sep = ", ", ...) {
82+
text_paste <- function(text, text2 = NULL, sep = ", ", enclose = NULL, ...) {
7683
if (!is.null(text2)) {
84+
if (!is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) {
85+
text <- sapply(text, function(i) {
86+
if (i != "") {
87+
i <- paste0(enclose, i, enclose)
88+
}
89+
i
90+
})
91+
text2 <- sapply(text2, function(i) {
92+
if (i != "") {
93+
i <- paste0(enclose, i, enclose)
94+
}
95+
i
96+
})
97+
}
7798
paste0(text, ifelse(text == "" | text2 == "", "", sep), text2)
7899
}
79100
}

man/format_text.Rd

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-format_text.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
test_that("text formatting helpers work as expected", {
2+
expect_equal(
3+
format_text(c("A very long First", "Some similar long Second", "Shorter Third", "More or less long Fourth", "And finally the Last"), width = 20),
4+
"A very long First,\nSome similar long\nSecond, Shorter\nThird, More or less\nlong Fourth and And\nfinally the Last\n"
5+
)
6+
7+
expect_equal(
8+
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),
9+
"`A very long\nFirst`, `Some\nsimilar long\nSecond`, `Shorter\nThird`, `More or\nless long Fourth`\nor `And finally the\nLast`\n"
10+
)
11+
212
expect_equal(
313
text_fullstop(c("something", "something else.")),
414
c("something.", "something else.")
@@ -15,6 +25,11 @@ test_that("text formatting helpers work as expected", {
1525
"First, Second and Last"
1626
)
1727

28+
expect_equal(
29+
text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`"),
30+
"`First`, `Second` or `Last`"
31+
)
32+
1833
expect_equal(
1934
text_remove(c("one!", "two", "three!"), "!"),
2035
c("one", "two", "three")
@@ -27,4 +42,9 @@ test_that("text formatting helpers work as expected", {
2742
text_paste(c("A", "", "B"), c("42", "42", "42")),
2843
c("A, 42", "42", "B, 42")
2944
)
45+
46+
expect_equal(
47+
text_paste(c("A", "", "B"), c("42", "42", "42"), enclose = "`"),
48+
c("`A`, `42`", "`42`", "`B`, `42`")
49+
)
3050
})

0 commit comments

Comments
 (0)