Skip to content

Commit

Permalink
Merge pull request #202 from jmbarbone/167-globbing
Browse files Browse the repository at this point in the history
167 globbing
  • Loading branch information
jmbarbone committed Aug 13, 2023
2 parents 192851d + 6af87bc commit c8a37ad
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export(get_recent_dir)
export(get_recent_file)
export(get_version)
export(get_warning)
export(glob)
export(has_error)
export(has_message)
export(has_warning)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# mark (development version)

* `glob()` added for basic wildcard globbing on character vectors [#167](https://github.com/jmbarbone/mark/issues/167)
* adds greater use of `{fs}` over base file functions [#160](https://github.com/jmbarbone/mark/issues/160)
* improvements in `todos()` and `fixmes()`
* File extension can now be set [#170](https://github.com/jmbarbone/mark/issues/170), which by default includes `qmd` ([#163](https://github.com/jmbarbone/mark/issues/163)) and `py` files
Expand Down
30 changes: 30 additions & 0 deletions R/glob.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Wildcard globbing
#'
#' Helper function for globbing character vectors
#'
#' @param x A vector of characters
#' @param pattern Wildcard globbing pattern
#' @param value,... Additional parameters passed to `grep`. Note: `value` is by
#' default `TRUE`; when `NA`, `...` is passed to `grepl`.
#' @examples
#' x <- c("apple", "banana", "peach", "pear", "orange")
#' glob(x, "*e")
#' glob(x, "pea*", value = FALSE)
#' glob(x, "*an*", value = NA)
#'
#' path <- system.file("R", package = "mark")
#' glob(list.files(path), "r*")
#' @export
glob <- function(x, pattern = NULL, value = TRUE, ...) {
pattern <- utils::glob2rx(pattern)
params <- list(...)
params$x <- x
params$pattern <- pattern

if (isNA(value)) {
do.call(grepl, params)
} else {
params$value <- value
do.call(grep, params)
}
}
28 changes: 28 additions & 0 deletions man/glob.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-glob.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("glob() works", {
x <- c("apple", "banana", "peach", "pear", "orange")
obj <- glob(x, "*e")
exp <- c("apple", "orange")
expect_identical(obj, exp)

obj <- glob(x, "pea*", value = FALSE)
exp <- c(3L, 4L)
expect_identical(obj, exp)

obj <- glob(x, "*an*", value = NA)
exp <- c(FALSE, TRUE, FALSE, FALSE, TRUE)
expect_identical(obj, exp)
})

0 comments on commit c8a37ad

Please sign in to comment.