Skip to content

Commit

Permalink
Add dist2km
Browse files Browse the repository at this point in the history
  • Loading branch information
HughParsonage committed Jan 30, 2019
1 parent 34174d3 commit 2ebb6c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 15 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ isAttached <- function(pkg) {
}
}


utils2km <- function(string) {
stopifnot(is.character(string),
length(string) == 1L)
# put km before m!
if (endsWith(string, "km")) {
dist_km <- sub("\\s*km$", "", string)
# use as.double here and as.numeric later to separate warning msgs
dist_km <- as.double(dist_km)
} else if (endsWith(string, "m")) {
dist_km <- sub("\\s*m$", "", string)
dist_km <- as.numeric(dist_km) / 1000
}
stopifnot(!anyNA(dist_km), is.numeric(dist_km))
dist_km
}
8 changes: 8 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
context("test-utils")

test_that("dist2km works", {
expect_equal(dist2km("12.3km", 123))
expect_equal(dist2km("12.3 km", 123))
expect_equal(dist2km("12.3m", 123/1000))
expect_equal(dist2km("12.3 m", 123/1000))
})

0 comments on commit 2ebb6c2

Please sign in to comment.