R/checkers.R

Defines functions are_same_length has_names has_metachr

Documented in has_metachr has_names

#' @title Check for metacharacters
#' @description Test whether a string contains metacharacters or not.
#' @param x A character string.
#' @return A boolean.
#' @export
has_metachr <- function(x) {
  assert_that(is.string(x))
  grepl(r"{[\\\[\](){}|?$^*+]}", x, perl = TRUE)
}


#' @title Check for names
#' @description Test whether an object has names or not.
#' @param x Object to test.
#' @param all If `TRUE`, will test whether all elements have a name.
#' @export
has_names <- function(x, all = FALSE) {
  .names <- names(x)
  if (is.null(.names)) return(FALSE)
  if (is_false(all)) return(TRUE)
  all(nzchar(.names))
}


are_same_length <- function(x, y) length(x) == length(y)
arnaudgallou/toolkit documentation built on Nov. 25, 2022, 5:42 p.m.