R/assertions.R

Defines functions is_dateish_or_null is_dateish is_count_or_null is_count is_string_or_null is_string is_character_or_null is_character is_bool

is_bool <- function(x) {
  is.logical(x) && length(x) == 1L
}

is_character <- function(x) {
  is.character(x) && !anyNA(x) && length(x) > 0L
}

is_character_or_null <- function(x) {
  is.null(x) || is_character(x)
}

is_string <- function(x, pattern = NULL, ...) {
  ok <- is.character(x) && length(x) == 1L && !is.na(x)
  if (ok && !is.null(pattern)) {
    ok <- grepl(pattern, x, ...)
  }
  ok
}

is_string_or_null <- function(x, pattern = NULL, ...) {
  is.null(x) || is_string(x, pattern, ...)
}

is_count <- function(x) {
  is.numeric(x) && length(x) == 1L && !is.na(x) && as.integer(x) == x && x > 0L
}

is_count_or_null <- function(x) {
  is.null(x) || is_count(x)
}

is_dateish <- function(x) {
  if (length(x) != 1L) {
    return(FALSE)
  }
  x <- as.character(x)
  if (grepl("^\\d{4}(M(0[1-9]|1[0-2])|Q[1-4])?$", x)) {
    TRUE
  } else {
    FALSE
  }
}

is_dateish_or_null <- function(x) {
  is.null(x) || is_dateish(x)
}

Try the worldbank package in your browser

Any scripts or data that you put into this service are public.

worldbank documentation built on April 4, 2025, 1:35 a.m.