| 1 |
|
| 2 |
truthy <- function(value, default = FALSE) { |
| 3 |
|
| 4 |
# https://github.com/rstudio/renv/issues/1558 |
| 5 |
if (is.call(value)) { |
| 6 |
value <- tryCatch(renv_dependencies_eval(value), error = identity) |
| 7 |
if (inherits(value, "error")) |
| 8 |
return(default) |
| 9 |
} |
| 10 |
|
| 11 |
# skip empty vectors |
| 12 |
if (length(value) == 0L) |
| 13 |
return(default) |
| 14 |
|
| 15 |
# handle symbols |
| 16 |
if (is.symbol(value)) |
| 17 |
value <- as.character(value) |
| 18 |
|
| 19 |
# only look at first element in vector |
| 20 |
value <- value[[1L]] |
| 21 |
|
| 22 |
# handle some non-character types up-front |
| 23 |
if (is.call(value)) |
| 24 |
return(default) |
| 25 |
else if (is.na(value)) |
| 26 |
return(default) |
| 27 |
else if (!is.character(value)) |
| 28 |
return(as.logical(value)) |
| 29 |
|
| 30 |
# check for known truthy / falsy values |
| 31 |
if (value %in% c("TRUE", "True", "true", "T", "1")) |
| 32 |
TRUE |
| 33 |
else if (value %in% c("FALSE", "False", "false", "F", "0")) |
| 34 |
FALSE |
| 35 |
else |
| 36 |
default |
| 37 |
|
| 38 |
} |