File: ids.R

package info (click to toggle)
r-cran-ids 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 352 kB
  • sloc: sh: 13; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 1,070 bytes parent folder | download
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
26
27
28
29
30
31
32
## A future version of this will keep a pool so that duplicates are
## excluded.  For now we'll just rely on the reasonably large pool.

#' Generic id generating function
#' @title Generic id generating function
#'
#' @param n number of ids to return.  If \code{NULL}, it instead
#'   returns the generating function
#'
#' @param ... A number of character vectors
#'
#' @param vals A list of character vectors, \emph{instead} of \code{...}
#'
#' @param style Style to join words with.  Can be one of "Pascal",
#'   "camel", "snake", "kebab", "dot", "title", "sentence", "lower",
#'   "upper", and "constant".
#'
#' @return Either a character vector of length \code{n}, or a
#'   function of one argument if \code{n} is \code{NULL}
#'
#' @export
#' @author Rich FitzJohn
#' @examples
#' # For an example, please see the vignette
ids <- function(n, ..., vals = list(...), style = "snake") {
  combine <- make_combine(style)
  force(vals)
  gen <- function(n = 1) {
    combine(vapply(vals, sample, character(n), n, replace = TRUE))
  }
  if (is.null(n)) gen else gen(n)
}