R/utils.R

Defines functions remove_something_impl ensure_empty return_list_if_empty glubort cat_line

cat_line <- function(x, .envir = caller_env(), indent = 0) {
  indent <- strrep(" ", 2 * indent)
  line <- paste0(indent, glue::glue(x, .envir = .envir), "\n")
  cat(line)
}

glubort <- function(x, .envir = caller_env()) {
  abort(glue::glue(x, .envir = .envir))
}

return_list_if_empty <- function(x, .envir = caller_env()) {
  if (is_empty(x)) {
    exec(return, list(), .env = .envir)
  }
  invisible(x)
}

ensure_empty <- function(x, message, .envir = caller_env(), .add_values = TRUE) {
  if (!is_empty(x)) {
    if (all(is.character(x)) && .add_values) {
      x <- paste0("`", x, "`")
      x <- paste(x, collapse = ", ")
      message <- paste(message, x, sep = ": ")
    }
    glubort(message, .envir = .envir)
  }
}

remove_something_impl <- function(old, ..., .abort_message) {
  syms <- exprs(...)
  not_sym <- purrr::discard(syms, is_symbol)
  if (!is_empty(not_sym)) {
    abort(.abort_message)
  }
  nms <- intersect(as.character(syms), names(old))
  to_remove <- rep_along(zap(), along = nms)
  names(to_remove) <- nms

  out <- list_modify(old, !!!to_remove)
  return_list_if_empty(out)
  out
}
shunsambongi/planner documentation built on Aug. 19, 2022, 9:57 a.m.