diff --git a/R/get-algorithm.R b/R/get-algorithm-logic.R similarity index 85% rename from R/get-algorithm.R rename to R/get-algorithm-logic.R index 6d96ced..7e2ec98 100644 --- a/R/get-algorithm.R +++ b/R/get-algorithm-logic.R @@ -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')") } diff --git a/tests/testthat/test-get-algorithm-logic.R b/tests/testthat/test-get-algorithm-logic.R index 159545d..9c93c4b 100644 --- a/tests/testthat/test-get-algorithm-logic.R +++ b/tests/testthat/test-get-algorithm-logic.R @@ -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)')" ) ) @@ -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)'))") })