R/randomSlug.R

Defines functions idSlug randomSlug

Documented in idSlug randomSlug

#' Generate a random slug
#'
#' idSlug is a convenience function with swapped argument order.
#'
#' @param x Length of slug
#' @param id If not NULL, prepended to slug (separated with a dash) as id; in
#' that case, it's also braces and a hash is added.
#' @param chars Characters to sample from
#'
#' @return A character value.
#' @rdname randomSlug
#' @export
#'
#' @examples randomSlug();
#' idSlug("identifier");
randomSlug <- function(x = 10,
                       id = NULL,
                       chars = c(letters, LETTERS, 0:9)) {
  res <- do.call(paste0, as.list(sample(chars, x)));
  if (!is.null(id)) {
    res <- paste0(" {#", id, "-", res ,"}");
  }
  return(res);
}

#' @rdname randomSlug
#' @export
idSlug = function(id = NULL,
                  x = 10,
                  chars = c(letters, LETTERS, 0:9)) {
  return(randomSlug(x=x, id=id, chars=chars));
}

Try the justifier package in your browser

Any scripts or data that you put into this service are public.

justifier documentation built on March 7, 2023, 6:59 p.m.