Skip to content

Commit 54d729f

Browse files
authored
Implement invalid_remove() (#241)
1 parent 901709f commit 54d729f

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ S3method(invalid_detect,clock_year_day)
324324
S3method(invalid_detect,clock_year_month_day)
325325
S3method(invalid_detect,clock_year_month_weekday)
326326
S3method(invalid_detect,clock_year_quarter_day)
327+
S3method(invalid_remove,clock_calendar)
327328
S3method(invalid_resolve,clock_calendar)
328329
S3method(invalid_resolve,clock_iso_year_week_day)
329330
S3method(invalid_resolve,clock_year_day)
@@ -622,6 +623,7 @@ export(get_year)
622623
export(invalid_any)
623624
export(invalid_count)
624625
export(invalid_detect)
626+
export(invalid_remove)
625627
export(invalid_resolve)
626628
export(is_duration)
627629
export(is_iso_year_week_day)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# clock (development version)
22

3+
* New `invalid_remove()` for removing invalid dates. This is just a wrapper
4+
around `x[!invalid_detect(x)]`, but works nicely with the pipe (#229).
5+
36
# clock 0.3.1
47

58
* Parsing into a date-time type that is coarser than the original string is now

R/invalid.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
#' - `invalid_count()`: Returns a single integer containing the number of
6565
#' invalid dates.
6666
#'
67+
#' - `invalid_remove()`: Returns `x` with invalid dates removed.
68+
#'
6769
#' - `invalid_resolve()`: Returns `x` with invalid dates resolved using the
6870
#' `invalid` strategy.
6971
#'
@@ -90,6 +92,9 @@
9092
#' # But `"previous-day"` here does not!
9193
#' x_previous_day[1] < x_previous_day[2]
9294
#'
95+
#' # Remove invalid dates entirely
96+
#' invalid_remove(x)
97+
#'
9398
#' y <- year_quarter_day(2019, 1, 90:92)
9499
#' y
95100
#'
@@ -139,6 +144,19 @@ invalid_count.clock_calendar <- function(x) {
139144

140145
# ------------------------------------------------------------------------------
141146

147+
#' @rdname clock-invalid
148+
#' @export
149+
invalid_remove <- function(x) {
150+
UseMethod("invalid_remove")
151+
}
152+
153+
#' @export
154+
invalid_remove.clock_calendar <- function(x) {
155+
x[!invalid_detect(x)]
156+
}
157+
158+
# ------------------------------------------------------------------------------
159+
142160
#' @rdname clock-invalid
143161
#' @export
144162
invalid_resolve <- function(x, ..., invalid = NULL) {

man/clock-invalid.Rd

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/invalid.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# errors on unsupported types
2+
3+
no applicable method for 'invalid_remove' applied to an object of class "c('double', 'numeric')"
4+

tests/testthat/test-invalid.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ------------------------------------------------------------------------------
2+
# invalid_remove()
3+
4+
test_that("can remove invalid dates from calendar types", {
5+
x <- year_month_day(2020, 2, 28:30)
6+
expect_identical(invalid_remove(x), x[1:2])
7+
8+
x <- year_day(2019, 365:366)
9+
expect_identical(invalid_remove(x), x[1L])
10+
})
11+
12+
test_that("errors on unsupported types", {
13+
expect_snapshot_error(invalid_remove(1))
14+
})

0 commit comments

Comments
 (0)