|
@ -2,32 +2,39 @@ |
|
|
#' |
|
|
#' |
|
|
#' @param x string |
|
|
#' @param x string |
|
|
#' @param replacement replace spaces with replacement character, defaults to `-` |
|
|
#' @param replacement replace spaces with replacement character, defaults to `-` |
|
|
#' @param remove remove characters that match this regex, defaults to `NULL` (no deleting) |
|
|
#' @param remove remove characters that match this JavaScript regex, defaults to `NULL` (no deleting). |
|
|
|
|
|
#' This should be a [JavaScript bare regex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) |
|
|
|
|
|
#' including the `//` and any modifiers. e.g. to remove all R's (upper and lower) from a slug you should |
|
|
|
|
|
#' specify `remove = "/[rR]/g`. |
|
|
#' @param lower convert to lower case, defaults to `TRUE` |
|
|
#' @param lower convert to lower case, defaults to `TRUE` |
|
|
#' @param strict strip special characters except replacement, defaults to `TRUE` |
|
|
#' @param strict strip special characters except replacement, defaults to `TRUE` |
|
|
#' @export |
|
|
#' @export |
|
|
#' @examples |
|
|
#' @examples |
|
|
#' slugify("R is great!") |
|
|
#' slugify("R is great!") # "r-is-great" |
|
|
|
|
|
#' slugify("R is great!", replacement = "@@") # "r@@is@@great" |
|
|
|
|
|
#' slugify("R is great!", remove = "/[Rr]/g") # "is-geat" |
|
|
slugify <- function(x, replacement = "-", remove = NULL, lower = TRUE, strict = TRUE) { |
|
|
slugify <- function(x, replacement = "-", remove = NULL, lower = TRUE, strict = TRUE) { |
|
|
|
|
|
|
|
|
if (!is.null(remove)) { |
|
|
if (!is.null(remove)) { |
|
|
|
|
|
|
|
|
.pkgenv$ctx$call( |
|
|
sprintf( |
|
|
"slugify", x[1], |
|
|
"var res = slugify(%s, { replacement: %s, remove: %s, lower: %s, strict: %s });\n", |
|
|
list( |
|
|
shQuote(x), shQuote(replacement), |
|
|
replacement = "-", |
|
|
remove, |
|
|
remove = remove[1], |
|
|
if (lower) "true" else "false", |
|
|
lower = lower[1], |
|
|
if (strict) "true" else "false" |
|
|
strict = strict[1] |
|
|
) -> stmt |
|
|
) |
|
|
|
|
|
) |
|
|
.pkgenv$ctx$eval(V8::JS(stmt)) |
|
|
|
|
|
|
|
|
|
|
|
.pkgenv$ctx$get("res") |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
|
|
|
|
|
|
.pkgenv$ctx$call( |
|
|
.pkgenv$ctx$call( |
|
|
"slugify", x[1], |
|
|
"slugify", x[1], |
|
|
list( |
|
|
list( |
|
|
replacement = "-", |
|
|
replacement = replacement, |
|
|
lower = lower[1], |
|
|
lower = lower[1], |
|
|
strict = strict[1] |
|
|
strict = strict[1] |
|
|
) |
|
|
) |
|
|