Skip to content

Commit

Permalink
Added unit_is_parseable.R file
Browse files Browse the repository at this point in the history
  • Loading branch information
nanu1605 committed Aug 2, 2022
1 parent 01b3704 commit 1eb3e95
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ authors:
orcid: 'https://orcid.org/0000-0002-5845-5628'
- given-names: Ayush Pradad
- given-names: Shashank Singh
- given-names: Tanishq Jain
- affiliation: Boston University
given-names: Alexis Helgeson
- affiliation: Pacific Northwest National Laboratory / University of Maryland
Expand Down
2 changes: 2 additions & 0 deletions base/utils/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Authors@R: c(person("Mike", "Dietze", role = c("aut"),
email = "[email protected]"),
person("Chris", "Black", role = c("aut"),
email = "[email protected]"),
person("Tanishq", "Jain", role = c("aut"),
email = "[email protected]"),
person("University of Illinois, NCSA", role = c("cph")))
Description: The Predictive Ecosystem Carbon Analyzer
(PEcAn) is a scientific workflow management tool that
Expand Down
1 change: 1 addition & 0 deletions base/utils/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export(trait.lookup)
export(transformstats)
export(tryl)
export(ud_convert)
export(unit_is_parseable)
export(units_are_equivalent)
export(vecpaste)
export(zero.truncate)
Expand Down
17 changes: 17 additions & 0 deletions base/utils/R/unit_is_parseable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##' Check whether a unit string is parseable
##'
##' Function will replace the now-unmaintained `udunits2::ud.is.parseable`
##' @author Tanishq Jain
##'
##' @param u1 A character string representing a type of units
##'
##' @return TRUE if the units is parseable, FALSE otherwise.
##' @export
unit_is_parseable <- function(u1){
tryCatch({
if(units::as_units(u1))
return(TRUE)
},
error = function(e) FALSE
)
} # unit_is_parseable
20 changes: 20 additions & 0 deletions base/utils/man/unit_is_parseable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions base/utils/tests/testthat/test-unit_is_parseable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("parseable unit", {
expect_equal(unit_is_parseable("miles"), TRUE)
expect_equal(unit_is_parseable(" K "), TRUE)
expect_equal(unit_is_parseable("10cm"), TRUE)
expect_equal(unit_is_parseable("m/s"), TRUE)
expect_equal(unit_is_parseable("kg"), TRUE)
})

test_that("Non-paresable unit", {
expect_equal(unit_is_parseable("fake"), FALSE)
expect_equal(unit_is_parseable("gk"), FALSE)
expect_equal(unit_is_parseable(NULL), FALSE)
expect_equal(unit_is_parseable("Not parseable"), FALSE)
expect_equal(unit_is_parseable("Loading"), FALSE)
})

# This differ from `udunits2::ud.is.parseable`
#test_that("incompatiable unit to parse", {
# expect_error(unit_is_parseable(""))
#})

0 comments on commit 1eb3e95

Please sign in to comment.