From b9586f228f6b645e027a5ab1c32313070ce69f6b Mon Sep 17 00:00:00 2001 From: Steph Date: Wed, 19 Jul 2017 14:52:28 +0100 Subject: [PATCH 1/2] Added a trim function that I expect to have --- NAMESPACE | 1 + R/pad-trim.r | 14 ++++++++++++++ man/str_trim_all.Rd | 24 ++++++++++++++++++++++++ tests/testthat/test-trim.r | 6 ++++++ 4 files changed, 45 insertions(+) create mode 100644 man/str_trim_all.Rd diff --git a/NAMESPACE b/NAMESPACE index 026354fb..5ecfa10e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,6 +37,7 @@ export(str_to_lower) export(str_to_title) export(str_to_upper) export(str_trim) +export(str_trim_all) export(str_trunc) export(str_view) export(str_view_all) diff --git a/R/pad-trim.r b/R/pad-trim.r index b02d240a..9985266d 100644 --- a/R/pad-trim.r +++ b/R/pad-trim.r @@ -89,3 +89,17 @@ str_trim <- function(string, side = c("both", "left", "right")) { both = stri_trim_both(string) ) } + +#' Trim whitespace from start, middle, and end of string. +#' +#' @param string A character vector. +#'@return A character vector. +#' @export +#' @seealso \code{\link{str_trim}} to see end-only trimming +#' @examples +#' str_trim_all(" String with trailing, middle, and leading white space\t") +#' str_trim_all("\n\nString with excess, trailing and leading white space\n\n") +str_trim_all <- function(string) { + stri_trim_both(str_replace_all(string,"\\s+"," ")) +} + diff --git a/man/str_trim_all.Rd b/man/str_trim_all.Rd new file mode 100644 index 00000000..1191fd70 --- /dev/null +++ b/man/str_trim_all.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pad-trim.r +\name{str_trim_all} +\alias{str_trim_all} +\title{Trim whitespace from start, middle, and end of string.} +\usage{ +str_trim_all(string) +} +\arguments{ +\item{string}{A character vector.} +} +\value{ +A character vector. +} +\description{ +Trim whitespace from start, middle, and end of string. +} +\examples{ +str_trim_all(" String with trailing, middle, and leading white space\\t") +str_trim_all("\\n\\nString with excess, trailing and leading white space\\n\\n") +} +\seealso{ +\code{\link{str_trim}} to see end-only trimming +} diff --git a/tests/testthat/test-trim.r b/tests/testthat/test-trim.r index 3a50d47e..e008979a 100644 --- a/tests/testthat/test-trim.r +++ b/tests/testthat/test-trim.r @@ -16,3 +16,9 @@ test_that("side argument restricts trimming", { expect_equal(str_trim(" abc ", "left"), "abc ") expect_equal(str_trim(" abc ", "right"), " abc") }) + +test_that("trimming all removes spaces tabs", { + expect_equal(str_trim_all("ab\t\tc\t"), "ab c") + expect_equal(str_trim_all("\ta bc"), "a bc") + expect_equal(str_trim_all("\ta\t bc\t"), "a bc") +}) From 4b31628a9ffd38a97d1c8199ef17f1fd1b9bb9a0 Mon Sep 17 00:00:00 2001 From: Steph Date: Thu, 20 Jul 2017 12:02:17 +0100 Subject: [PATCH 2/2] Change to str_squish This is the conventional name in ruby and helps keep some clarity that it does something extra to str_trim --- NAMESPACE | 2 +- R/pad-trim.r | 8 ++++---- man/{str_trim_all.Rd => str_squish.Rd} | 10 +++++----- tests/testthat/test-trim.r | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) rename man/{str_trim_all.Rd => str_squish.Rd} (63%) diff --git a/NAMESPACE b/NAMESPACE index 5ecfa10e..da5870fd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,13 +31,13 @@ export(str_replace_na) export(str_sort) export(str_split) export(str_split_fixed) +export(str_squish) export(str_sub) export(str_subset) export(str_to_lower) export(str_to_title) export(str_to_upper) export(str_trim) -export(str_trim_all) export(str_trunc) export(str_view) export(str_view_all) diff --git a/R/pad-trim.r b/R/pad-trim.r index 9985266d..d899e839 100644 --- a/R/pad-trim.r +++ b/R/pad-trim.r @@ -93,13 +93,13 @@ str_trim <- function(string, side = c("both", "left", "right")) { #' Trim whitespace from start, middle, and end of string. #' #' @param string A character vector. -#'@return A character vector. +#' @return A character vector. #' @export #' @seealso \code{\link{str_trim}} to see end-only trimming #' @examples -#' str_trim_all(" String with trailing, middle, and leading white space\t") -#' str_trim_all("\n\nString with excess, trailing and leading white space\n\n") -str_trim_all <- function(string) { +#' str_squish(" String with trailing, middle, and leading white space\t") +#' str_squish("\n\nString with excess, trailing and leading white space\n\n") +str_squish <- function(string) { stri_trim_both(str_replace_all(string,"\\s+"," ")) } diff --git a/man/str_trim_all.Rd b/man/str_squish.Rd similarity index 63% rename from man/str_trim_all.Rd rename to man/str_squish.Rd index 1191fd70..76b86028 100644 --- a/man/str_trim_all.Rd +++ b/man/str_squish.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pad-trim.r -\name{str_trim_all} -\alias{str_trim_all} +\name{str_squish} +\alias{str_squish} \title{Trim whitespace from start, middle, and end of string.} \usage{ -str_trim_all(string) +str_squish(string) } \arguments{ \item{string}{A character vector.} @@ -16,8 +16,8 @@ A character vector. Trim whitespace from start, middle, and end of string. } \examples{ -str_trim_all(" String with trailing, middle, and leading white space\\t") -str_trim_all("\\n\\nString with excess, trailing and leading white space\\n\\n") +str_squish(" String with trailing, middle, and leading white space\\t") +str_squish("\\n\\nString with excess, trailing and leading white space\\n\\n") } \seealso{ \code{\link{str_trim}} to see end-only trimming diff --git a/tests/testthat/test-trim.r b/tests/testthat/test-trim.r index e008979a..3e491345 100644 --- a/tests/testthat/test-trim.r +++ b/tests/testthat/test-trim.r @@ -17,8 +17,8 @@ test_that("side argument restricts trimming", { expect_equal(str_trim(" abc ", "right"), " abc") }) -test_that("trimming all removes spaces tabs", { - expect_equal(str_trim_all("ab\t\tc\t"), "ab c") - expect_equal(str_trim_all("\ta bc"), "a bc") - expect_equal(str_trim_all("\ta\t bc\t"), "a bc") +test_that("squishing all removes all excess spaces and tabs", { + expect_equal(str_squish("ab\t\tc\t"), "ab c") + expect_equal(str_squish("\ta bc"), "a bc") + expect_equal(str_squish("\ta\t bc\t"), "a bc") })