Browse Source

Fixes #1

master
boB Rudis 4 years ago
parent
commit
f32d2cb4c6
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 4
      DESCRIPTION
  2. 4
      NEWS.md
  3. 31
      R/slugify.R
  4. 4
      README.Rmd
  5. 14
      README.md
  6. 6
      inst/tinytest/test_slugify.R
  7. 9
      man/slugify.Rd

4
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"))

4
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

31
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]
)

4
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

14
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

6
inst/tinytest/test_slugify.R

@ -1,3 +1,9 @@
library(slugify)
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")

9
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:

Loading…
Cancel
Save