TimTaylor / ympes

https://timtaylor.github.io/ympes/
GNU General Public License v3.0
1 stars 0 forks source link

feat: Additional assertions #1

Open TimTaylor opened 2 weeks ago

TimTaylor commented 2 weeks ago
assert_range <- function(
    x,
    ...,
    .arg = deparse(substitute(x)),
    .call = sys.call(-1L),
    .subclass = NULL
) {
    .assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
    if (!is.numeric(x) || length(x) != 2L || anyNA(x)) {
        msg <- sprintf("`%s` must be a non-missing numeric vector of length 2.", .arg)
        .stop(msg, ..., .call = .call, .subclass = .subclass)
    }
    if (x[2L] < x[1L]) {
        msg <- sprintf("`%s` must be non-decreasing.", .arg)
        .stop(msg, ..., .call = .call, .subclass = .subclass)
    }
}
assert_scalar_percentage <- function(
    x,
    ...,
    .arg = deparse(substitute(x)),
    .call = sys.call(-1L),
    .subclass = NULL
) {
    .assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
    if (!(is.numeric(x) && length(x) == 1L) || is.na(x) || x < 0 || x > 1) {
        msg <- sprintf("`%s` must be a non-missing, scalar numeric vector between 0 and 1.", .arg)
        .stop(msg, ..., .call = .call, .subclass = .subclass)
    }
}
TimTaylor commented 4 days ago

maybe but I'm not sure??