Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

167 globbing #202

Merged
merged 10 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})