Skip to content

Commit

Permalink
Optimize get_class() function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed May 23, 2021
1 parent fbaf698 commit c404b07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
8 changes: 2 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,8 @@ na_as <- function(x) {
}

get_class <- function(x) {
foo <- function(x) {
class(x)[1]
}

if (is.list(x) || is.data.frame(x)) {
vapply(x, foo, character(1))
if (is.list(x)) {
vapply(x, function(x) class(x)[1], character(1))
} else {
class(x)[1]
}
Expand Down
14 changes: 5 additions & 9 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,13 @@ test_that("na_as() | general test", {
})

test_that("get_class() | general test", {
foo <- function(x) {
class(x)[1]
}
test <- function(x) class(x)[1]

expect_equal(get_class(1), "numeric")

x <- datasets::iris
expect_equal(get_class(x), vapply(x, foo, character(1)))

x <- list(a = 1, b = 1)
expect_equal(get_class(x), vapply(x, foo, character(1)))
expect_equal(get_class(datasets::iris),
vapply(datasets::iris, test, character(1)))
expect_equal(get_class(list(a = 1, b = 1)),
vapply(list(a = 1, b = 1), test, character(1)))
})

test_that("fix_character() | general test", {
Expand Down

0 comments on commit c404b07

Please sign in to comment.