From 2518c523130d957e5c5e42999e90c5f5e7d13d83 Mon Sep 17 00:00:00 2001 From: Joe Thorley Date: Fri, 12 Mar 2021 13:17:25 -0800 Subject: [PATCH] - Added `unique.hms()` function. --- NAMESPACE | 1 + R/unique.R | 6 ++++++ man/hms-package.Rd | 2 +- tests/testthat/test-unique.R | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 R/unique.R create mode 100644 tests/testthat/test-unique.R diff --git a/NAMESPACE b/NAMESPACE index 1b9cc97..37dc7d8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ S3method(as_hms,default) S3method(c,hms) S3method(format,hms) S3method(print,hms) +S3method(unique,hms) S3method(vec_cast,hms) S3method(vec_cast.POSIXct,hms) S3method(vec_cast.POSIXlt,hms) diff --git a/R/unique.R b/R/unique.R new file mode 100644 index 0000000..877b009 --- /dev/null +++ b/R/unique.R @@ -0,0 +1,6 @@ +#' @export +unique.hms <- function(x, incomparables = FALSE, ...) { + x <- as.numeric(x) + x <- unique(x, incomparables = incomparables, ...) + new_hms(x) +} diff --git a/man/hms-package.Rd b/man/hms-package.Rd index de4ed5e..4c8f89a 100644 --- a/man/hms-package.Rd +++ b/man/hms-package.Rd @@ -11,7 +11,7 @@ Implements an S3 class for storing and formatting time-of-day values, based on the 'difftime' class. } \details{ -\ifelse{html}{\out{Stable lifecycle}}{\strong{Stable}} +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}} } \seealso{ Useful links: diff --git a/tests/testthat/test-unique.R b/tests/testthat/test-unique.R new file mode 100644 index 0000000..edf6000 --- /dev/null +++ b/tests/testthat/test-unique.R @@ -0,0 +1,7 @@ +test_that("unique", { + expect_identical(unique(new_hms(numeric(0))), new_hms(numeric(0))) + expect_identical(unique(new_hms(NA_real_)), new_hms(NA_real_)) + expect_identical(unique(new_hms(101)), new_hms(101)) + expect_identical(unique(new_hms(c(101, 101, 99, 101))), new_hms(c(101, 99))) + expect_identical(unique(new_hms(c(101, NA_real_, 101, 99, 101))), new_hms(c(101, NA_real_, 99))) +})