R/bstr_case.R

Defines functions bstr_switch_case bstr_to_lower bstr_to_upper

Documented in bstr_switch_case bstr_to_lower bstr_to_upper

#' Convert case of a bstring sequence.
#'
#' @inheritParams class_bstr
#' @examples
#' temp <- dstr_rand_seq(1, 10, "[atgcATGC]", seed = 1)
#' c(
#'   temp,
#'   upper = bstr_to_upper(temp),
#'   lower = bstr_to_lower(temp),
#'   switch = bstr_switch_case(temp)
#' )
#'
#' @name case
NULL

#' @rdname case
#' @export
bstr_to_upper <- function(bstrobj) {
  bstrobj <- as_bstr(bstrobj)
  at <- attributes(bstrobj)

  bstrobj <- stringr::str_to_upper(bstrobj)

  attributes(bstrobj) <- at
  bstrobj
}

#' @rdname case
#' @export
bstr_to_lower <- function(bstrobj) {
  bstrobj <- as_bstr(bstrobj)
  at <- attributes(bstrobj)

  bstrobj <- stringr::str_to_lower(bstrobj)

  attributes(bstrobj) <- at
  bstrobj
}

#' @rdname case
#' @export
bstr_switch_case <- function(bstrobj) {
  bstrobj <- as_bstr(bstrobj)
  at <- attributes(bstrobj)

  bstrobj <- bstrobj %>% stringr::str_extract_all(".")
  lower_c <- bstrobj %>% purrr::map(~ stringr::str_detect(.x, "[[:lower:]]"))
  upper_c <- bstrobj %>% purrr::map(~ stringr::str_detect(.x, "[[:upper:]]"))

  bstrobj <-
    bstrobj %>%
    purrr::map2(lower_c, ~ ifelse(.y, stringr::str_to_upper(.x), .x)) %>%
    purrr::map2(upper_c, ~ ifelse(.y, stringr::str_to_lower(.x), .x)) %>%
    purrr::map_chr(~ paste0(.x, collapse = ""))

  attributes(bstrobj) <- at
  bstrobj
}
t-arae/bstringr documentation built on March 18, 2021, 3:08 a.m.