Skip to content

Commit

Permalink
fix: 🐛 regex works on more than one =~ (#176)
Browse files Browse the repository at this point in the history
## Description

There was a bug in the regex that made it not detect/replace text when
there was more than one `=~`.
  • Loading branch information
lwjohnst86 authored Dec 20, 2024
1 parent 8a84677 commit caed7b8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/get-algorithm.R → R/get-algorithm-logic.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ get_algorithm_logic <- function(criteria, algorithm_logic = algorithm) {
stringr::str_replace_all("AND", "&") |>
stringr::str_replace_all("OR", "|") |>
# regex are defined with '=~', so convert them into a stringr function.
stringr::str_replace_all("(\\(?)([a-zA-Z0-9_]+)\\)? \\=\\~ ('.*')", "\\1stringr::str_detect(\\2, \\3)")
stringr::str_replace_all("([a-zA-Z0-9_]+) \\=\\~ '(.*?)'", "stringr::str_detect(\\1, '\\2')")
}
7 changes: 5 additions & 2 deletions tests/testthat/test-get-algorithm-logic.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
test_algorithm_logic <- data.frame(
name = c("hba1c", "podiatrist_services", "gld"),
name = c("hba1c", "podiatrist_services", "gld", "some_gld"),
logic = c(
"(analysiscode == 'NPU27300' AND value >= 48) OR (analysiscode == 'NPU03835' AND value >= 6.5)",
"(speciale =~ '^54') AND (barnmak != 0)",
"atc =~ '^A10'"
"atc =~ '^A10'",
"atc =~ '^A10' AND !(atc =~ '^(A10BJ|A10D)')"
)
)

Expand All @@ -21,4 +22,6 @@ test_that("`and` logic and regex within parentheses are converted to R logic", {
# i.e., the regex is within a parenthesis
get_algorithm_logic("podiatrist_services", test_algorithm_logic) |>
expect_equal("(stringr::str_detect(speciale, '^54')) & (barnmak != 0)")
get_algorithm_logic("some_gld", test_algorithm_logic) |>
expect_equal("stringr::str_detect(atc, '^A10') & !(stringr::str_detect(atc, '^(A10BJ|A10D)'))")
})

0 comments on commit caed7b8

Please sign in to comment.