R/tr_trunc_neatly.R

Defines functions tr_trunc_neatly

Documented in tr_trunc_neatly

#' Cleanly trim long strings
#'
#' @param x Input string to trim to desired length, appending an ellipsis to the
#'   end, and without splitting words
#' @param l Desired length at which to trim strings
#'
#' @return Character string
#' @export
#'
#' @importFrom purrr map_chr
#'
#' @description Trims the input string to the desired length, appending an
#'   ellipsis to the end, without splitting in the middle of a word.
#'
#' @references None.
#' @seealso <https://www.github.com/travis-m-blimkie/tRavis>
#'
#' @examples
#' tr_trunc_neatly("This is a test string", l = 17)
#'
tr_trunc_neatly <- function(x, l = 60) {
  map_chr(
    x,
    ~if (is.na(.x)) {
      return(NA_character_)
    } else if (nchar(.x) <= l) {
      return(.x)
    } else {
      shortened <- gsub(
        x = substr(
          x = as.character(.x),
          start = 1,
          stop = l
        ),
        pattern = " ([^ ]*)$",
        replacement = "..."
      )
      return(shortened)
    }
  )
}
travis-m-blimkie/tRavis documentation built on April 9, 2024, 11:45 p.m.