Skip to content

Commit 73402d7

Browse files
authored
Support is.nan(), is.finite(), and is.infinite() (#243)
* Add `is.nan()`, `is.finite()`, and `is.infinite()` for clock-rcrds * Add `is.nan()`, `is.finite()`, and `is.infinite()` for weekday * NEWS bullet
1 parent 9b09eec commit 73402d7

14 files changed

+238
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ S3method(vec_cast,clock_year_month_day.clock_year_month_day)
498498
S3method(vec_cast,clock_year_month_weekday.clock_year_month_weekday)
499499
S3method(vec_cast,clock_year_quarter_day.clock_year_quarter_day)
500500
S3method(vec_cast,clock_zoned_time.clock_zoned_time)
501+
S3method(vec_math,clock_rcrd)
502+
S3method(vec_math,clock_weekday)
501503
S3method(vec_proxy,clock_duration)
502504
S3method(vec_proxy,clock_iso_year_week_day)
503505
S3method(vec_proxy,clock_time_point)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
* New `invalid_remove()` for removing invalid dates. This is just a wrapper
1010
around `x[!invalid_detect(x)]`, but works nicely with the pipe (#229).
11+
12+
* All clock types now support `is.nan()`, `is.finite()`, and `is.infinite()`
13+
(#235).
1114

1215
# clock 0.3.1
1316

R/rcrd.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,28 @@ names.clock_rcrd <- function(x) {
3737

3838
vec_slice(x, i)
3939
}
40+
41+
# ------------------------------------------------------------------------------
42+
43+
#' @export
44+
vec_math.clock_rcrd <- function(.fn, .x, ...) {
45+
switch(
46+
.fn,
47+
is.nan = clock_rcrd_is_nan(.x),
48+
is.finite = clock_rcrd_is_finite(.x),
49+
is.infinite = clock_rcrd_is_infinite(.x),
50+
NextMethod()
51+
)
52+
}
53+
54+
clock_rcrd_is_nan <- function(x) {
55+
vec_rep(FALSE, vec_size(x))
56+
}
57+
58+
clock_rcrd_is_finite <- function(x) {
59+
!vec_equal_na(x)
60+
}
61+
62+
clock_rcrd_is_infinite <- function(x) {
63+
vec_rep(FALSE, vec_size(x))
64+
}

R/weekday.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,31 @@ add_days.clock_weekday <- function(x, n, ...) {
489489

490490
# ------------------------------------------------------------------------------
491491

492+
#' @export
493+
vec_math.clock_weekday <- function(.fn, .x, ...) {
494+
switch(
495+
.fn,
496+
is.nan = weekday_is_nan(.x),
497+
is.finite = weekday_is_finite(.x),
498+
is.infinite = weekday_is_infinite(.x),
499+
NextMethod()
500+
)
501+
}
502+
503+
weekday_is_nan <- function(x) {
504+
vec_rep(FALSE, vec_size(x))
505+
}
506+
507+
weekday_is_finite <- function(x) {
508+
!vec_equal_na(x)
509+
}
510+
511+
weekday_is_infinite <- function(x) {
512+
vec_rep(FALSE, vec_size(x))
513+
}
514+
515+
# ------------------------------------------------------------------------------
516+
492517
validate_encoding <- function(encoding) {
493518
if (!is_string(encoding, string = c("western", "iso"))) {
494519
abort("`encoding` must be one of \"western\" or \"iso\".")

tests/testthat/test-duration.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,21 @@ test_that("precision: can get the precision", {
279279
test_that("precision: can only be called on durations", {
280280
expect_snapshot_error(duration_precision(sys_days(0)))
281281
})
282+
283+
# ------------------------------------------------------------------------------
284+
# vec_math()
285+
286+
test_that("is.nan() works", {
287+
x <- duration_years(c(1, NA))
288+
expect_identical(is.nan(x), c(FALSE, FALSE))
289+
})
290+
291+
test_that("is.finite() works", {
292+
x <- duration_years(c(1, NA))
293+
expect_identical(is.finite(x), c(TRUE, FALSE))
294+
})
295+
296+
test_that("is.infinite() works", {
297+
x <- duration_years(c(1, NA))
298+
expect_identical(is.infinite(x), c(FALSE, FALSE))
299+
})

tests/testthat/test-gregorian-year-day.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,21 @@ test_that("throws known classed error", {
335335
expect_snapshot_error(invalid_resolve(year_day(2019, 366)))
336336
expect_error(invalid_resolve(year_day(2019, 366)), class = "clock_error_invalid_date")
337337
})
338+
339+
# ------------------------------------------------------------------------------
340+
# vec_math()
341+
342+
test_that("is.nan() works", {
343+
x <- year_day(c(2019, NA))
344+
expect_identical(is.nan(x), c(FALSE, FALSE))
345+
})
346+
347+
test_that("is.finite() works", {
348+
x <- year_day(c(2019, NA))
349+
expect_identical(is.finite(x), c(TRUE, FALSE))
350+
})
351+
352+
test_that("is.infinite() works", {
353+
x <- year_day(c(2019, NA))
354+
expect_identical(is.infinite(x), c(FALSE, FALSE))
355+
})

tests/testthat/test-gregorian-year-month-day.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,21 @@ test_that("throws known classed error", {
566566
expect_snapshot_error(invalid_resolve(year_month_day(2019, 2, 31)))
567567
expect_error(invalid_resolve(year_month_day(2019, 2, 31)), class = "clock_error_invalid_date")
568568
})
569+
570+
# ------------------------------------------------------------------------------
571+
# vec_math()
572+
573+
test_that("is.nan() works", {
574+
x <- year_month_day(c(2019, NA))
575+
expect_identical(is.nan(x), c(FALSE, FALSE))
576+
})
577+
578+
test_that("is.finite() works", {
579+
x <- year_month_day(c(2019, NA))
580+
expect_identical(is.finite(x), c(TRUE, FALSE))
581+
})
582+
583+
test_that("is.infinite() works", {
584+
x <- year_month_day(c(2019, NA))
585+
expect_identical(is.infinite(x), c(FALSE, FALSE))
586+
})

tests/testthat/test-gregorian-year-month-weekday.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,21 @@ test_that("strict mode can be activated", {
247247
local_options(clock.strict = TRUE)
248248
expect_snapshot_error(invalid_resolve(year_month_weekday(2019, 1, 1, 1)))
249249
})
250+
251+
# ------------------------------------------------------------------------------
252+
# vec_math()
253+
254+
test_that("is.nan() works", {
255+
x <- year_month_weekday(c(2019, NA))
256+
expect_identical(is.nan(x), c(FALSE, FALSE))
257+
})
258+
259+
test_that("is.finite() works", {
260+
x <- year_month_weekday(c(2019, NA))
261+
expect_identical(is.finite(x), c(TRUE, FALSE))
262+
})
263+
264+
test_that("is.infinite() works", {
265+
x <- year_month_weekday(c(2019, NA))
266+
expect_identical(is.infinite(x), c(FALSE, FALSE))
267+
})

tests/testthat/test-iso-year-week-day.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,21 @@ test_that("strict mode can be activated", {
223223
local_options(clock.strict = TRUE)
224224
expect_snapshot_error(invalid_resolve(iso_year_week_day(2019, 1)))
225225
})
226+
227+
# ------------------------------------------------------------------------------
228+
# vec_math()
229+
230+
test_that("is.nan() works", {
231+
x <- iso_year_week_day(c(2019, NA))
232+
expect_identical(is.nan(x), c(FALSE, FALSE))
233+
})
234+
235+
test_that("is.finite() works", {
236+
x <- iso_year_week_day(c(2019, NA))
237+
expect_identical(is.finite(x), c(TRUE, FALSE))
238+
})
239+
240+
test_that("is.infinite() works", {
241+
x <- iso_year_week_day(c(2019, NA))
242+
expect_identical(is.infinite(x), c(FALSE, FALSE))
243+
})

tests/testthat/test-naive-time.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,21 @@ test_that("ptype is correct", {
584584
expect_identical(vec_ptype(x), expect)
585585
}
586586
})
587+
588+
# ------------------------------------------------------------------------------
589+
# vec_math()
590+
591+
test_that("is.nan() works", {
592+
x <- naive_days(c(2019, NA))
593+
expect_identical(is.nan(x), c(FALSE, FALSE))
594+
})
595+
596+
test_that("is.finite() works", {
597+
x <- naive_days(c(2019, NA))
598+
expect_identical(is.finite(x), c(TRUE, FALSE))
599+
})
600+
601+
test_that("is.infinite() works", {
602+
x <- naive_days(c(2019, NA))
603+
expect_identical(is.infinite(x), c(FALSE, FALSE))
604+
})

0 commit comments

Comments
 (0)