Skip to content

Commit

Permalink
Reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia-Kosenkov committed Aug 3, 2024
1 parent 31a0f46 commit 686fd88
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions R/import-standalone-obj-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,44 +185,18 @@ vec_or_scalar_type_friendly <- function(x, value) {
)
}


vec_type_friendly <- function(x, length = FALSE) {
if (!rlang::is_vector(x)) {
rlang::abort("`x` must be a vector.")
}
type <- typeof(x)
n_dim <- length(dim(x))

add_length <- function(type) {
if (length && !n_dim) {
paste0(type, sprintf(" of length %s", length(x)))
} else {
type
}
}

if (type == "list") {
if (n_dim < 2) {
return(add_length("a list"))
} else if (is.data.frame(x)) {
return("a data frame")
} else if (n_dim == 2) {
return("a list matrix")
} else {
return("a list array")
}
return(.list_type_friendly(x, type, length, n_dim))
}

type <- switch(type,
logical = "a logical %s",
integer = "an integer %s",
numeric = ,
double = "a double %s",
complex = "a complex %s",
character = "a character %s",
raw = "a raw %s",
type = paste0("a ", type, " %s")
)
type <- .get_message_pattern(type)

if (n_dim < 2) {
kind <- "vector"
Expand All @@ -236,10 +210,43 @@ vec_type_friendly <- function(x, length = FALSE) {
if (n_dim >= 2) {
out
} else {
add_length(out)
.with_length(x, out, length, n_dim)
}
}

.list_type_friendly <- function(x, type, length, n_dim) {
if (n_dim < 2) {
return(.with_length(x, "a list", length, n_dim))
} else if (is.data.frame(x)) {
return("a data frame")
} else if (n_dim == 2) {
return("a list matrix")
} else {
return("a list array")
}
}

.with_length <- function(x, type, length, n_dim) {
if (length && !n_dim) {
paste0(type, sprintf(" of length %s", length(x)))
} else {
type
}
}

.get_message_pattern <- function(type) {
switch(type,
logical = "a logical %s",
integer = "an integer %s",
numeric = ,
double = "a double %s",
complex = "a complex %s",
character = "a character %s",
raw = "a raw %s",
type = paste0("a ", type, " %s")
)
}

.rlang_as_friendly_type <- function(type) {
switch(type,
list = "a list",
Expand Down

0 comments on commit 686fd88

Please sign in to comment.