From f773dc9cfbc16497587f91b847ba8f5ecc8f8370 Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Tue, 10 Dec 2024 11:35:35 +0100 Subject: [PATCH] feat: more integer conversions for ids --- R/assertions.R | 9 +++++---- R/indicators.R | 9 +++++---- tests/testthat/test-utils.R | 36 ++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/R/assertions.R b/R/assertions.R index f6278c4..da95619 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -31,10 +31,7 @@ is_count_or_null <- function(x) { is.null(x) || is_count(x) } -is_valid_date <- function(x) { - if (is.null(x)) { - return(TRUE) - } +is_dateish <- function(x) { if (length(x) != 1L) { return(FALSE) } @@ -45,3 +42,7 @@ is_valid_date <- function(x) { FALSE } } + +is_dateish_or_null <- function(x) { + is.null(x) || is_dateish(x) +} diff --git a/R/indicators.R b/R/indicators.R index 1be7ca9..bc0ae0e 100644 --- a/R/indicators.R +++ b/R/indicators.R @@ -305,7 +305,7 @@ wb_indicator <- function(indicator = NULL, lang = "en") { id = map_chr(data, "id"), name = map_chr(data, "name"), unit = map_chr(data, "unit"), - source_id = map_chr(data, \(x) x$source$id), + source_id = map_chr(data, \(x) x$source$id) |> as.integer(), source_value = map_chr(data, \(x) x$source$value), source_note = map_chr(data, "sourceNote"), source_organization = map_chr(data, "sourceOrganization"), @@ -315,7 +315,8 @@ wb_indicator <- function(indicator = NULL, lang = "en") { } else { NA_character_ } - }), + }) |> + as.integer(), topic_value = map_chr(data, function(x) { if (length(x$topics) > 0L && length(x$topics[[1L]]) > 0L) { x$topics[[1L]]$value @@ -373,8 +374,8 @@ wb_country_indicator <- function(indicator = "NY.GDP.MKTP.CD", stopifnot( is_string(indicator), is_character_or_null(country), nchar(country) %in% 2:3, - is_valid_date(start_date), - is_valid_date(end_date) + is_dateish_or_null(start_date), + is_dateish_or_null(end_date) ) has_start_date <- !is.null(start_date) has_end_date <- !is.null(end_date) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index c28d8e7..5b6ca50 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -35,28 +35,28 @@ test_that("format_date works", { expect_identical(format_date("", "2025"), ":2025") }) -test_that("is_valid_date works", { +test_that("is_dateish_or_null works", { # NULL input - expect_true(is_valid_date(NULL)) + expect_true(is_dateish_or_null(NULL)) # valid year - expect_true(is_valid_date(2024)) - expect_true(is_valid_date("2024")) + expect_true(is_dateish_or_null(2024)) + expect_true(is_dateish_or_null("2024")) # valid year with month - expect_true(is_valid_date("2024M01")) - expect_true(is_valid_date("2024M12")) + expect_true(is_dateish_or_null("2024M01")) + expect_true(is_dateish_or_null("2024M12")) # valid year with quarter - expect_true(is_valid_date("2024Q1")) - expect_true(is_valid_date("2024Q4")) + expect_true(is_dateish_or_null("2024Q1")) + expect_true(is_dateish_or_null("2024Q4")) # invalid lengths - expect_false(is_valid_date(c("2024", "2025"))) - expect_false(is_valid_date(c("2024M01", "2024Q1"))) + expect_false(is_dateish_or_null(c("2024", "2025"))) + expect_false(is_dateish_or_null(c("2024M01", "2024Q1"))) # invalid formats - expect_false(is_valid_date("202")) - expect_false(is_valid_date("2024M13")) - expect_false(is_valid_date("2024Q5")) - expect_false(is_valid_date("2024M00")) - expect_false(is_valid_date("2024X01")) - expect_false(is_valid_date("24M01")) - expect_false(is_valid_date("2024-Q1")) - expect_false(is_valid_date("2024/M01")) + expect_false(is_dateish_or_null("202")) + expect_false(is_dateish_or_null("2024M13")) + expect_false(is_dateish_or_null("2024Q5")) + expect_false(is_dateish_or_null("2024M00")) + expect_false(is_dateish_or_null("2024X01")) + expect_false(is_dateish_or_null("24M01")) + expect_false(is_dateish_or_null("2024-Q1")) + expect_false(is_dateish_or_null("2024/M01")) })