R/zzz.R

Defines functions valid_path nullable has_class is_boolean is_strings is_string is_number

is_number <- function(x) {
  is.numeric(x) || tryCatch(as.numeric(x), warning = function(e) FALSE)
}

is_string <- function(x, allow_na = FALSE) {
  is.character(x) && length(x) == 1 && (allow_na || !is.na(x))
}

is_strings <- function(x, allow_na = FALSE) {
  is.character(x) && length(x) > 0 && (allow_na || all(!is.na(x)))
}

is_boolean <- function(x) {
  is.logical(x) && length(x) > 0
}

has_class <- function(x, class) {
  if (isNamespaceLoaded("methods")) {
    methods::is(x, class)
  } else {
    inherits(x, class)
  }
}

nullable <- function(fun) {
  function(x, ...) {
    is.null(x) || fun(x, ...)
  }
}

valid_path <- function(path) {
  is_string(path) && dir.exists(path)
}
jimhester/gmailr documentation built on Nov. 4, 2023, 1:02 p.m.