Skip to content

Commit 3a53e09

Browse files
committed
ymd() supports ... making it more convinient
1 parent 9ed2ee7 commit 3a53e09

File tree

7 files changed

+46
-40
lines changed

7 files changed

+46
-40
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: ymd
22
Title: Parse 'YMD' Format Number or String to Date
3-
Version: 0.0.1.9000
3+
Version: 0.0.2
44
Authors@R: c(
55
person("Xianying", "Tan", , "shrektan@126.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-6072-3521")),

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# ymd 0.1.0
2+
3+
* `ymd()` now supports `...` arguments, which is convinient for interactive use, e.g., `ymd(210101, 220201)`.
4+
15
# ymd 0.0.1
26

37
* Added a `NEWS.md` file to track changes to the package.

R/extendr-wrappers.R

+1-17
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,7 @@
88
#' @useDynLib ymd, .registration = TRUE
99
NULL
1010

11-
#' Convert 'YMD' format integer or string to Date
12-
#'
13-
#' Transform integer or strings vectors in 'YMD' format to Date objects.
14-
#' It intends to only support limited formats (no separator or one of
15-
#' '.', ' ', '-' and '/' separators). See the possible formats in examples.
16-
#'
17-
#' @param x An integer or string vector in 'YMD' format. Double
18-
#' values without the decimal part are allowed.
19-
#' @return A Date object. When the parse fails for certain input,
20-
#' the value returned would be `NA`, silently.
21-
#'
22-
#' @examples
23-
#' ymd(c(210326, 19981225))
24-
#' ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
25-
#'
26-
#' @export
27-
ymd <- function(x) .Call(wrap__ymd, x)
11+
rust_ymd <- function(x) .Call(wrap__rust_ymd, x)
2812

2913
period_begin <- function(x, unit) .Call(wrap__period_begin, x, unit)
3014

R/ymd.R

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' Convert 'YMD' format integer or string to Date
2+
#'
3+
#' Transform integer or strings vectors in 'YMD' format to Date objects.
4+
#' It intends to only support limited formats (no separator or one of
5+
#' '.', ' ', '-' and '/' separators). See the possible formats in examples.
6+
#'
7+
#' @param x An integer or string vector in 'YMD' format. Double
8+
#' values without the decimal part are allowed.
9+
#' @param ... The same as `x`. It will be merged into one vector with `x`.
10+
#' It's convinient for interactive use.
11+
#'
12+
#' @return A Date object. When the parse fails for certain input,
13+
#' the value returned would be `NA`, silently.
14+
#'
15+
#' @examples
16+
#' ymd(c(210326, 19981225))
17+
#' ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
18+
#' ymd(210420, 180322)
19+
#'
20+
#' @export
21+
ymd <- function(x, ...) {
22+
if (...length()) {
23+
x <- c(x, unlist(list(...)))
24+
}
25+
rust_ymd(x)
26+
}

man/ymd.Rd

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/src/lib.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,8 @@ fn make_rdate2(x: Vec<Option<NaiveDate>>) -> Robj {
9999
make_rdate(v)
100100
}
101101

102-
/// Convert 'YMD' format integer or string to Date
103-
///
104-
/// Transform integer or strings vectors in 'YMD' format to Date objects.
105-
/// It intends to only support limited formats (no separator or one of
106-
/// '.', ' ', '-' and '/' separators). See the possible formats in examples.
107-
///
108-
/// @param x An integer or string vector in 'YMD' format. Double
109-
/// values without the decimal part are allowed.
110-
/// @return A Date object. When the parse fails for certain input,
111-
/// the value returned would be `NA`, silently.
112-
///
113-
/// @examples
114-
/// ymd(c(210326, 19981225))
115-
/// ymd(c("2020/1/8", "20 1 7", "1998.7.1", "1990-02-03"))
116-
///
117-
/// @export
118102
#[extendr]
119-
fn ymd(x: Robj) -> Robj {
103+
fn rust_ymd(x: Robj) -> Robj {
120104
if x.inherits("Date") {
121105
return x;
122106
}
@@ -167,7 +151,7 @@ fn beop(x: Robj, unit: &str, fun: fn(&NaiveDate, period::Period) -> NaiveDate) -
167151
Some(i) => i,
168152
None => return make_rdate(vec![None; x.len()]),
169153
};
170-
let x = robj2date(ymd(x));
154+
let x = robj2date(rust_ymd(x));
171155
let out = x
172156
.iter()
173157
.map(|v| match v {
@@ -203,7 +187,7 @@ fn period_end(x: Robj, unit: &str) -> Robj {
203187
/// @export
204188
#[extendr]
205189
fn edate(ref_date: Robj, months: i32) -> Robj {
206-
let out = robj2date(ymd(ref_date))
190+
let out = robj2date(rust_ymd(ref_date))
207191
.iter()
208192
.map(|v| match v {
209193
Some(date) => Some(period::add_months(date, months)),
@@ -336,7 +320,7 @@ mod test {
336320
// See corresponding C code in `entrypoint.c`.
337321
extendr_module! {
338322
mod ymd;
339-
fn ymd;
323+
fn rust_ymd;
340324
fn period_begin;
341325
fn period_end;
342326
fn edate;

tests/testthat/test-ymd.R

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ test_that("parse short year dates correctly", {
3838
expect_equal(ymd("0098-03-05"), as.Date("0098-03-05"))
3939
expect_equal(ymd("98-3-05"), as.Date("1998-03-05"))
4040
})
41+
42+
test_that("ymd ... works", {
43+
expect_equal(ymd(210101, 220101), ymd(c(210101, 220101)))
44+
})

0 commit comments

Comments
 (0)