Skip to content

Commit

Permalink
account for nested list scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
k-doering-NOAA committed Dec 19, 2024
1 parent df831f7 commit 58c37b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
50 changes: 33 additions & 17 deletions R/rm_dollar_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ rm_dollar_sign <- function(file,
replacement = replace_no_backtick_in_quotes,
mod_lines
)
if (allow_recursive) {
mod_lines <- recursive_replace(pattern = pattern_no_backtick_in_quotes,
replace = replace_no_backtick_in_quotes, lines = mod_lines, max_loops = max_loops)
} else {
if (length(grep(pattern_no_backtick_in_quotes, x = mod_lines)) > 0) {
warning(
"There are lists in lists in quotes, but allow_recursive = FALSE, so not all",
"dollar sign operators were converted."
)
}
}

pattern_no_backtick <-
"([[:alnum:]]|\\.|\\_|\\]|\\(|\\))\\$([[:alnum:]]+)(([[:alnum:]]|\\.|\\_)*)(\\s|[[:punct:]]|$)"
Expand All @@ -84,23 +95,8 @@ rm_dollar_sign <- function(file,
mod_lines
)
if (allow_recursive) {
# get rid of $ when there are lists in lists.
ind <- 1
while (length(grep(pattern_no_backtick, x = mod_lines)) > 0 &
ind <= max_loops) {
ind <- ind + 1
mod_lines <- gsub(
pattern = pattern_no_backtick,
replacement = replace_no_backtick,
mod_lines
)
}
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
warning(
"max_loops was set too low to replace all instances of dollar ",
"sign references."
)
}
mod_lines <- recursive_replace(pattern = pattern_no_backtick,
replace = replace_no_backtick, lines = mod_lines, max_loops = max_loops)
} else {
if (length(grep(pattern_no_backtick, x = mod_lines)) > 0) {
warning(
Expand All @@ -114,3 +110,23 @@ rm_dollar_sign <- function(file,
}
mod_lines
}

recursive_replace <- function(pattern, replace, lines, max_loops) {
# get rid of $ when there are lists in lists.
ind <- 1
while (length(grep(pattern, x = lines)) > 0 && ind <= max_loops) {
ind <- ind + 1
lines <- gsub(
pattern = pattern,
replacement = replace,
lines
)
}
if (length(grep(pattern, x = lines)) > 0) {
warning(
"max_loops was set too low to replace all instances of dollar ",
"sign references in quotes."
)
}
lines
}
6 changes: 4 additions & 2 deletions tests/testthat/test-rm_dollar_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ test_that("rm dollar sign works", {
"x$mylist$my_col$YetAnotherCol",
"x$mylist$my_col$`1_somename`",
"x()$my_name <- y$test",
"\"test object$item with additional quoted text\""
"\"test object$item with additional quoted text\"",
"\"test object$item1$item2 with additional quoted text and nested list\""
)
expect_output <- c(
"x[[\"my_name\"]] <- y[[\"test\"]]",
Expand All @@ -33,7 +34,8 @@ test_that("rm dollar sign works", {
"x[[\"mylist\"]][[\"my_col\"]][[\"YetAnotherCol\"]]",
"x[[\"mylist\"]][[\"my_col\"]][[\"1_somename\"]]",
"x()[[\"my_name\"]] <- y[[\"test\"]]",
"\"test object[['item']] with additional quoted text\""
"\"test object[['item']] with additional quoted text\"",
"\"test object[['item1']][['item2']] with additional quoted text and nested list\""
)
writeLines(test_text, "test_rm_dollar_sign.txt")
new_text <- rm_dollar_sign(
Expand Down

0 comments on commit 58c37b3

Please sign in to comment.