Skip to content

Commit 5a9e3dd

Browse files
committed
adding space and indent functions
1 parent 90f8ba7 commit 5a9e3dd

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export(str_flatten_comma)
3232
export(str_glue)
3333
export(str_glue_data)
3434
export(str_ilike)
35+
export(str_indent)
3536
export(str_interp)
3637
export(str_length)
3738
export(str_like)
@@ -48,6 +49,7 @@ export(str_replace)
4849
export(str_replace_all)
4950
export(str_replace_na)
5051
export(str_sort)
52+
export(str_space)
5153
export(str_split)
5254
export(str_split_1)
5355
export(str_split_fixed)

R/indent.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#' Indent multiple strings
2+
#'
3+
#' @description
4+
#' `str_indent()` adds a customizable amount of spaces (or other characters)
5+
#' before every word, as if it was tabbed, and puts every word on a new line.
6+
#' @param indent_level controls the number of characters to add,
7+
#' @param indent_character controls what character is placed.
8+
#' @returns a newline separated list of words with a 'tab' character in between
9+
#' @seealso [str_spaceout()] to add spaces between words.
10+
#' @export
11+
#' @examples
12+
#' x <- c("why", "video", "cross", "extra", "deal", "authority")
13+
#' # Default options
14+
#' str_indent(x)
15+
#' # Extra indentation
16+
#' str_indent(x, indent_level = 4)
17+
#' # Using periods for visibility
18+
#' str_indent(x, indent_character = '.')
19+
#' # Both parameters changed
20+
#' str_indent(x, indent_level = 6, indent_character = '-')
21+
22+
str_indent <- function(x, indent_level = 3, indent_character = ' ', collapse = NULL) {
23+
24+
if (indent_level < 0) {
25+
stop("Invalid indent level, indent_level must be zero or positive.")
26+
}
27+
# Create a "tab" string out of 3 spaces or other character
28+
indent <- paste0(rep(indent_character, indent_level), collapse = '')
29+
new_strings <- gsub("(?m)^", indent, x, perl = TRUE)
30+
31+
# If vector not atomic, turns it into a single string separated by newlines
32+
if (!is.null(collapse)) {
33+
new_strings <- paste0(new_strings, collapse = collapse)
34+
}
35+
36+
return(new_strings)
37+
# perl must be TRUE to use regular expressions in baser
38+
}
39+

R/space.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#' Split up strings
2+
#'
3+
#' @description
4+
#' `str_space()` adds a space wherever a lowercase letter is followed by an
5+
#' uppercase letter, with modifiable functionality for special cases.
6+
#'
7+
#' @param split_style controls the placement of splits and can be
8+
#' modified to the following styles:
9+
#' `acronyms`: splits at lowercase-uppercase and between two uppercase letters
10+
#' followed by lowercase,
11+
#' `allcaps`: splits between all consecutive uppercase letters
12+
#' `default`: splits only when a lowercase letter is succeeded by an uppercase letter
13+
#' `everything`: splits between every character
14+
#'
15+
#' @param split_char controls the character that splits words.
16+
#' This defaults to space but can be changed to anything.
17+
#'
18+
#' @seealso [str_indent()] to add tabs between words.
19+
#' @export
20+
#' @examples
21+
#' input <- "XMLHTTPRequestAndHTMLParser"
22+
#' # Default splitting
23+
#' str_space(input, split_style = "default")
24+
#' # Split acronyms
25+
#' str_space(input, split_style = "acronyms")
26+
#' # Split all consecutive uppercase letters
27+
#' str_space(input, split_style = "allcaps")
28+
#' # Split every character
29+
#' str_space(input, split_style = "everything")
30+
#'
31+
32+
str_space <- function(x, split_style = "default", split_char = " ") {
33+
if (split_style == "acronyms") {
34+
# Splits at lowercase-uppercase and between two uppercase letters followed by lowercase
35+
str_replace_all(x, "(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", split_char)
36+
} else if (split_style == "allcaps") {
37+
# Splits between all consecutive uppercase letters
38+
str_replace_all(x, "(?<=\\p{Lu})(?=\\p{Lu})", split_char)
39+
} else if (split_style == "default") {
40+
# Splits only when a lowercase letter is succeeded by an uppercase letter
41+
str_replace_all(x, "(?<=[a-z])(?=[A-Z])", split_char)
42+
} else if (split_style == "everything") {
43+
# Splits between every character
44+
str_replace_all(x, "(?<=.)(?=.)", split_char)
45+
} else {
46+
stop("Invalid split_style. Choose from 'default', 'acronyms', 'allcaps', or 'everything'.")
47+
}
48+
}

man/str_indent.Rd

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

man/str_space.Rd

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

tests/testthat/test-indent.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
test_that("indenting works on vectors of different lengths", {
2+
expect_equal(str_indent(c("why", "video", "cross", "extra", "deal", "authority")),
3+
c(" why", " video", " cross", " extra", " deal", " authority"))
4+
5+
expect_equal(str_indent(c("why", "video", "cross", "extra", "deal", "authority")),
6+
c(" why", " video", " cross", " extra", " deal", " authority"))
7+
expect_equal(str_indent("Jan\nFeb\nMar"), " Jan\n Feb\n Mar")
8+
})
9+
10+
test_that("indenting works on vectors of different lengths with optional indent character", {
11+
expect_equal(str_indent(c("why", "video", "cross", "extra", "deal", "authority"),
12+
indent_character = ".", collapse = "\n"), "...why\n...video\n...cross\n...extra\n...deal\n...authority")
13+
expect_equal(str_indent("Jan\nFeb\nMar", indent_character = "."), "...Jan\n...Feb\n...Mar")
14+
})
15+
16+
test_that("indenting with collapse works", {
17+
18+
expect_equal(str_indent(c("why", "video", "cross", "extra", "deal", "authority"),
19+
indent_character = ".", collapse = "\n"),
20+
"...why\n...video\n...cross\n...extra\n...deal\n...authority")
21+
})

tests/testthat/test-space.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test_that("str_space works", {
2+
expect_equal(str_space("XMLHTTPRequestAndHTMLParser"), "XMLHTTPRequest And HTMLParser")
3+
expect_equal(str_space("XMLHTTPRequestAndHTMLParser", "allcaps"), "X M L H T T P RequestAndH T M L Parser")
4+
expect_equal(str_space("XMLHTTPRequestAndHTMLParser", "acronyms"), "XMLHTTP Request And HTML Parser")
5+
expect_equal(str_space("XMLHTTPRequestAndHTMLParser", "everything"), "X M L H T T P R e q u e s t A n d H T M L P a r s e r")
6+
})

0 commit comments

Comments
 (0)