Skip to content

Commit

Permalink
Change leading characters in label_date_short() (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand authored Oct 21, 2024
1 parent 16036b0 commit 8ca05e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# scales (development version)

* New `label_date_short(leading)` argument to replace leading zeroes (#442)
* `breaks_pretty()` will return the input limit when it has no range (#446)
* `transform_exp()` now has more sensible breaks, available in `breaks_exp()`
(@teunbrand, #405).
Expand Down
18 changes: 15 additions & 3 deletions R/label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#' uses the current locale. Setting this argument requires stringi, and you
#' can see a complete list of supported locales with
#' [stringi::stri_locale_list()].
#' @param leading A string to replace leading zeroes with. Can be `""` to
#' disable leading characters or `"\u2007"` for figure-spaces.
#' @export
#' @examples
#' date_range <- function(start, days) {
Expand Down Expand Up @@ -53,8 +55,9 @@ label_date <- function(format = "%Y-%m-%d", tz = "UTC", locale = NULL) {
#' @export
#' @rdname label_date
#' @param sep Separator to use when combining date formats into a single string.
label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n") {
force_all(format, sep)
label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n",
leading = "0") {
force_all(format, sep, leading)

function(x) {
dt <- unclass(as.POSIXlt(x))
Expand Down Expand Up @@ -90,7 +93,16 @@ label_date_short <- function(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n")
)

format <- apply(for_mat, 1, function(x) paste(rev(x[!is.na(x)]), collapse = sep))
format(x, format)
x <- format(x, format)

if (isTRUE(leading == "0")) {
return(x)
}

# Replace leading 0s with `leading` character
x <- gsub("^0", leading, x)
x <- gsub(paste0(sep, "0"), paste0(sep, leading), x, fixed = TRUE)
x
}
}

Expand Down
9 changes: 8 additions & 1 deletion man/label_date.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-label-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ test_that("can set locale", {
expect_equal(time_format("%B", locale = "fr")(x), "janvier")
})

test_that("label_date_short can replace leading zeroes", {
x <- seq(as.Date("2024-01-01"), as.Date("2025-01-01"), by = "1 month")
labels <- label_date_short(
format = c("%Y", "%m", "%d"),
sep = "-", leading = "x"
)(x)
expect_equal(labels, c("x1-2024", paste0("x", 2:9), c(10:12), "x1-2025"))
})

test_that("date_short doesn't change unexpectedly", {
expect_snapshot({
dformat <- label_date_short()
Expand Down

0 comments on commit 8ca05e0

Please sign in to comment.