R/utils.R

Defines functions js_null package_js dquote squote scalar to_json `%||%` vlapply vcapply

vcapply <- function(X, FUN, ...) {
  vapply(X, FUN, character(1), ...)
}


vlapply <- function(X, FUN, ...) {
  vapply(X, FUN, logical(1), ...)
}


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


to_json <- function(x, auto_unbox = FALSE, digits = NA, ...) {
  V8::JS(jsonlite::toJSON(x, auto_unbox = auto_unbox, digits = digits, ...))
}


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


squote <- function(x) {
  sprintf("'%s'", x)
}


dquote <- function(x) {
  sprintf('"%s"', x)
}


package_js <- function(name) {
  readLines(system.file(name, package = "odin.js", mustWork = TRUE),
            warn = FALSE)
}


js_null <- function() {
  V8::JS("null")
}
mrc-ide/odin.js documentation built on Nov. 2, 2022, 2:27 p.m.