Skip to content

Commit

Permalink
allow the comma sep function to handle non-numeric values (#79)
Browse files Browse the repository at this point in the history
* allow the comma sep function to handle non-numeric values

* update docs

* update version to v0.4.1
  • Loading branch information
cjrace authored Aug 23, 2024
1 parent 0ae2aba commit 54cad63
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: dfeR
Title: Common DfE R tasks
Version: 0.4.0
Version: 0.4.1
Authors@R: c(
person("Cam", "Race", , "[email protected]", role = c("aut", "cre")),
person("Laura", "Selby", , "[email protected]", role = "aut"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dfeR 0.4.1

Update comma_sep() function to allow non-numeric values instead of throwing an error, now returns them unchanged.

# dfeR 0.4.0

Add function which creates a DfE R project:
Expand Down
9 changes: 3 additions & 6 deletions R/comma_sep.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#' Comma separate
#'
#' @description
#' Adds separating commas to big numbers.
#' Adds separating commas to big numbers. If a value is not numeric it will
#' return the value unchanged and as a string.
#'
#' @param number number to be comma separated
#'
#' @return string containing comma separated number
#' @return string
#' @export
#'
#' @examples
#' comma_sep(100)
#' comma_sep(1000)
#' comma_sep(3567000)
comma_sep <- function(number) {
if (!is.numeric(number)) {
stop("number must be a numeric value")
}

format(number, big.mark = ",", trim = TRUE, scientific = FALSE)
}
5 changes: 3 additions & 2 deletions man/comma_sep.Rd

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

18 changes: 5 additions & 13 deletions tests/testthat/test-comma_sep.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ test_that("comma separates", {
expect_equal(comma_sep(-1000), "-1,000")
})

test_that("rejects non-numbers", {
expect_error(
comma_sep("12"),
"number must be a numeric value"
)
expect_error(
comma_sep("test"),
"number must be a numeric value"
)
expect_error(
comma_sep(TRUE),
"number must be a numeric value"
)
test_that("handles non-numbers", {
expect_equal(comma_sep("12"), "12")
expect_equal(comma_sep("1200"), "1200")
expect_equal(comma_sep("test"), "test")
expect_equal(comma_sep(TRUE), "TRUE")
})

0 comments on commit 54cad63

Please sign in to comment.