Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Created April 14, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gadenbuie/7d21aab922aec5d2b95675b7f611a256 to your computer and use it in GitHub Desktop.
Save gadenbuie/7d21aab922aec5d2b95675b7f611a256 to your computer and use it in GitHub Desktop.
version_diff <- function(old, new) {
stopifnot(length(old) == length(new))
if (length(old) > 1) {
has_names <- !is.null(names(old))
return(
mapply(version_diff, old, new, SIMPLIFY = FALSE, USE.NAMES = has_names)
)
}
as_pkg_version <- function(x) {
if (inherits(x, "package_version")) return(x)
package_version(x)
}
old <- as_pkg_version(old)
new <- as_pkg_version(new)
pad <- function(x, len) {
if (length(x) >= len) return(x)
c(x, rep(0, len - length(x)))
}
new <- unclass(new)[[1]]
old <- unclass(old)[[1]]
max_len <- max(length(new), length(old))
diff <- pad(new, max_len) - pad(old, max_len)
idx_first <- which(diff > 0)[[1]]
diff <- pad(diff[1:idx_first], 3)
package_version(paste(diff, collapse = "."))
}
version_diff(
old = c(DT = "0.17", Rcpp = "1.0.7", MASS = "7.3-51"),
new = c( "0.22", "1.0.8.3", "7.3-56")
)
#> $DT
#> [1] '0.5.0'
#>
#> $Rcpp
#> [1] '0.0.1'
#>
#> $MASS
#> [1] '0.0.5'
# https://gist.github.com/7d21aab922aec5d2b95675b7f611a256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment