Skip to content

Commit 39d52e0

Browse files
author
Ryan Patrick Kyle
committed
🐪 DRY up key validation logic
1 parent 79b0faa commit 39d52e0

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

R/dash.R

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -815,15 +815,7 @@ Dash <- R6::R6Class(
815815
# specify a custom index string
816816
# ------------------------------------------------------------------------
817817
index_string = function(string) {
818-
required_keys <- c("app_entry", "config", "scripts")
819-
820-
keys_present <- vapply(required_keys, function(x) grepl(x, string), logical(1))
821-
822-
if (!all(keys_present)) {
823-
stop(sprintf("Did you forget to include %s in your index string?",
824-
paste(names(keys_present[keys_present==FALSE]), collapse = ", ")))
825-
}
826-
private$custom_index <- string
818+
private$custom_index <- validate_keys(string)
827819
},
828820

829821
# ------------------------------------------------------------------------
@@ -838,14 +830,7 @@ Dash <- R6::R6Class(
838830
template = sub(key, kwargs[[name]], template)
839831
}
840832

841-
required_keys <- c("app_entry", "config", "scripts")
842-
843-
checks <- sapply(requiredKeys, function(x) grepl(x, names(kwargs)))
844-
845-
if (FALSE %in% checks) {
846-
stop(sprintf("Did you forget to include %s in your index string?",
847-
paste(requiredKeys[!checks], collapse = ", ")))
848-
}
833+
validate_keys(names(kwargs))
849834

850835
private$template_index <- template
851836
},

R/utils.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,3 +1287,16 @@ interpolate_str <- function(index_template, ...) {
12871287
}
12881288
return(template)
12891289
}
1290+
1291+
validate_keys <- function(string) {
1292+
required_keys <- c("app_entry", "config", "scripts")
1293+
1294+
keys_present <- vapply(required_keys, function(x) grepl(x, string), logical(1))
1295+
1296+
if (!all(keys_present)) {
1297+
stop(sprintf("Did you forget to include %s in your index string?",
1298+
paste(names(keys_present[keys_present==FALSE]), collapse = ", ")))
1299+
} else {
1300+
return(string)
1301+
}
1302+
}

0 commit comments

Comments
 (0)