-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_pref.R
25 lines (24 loc) · 1.02 KB
/
parse_pref.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#' Parse prefecture codes or names
#'
#' @param x A `character` vector of prefecture codes or names.
#' @param strict A scalar logical. Whether to require the code to have 1 or 2
#' digits. By default, `TRUE`.
#'
#' @return A `jpcity_pref` object.
#'
#' @export
parse_pref <- function(x,
strict = TRUE) {
if (!is_scalar_logical(strict)) {
cli::cli_abort("{.arg strict} must be a scalar logical.")
}
pref_code <- dplyr::case_when(stringr::str_detect(x, "^\\d{1,2}$") ~ quiet_as_integer(x),
!strict & stringr::str_detect(x, "^\\d+$") ~ x |>
stringr::str_sub(1, 2) |>
quiet_as_integer(),
.default = vec_slice(string_pref_name$pref_code,
vec_match(extract_pref_name(x), string_pref_name$string_pref_name)))
data <- vec_slice(string_pref_name,
vec_match(pref_code, string_pref_name$pref_code))
pref(data)
}