R/util.R

Defines functions list_to_sexp list_to_integer system_file read_string prepare_code uglify package_version_string scalar `%||%`

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


scalar <- function(x) {
  jsonlite::unbox(x)
}


package_version_string <- function(name) {
  as.character(utils::packageVersion(name))
}


uglify <- function(code) {
  ## This works for now but is slow; hopefully we can come up with
  ## some v8-hosted solution soon.
  ## > system2("uglifyjs", "--v8", stdout = TRUE, input = code)
  code
}


prepare_code <- function(code, pretty) {
  code <- paste(code, collapse = "\n")
  if (!pretty) {
    code <- uglify(code)
  }
  code
}


read_string <- function(path) {
  ## We need 'warn = FALSE' here to prevent warnings about missing
  ## trailing newlines (which the bundled js files in odin lack due to
  ## webpack)
  paste(readLines(path, warn = FALSE), collapse = "\n")
}


system_file <- function(path, package) {
  system.file(path, package = package, mustWork = TRUE)
}


list_to_integer <- function(x) {
  vapply(x, identity, integer(1))
}


list_to_sexp <- function(x) {
  if (is.recursive(x)) {
    as.call(lapply(x, list_to_sexp))
  } else if (is.character(x)) {
    as.name(x)
  } else {
    x
  }
}
mrc-ide/odin.api documentation built on Sept. 13, 2023, 4:01 p.m.