Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/replace_non_ascii.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ replace_non_ascii <- function (x, replacement = '',
remove.nonconverted = TRUE, ...) {

x <- replace_curly_quote(x)
x <- stringi::stri_trans_general(x, "latin-ascii")
x <- stringi::stri_trans_general(x, "Any-Latin; Latin-ASCII")
x <- iconv(as.character(x), "", "ASCII", "byte")
Encoding(x) <- "latin1"
x <- mgsub(x, ser, reps)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-replace_non_ascii.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
context("Checking replace_non_ascii")

test_that("replace_non_ascii transliterates Latin and non-Latin scripts to ASCII", {
x <- c("heiß", "brûlée", "Дорога", "キャンパス", "भोजन")
Encoding(x) <- "UTF-8"
expect_equal(replace_non_ascii(x), c("heiss", "brulee", "Doroga", "kyanpasu", "bhojana"))
})

test_that("replace_non_ascii with remove.nonconverted = FALSE preserves unmapped characters", {
x <- "hello"
expect_equal(replace_non_ascii(x, remove.nonconverted = FALSE), "hello")
})

test_that("replace_non_ascii2 replaces non-ASCII with regex", {
x <- "hello world"
expect_equal(replace_non_ascii2(x), "hello world")
})

test_that("replace_curly_quote replaces curly quotes", {
z <- '\x93Hello\x94'
Encoding(z) <- "latin1"
expect_equal(replace_curly_quote(z), '"Hello"')
})