diff --git a/DESCRIPTION b/DESCRIPTION index 1f2f962..35f06e9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: slugify Type: Package Title: Create slug strings for SEO -Version: 0.1.0 -Date: 2020-06-26 +Version: 0.1.1 +Date: 2020-06-29 Authors@R: c( person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5670-2640")) diff --git a/NEWS.md b/NEWS.md index 9b4679b..f61598a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,2 +1,6 @@ +0.1.1 +* Fixes #1 (h/t @mpjashby) +* `remove` now works like it should + 0.1.0 * Initial release diff --git a/R/slugify.R b/R/slugify.R index 76b1886..e7a8eb1 100644 --- a/R/slugify.R +++ b/R/slugify.R @@ -2,32 +2,39 @@ #' #' @param x string #' @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 strict strip special characters except replacement, defaults to `TRUE` #' @export #' @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) { if (!is.null(remove)) { - .pkgenv$ctx$call( - "slugify", x[1], - list( - replacement = "-", - remove = remove[1], - lower = lower[1], - strict = strict[1] - ) - ) + sprintf( + "var res = slugify(%s, { replacement: %s, remove: %s, lower: %s, strict: %s });\n", + shQuote(x), shQuote(replacement), + remove, + if (lower) "true" else "false", + if (strict) "true" else "false" + ) -> stmt + + .pkgenv$ctx$eval(V8::JS(stmt)) + + .pkgenv$ctx$get("res") } else { .pkgenv$ctx$call( "slugify", x[1], list( - replacement = "-", + replacement = replacement, lower = lower[1], strict = strict[1] ) diff --git a/README.Rmd b/README.Rmd index c4bc6fc..b1a2ef2 100644 --- a/README.Rmd +++ b/README.Rmd @@ -43,6 +43,10 @@ packageVersion("slugify") ```{r ex-01} slugify("R is great!") + +slugify("R is great!", replacement = "@@") + +slugify("R is great!", remove = "/[Rr]/g") ``` ## slugify Metrics diff --git a/README.md b/README.md index c6ed157..626e22b 100644 --- a/README.md +++ b/README.md @@ -53,20 +53,28 @@ library(slugify) # current version packageVersion("slugify") -## [1] '0.1.0' +## [1] '0.1.1' ``` ``` r slugify("R is great!") ## [1] "r-is-great" + +slugify("R is great!", replacement = "@@") +## [1] "r@@is@@great" + +slugify("R is great!", remove = "/[Rr]/g") +## [1] "is-geat" ``` ## slugify Metrics | Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | | :--- | -------: | --: | --: | ---: | ----------: | ---: | -------: | ---: | -| R | 4 | 0.8 | 33 | 0.79 | 13 | 0.43 | 20 | 0.39 | -| Rmd | 1 | 0.2 | 9 | 0.21 | 17 | 0.57 | 31 | 0.61 | +| R | 4 | 0.8 | 33 | 0.75 | 15 | 0.44 | 25 | 0.45 | +| Rmd | 1 | 0.2 | 11 | 0.25 | 19 | 0.56 | 31 | 0.55 | + +clock Package Metrics for slugify ## Code of Conduct diff --git a/inst/tinytest/test_slugify.R b/inst/tinytest/test_slugify.R index 9d913e9..d1d1cbb 100644 --- a/inst/tinytest/test_slugify.R +++ b/inst/tinytest/test_slugify.R @@ -1,3 +1,9 @@ library(slugify) -expect_true(slugify("R is great!") == "r-is-great") \ No newline at end of file +expect_true(slugify("R is great!") == "r-is-great") + +expect_true(slugify("R is great!", lower = FALSE) == "R-is-great") + +expect_true(slugify("R is great!", replacement = "@") == "r@is@great") + +expect_true(slugify("R is great!", remove = "/[Rr]/g") == "is-geat") diff --git a/man/slugify.Rd b/man/slugify.Rd index a956a2c..3ab6dca 100644 --- a/man/slugify.Rd +++ b/man/slugify.Rd @@ -13,7 +13,10 @@ slugify(x, replacement = "-", remove = NULL, lower = TRUE, strict = TRUE) \item{replacement}{replace spaces with replacement character, defaults to \code{-}} -\item{remove}{remove characters that match this regex, defaults to \code{NULL} (no deleting)} +\item{remove}{remove characters that match this JavaScript regex, defaults to \code{NULL} (no deleting). +This should be a \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions}{JavaScript bare regex} +including the \verb{//} and any modifiers. e.g. to remove all R's (upper and lower) from a slug you should +specify \verb{remove = "/[rR]/g}.} \item{lower}{convert to lower case, defaults to \code{TRUE}} @@ -24,7 +27,9 @@ Most blogging platforms turn title phrases into slugs for better SEO. Tools are provided to turn phrases into slugs. } \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" } \seealso{ Useful links: