R/slugify-r.R

Defines functions slugify `%na%`

Documented in slugify

`%na%` <- function(a, b) if (is.na(a)) b else a

#' Native R slugify (with the help of {stringi})
#'
#' @param x string to slugify
#' @param repl what to replace spaces with
#' @param lower lowercase final output?
#' @export
slugify <- function(x, repl = "-", lower = TRUE) {

  x <- stri_replace_all_fixed(x, names(slugify_charmap), slugify_charmap, vectorize_all = FALSE)
  x <- stri_replace_all_regex(x, "[^\\P{P}-]", "")
  x <- stri_trim_both(x)
  x <- stri_replace_all_regex(x, "[[:space:]]+", repl)

  if (lower) (x <- stri_trans_tolower(x))

  x

}
hrbrmstr/slugify documentation built on May 24, 2021, 4:23 a.m.