Skip to content

Commit

Permalink
Export is_bool()
Browse files Browse the repository at this point in the history
Closes #695
  • Loading branch information
lionel- committed Jun 12, 2019
1 parent 3ddbd97 commit 51b7b5d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -265,6 +265,7 @@ export(is_bare_raw)
export(is_bare_string)
export(is_bare_vector)
export(is_binary_lang)
export(is_bool)
export(is_box)
export(is_bytes)
export(is_call)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
@@ -1,6 +1,11 @@

# rlang 0.3.99.9000

* `is_bool()` is a scalar type predicate that checks whether its input
is a single `TRUE` or `FALSE`. Like `is_string()`, it returns
`FALSE` when the input is missing. This is useful for type-checking
function arguments (#695).

* Lists of quosures now have pillar methods for display in tibbles.

* The performance of `exec()` has been improved. It is now on the same
Expand Down
11 changes: 10 additions & 1 deletion R/types.R
Expand Up @@ -101,8 +101,16 @@ is_null <- function(x) {

#' Scalar type predicates
#'
#' @description
#'
#' These predicates check for a given type and whether the vector is
#' "scalar", that is, of length 1.
#'
#' In addition to the length check, `is_string()` and `is_bool()`
#' return `FALSE` if their input is missing. This is useful for
#' type-checking arguments, when your function expects a single string
#' or a single `TRUE` or `FALSE`.
#'
#' @inheritParams type-predicates
#' @param x object to be tested.
#' @seealso [type-predicates], [bare-type-predicates]
Expand Down Expand Up @@ -162,7 +170,8 @@ is_string <- function(x, string = NULL) {
#' @export
#' @rdname scalar-type-predicates
is_scalar_bytes <- is_scalar_raw

#' @export
#' @rdname scalar-type-predicates
is_bool <- function(x) {
is_logical(x, n = 1) && !is.na(x)
}
Expand Down
8 changes: 8 additions & 0 deletions man/scalar-type-predicates.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-types.R
Expand Up @@ -166,3 +166,10 @@ test_that("is_string() matches on string", {
expect_true(is_string("foo", c("bar", "foo")))
expect_false(is_string("foo", c("bar", "baz")))
})

test_that("is_bool() checks for single `TRUE` or `FALSE`", {
expect_true(is_bool(TRUE))
expect_true(is_bool(FALSE))
expect_false(is_bool(NA))
expect_false(is_bool(c(TRUE, FALSE)))
})

0 comments on commit 51b7b5d

Please sign in to comment.