R/utils.R

Defines functions format_date_range collapse_or format_param to_snake_case na_if_empty clean_strings `%&&%` `%||%`

`%||%` <- function(lhs, rhs) {
  if (is.null(lhs)) rhs else lhs # nolint
}

`%&&%` <- function(lhs, rhs) {
  if (is.null(lhs)) lhs else rhs
}

clean_strings <- function(data) {
  for (i in seq_along(data)) {
    x <- data[[i]]
    if (is.character(x)) {
      data[[i]] <- na_if_empty(trimws(x))
    }
  }
  data
}

na_if_empty <- function(x) {
  replace(x, !nzchar(x), NA_character_)
}

to_snake_case <- function(x) {
  x <- gsub("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))", "_\\1", x, perl = TRUE)
  x <- tolower(gsub("[^[:alnum:]]+", "_", x))
  gsub("^_+|_+$", "", x)
}

format_param <- function(x) {
  paste(x %||% "all", collapse = ";")
}

collapse_or <- function(x) {
  if (length(x) == 0L) {
    return()
  }
  paste0(x, collapse = "^")
}

format_date_range <- function(start_date, end_date) {
  if (!is.null(start_date) && !is.null(end_date)) {
    paste(start_date, end_date, sep = ":")
  } else {
    start_date %||% end_date
  }
}

Try the worldbank package in your browser

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

worldbank documentation built on Nov. 17, 2026, 5:06 p.m.