R/utils.R

Defines functions complete_semver use_cache is_cached read_from_cache write_to_cache

Documented in complete_semver

#' Turn partial "valid" semantic version strings into a complete semver-tri or quad strings
#'
#' For MAJOR.MINOR.PATCH (semver-tri), turn `1` into `1.0.0`; `1.1` into `1.1.0`; `1.1.1`
#' into `1.1.1`. For MAJOR.MINOR.PATCH.EXTENSION (semver-quad), turn `1` into `1.0.0.0`;
#' `1.1` into `1.1.0.0`; `1.1.1.0` into `1.1.1.0`.
#'
#' Partial validity checking is performed to test if the input strings contain only
#' digits and periods. "Invalid" input is returned unscathed.
#'
#' @param x a character vector of full or partial version strings
#' @param quad (logical) if `TRUE` then a three-dot semver is returned, else a two-dot semver
#'        is returned. Default: `FALSE`.
#' @export
complete_semver <- function(x, quad = FALSE) {

  x <- stri_trim_both(x)
  x <- stri_replace_all_regex(x, "(^\\.|\\.$)", "")

  max_dots <- if (quad) 3 else 2

  purrr::map_chr(x, ~{
    if (stri_detect_regex(.x, "^[[:digit:]\\.]+$")) {
      times <- max_dots - stri_count_fixed(.x, ".")
      if (times > 0) {
        sprintf("%s%s", .x, paste0(rep(".0", times), collapse=""))
      } else {
        .x
      }
    } else {
      .x
    }
  })

}

use_cache <- function() {
  dir.exists("~/.vershist")
}

is_cached <- function(tech) {
  dir.exists("~/.vershist") &&
    file.exists(file.path("~/.vershist", sprintf("%s.rds", tech)))
}

read_from_cache <- function(tech) {
  readRDS(file.path("~/.vershist", sprintf("%s.rds", tech)))
}

write_to_cache <- function(vers_dat, tech) {
  saveRDS(vers_dat, file.path("~/.vershist", sprintf("%s.rds", tech)))
}
hrbrmstr/vershist documentation built on Feb. 3, 2020, 12:10 a.m.