From 02f19c9baafd5019d7abaea1a9896c8586604d91 Mon Sep 17 00:00:00 2001 From: eau leaf Date: Tue, 6 Jun 2023 23:04:45 -0700 Subject: [PATCH 1/2] cosmetic --- R/trunc.R | 10 ++++++++-- tests/testthat/test-trunc.R | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/R/trunc.R b/R/trunc.R index b48c78bf..d9366612 100644 --- a/R/trunc.R +++ b/R/trunc.R @@ -30,15 +30,21 @@ str_trunc <- function(string, width, side = c("right", "left", "center"), cli::cli_abort( "`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)})." ) + } else if (width... > 1) { + end_c <- end_l <- -1 + } else if (width... == 1) { + end_c <- 0; end_l <- -1 + } else { + end_c <- end_l <- 0 } string[too_long] <- switch(side, right = str_c(str_sub(string[too_long], 1, width...), ellipsis), - left = str_c(ellipsis, str_sub(string[too_long], -width..., -1)), + left = str_c(ellipsis, str_sub(string[too_long], -width..., end_l)), center = str_c( str_sub(string[too_long], 1, ceiling(width... / 2)), ellipsis, - str_sub(string[too_long], -floor(width... / 2), -1) + str_sub(string[too_long], -floor(width... / 2), end_c) ) ) string diff --git a/tests/testthat/test-trunc.R b/tests/testthat/test-trunc.R index 1bb80614..d195dfa1 100644 --- a/tests/testthat/test-trunc.R +++ b/tests/testthat/test-trunc.R @@ -35,3 +35,12 @@ test_that("does not truncate to a length shorter than elipsis", { str_trunc("foobar", 3, ellipsis = "....") }) }) + +test_that("right side of ellipsis correctly substrings when internal var `width...` is 0 or 1", { + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 3, "center", ".."), + c('', 'a', 'aa', 'aaa', 'a..','a..')) + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 2, "left", ".."), + c('', 'a', 'aa', '..', '..','..')) +}) + + From d27b68f37e8b67fd3324e97cc06a0041c9af010d Mon Sep 17 00:00:00 2001 From: eauleaf <81445509+eauleaf@users.noreply.github.com> Date: Wed, 7 Jun 2023 10:55:26 -0700 Subject: [PATCH 2/2] Update test-trunc.R added a couple more tests for completeness --- tests/testthat/test-trunc.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-trunc.R b/tests/testthat/test-trunc.R index d195dfa1..8fc1919e 100644 --- a/tests/testthat/test-trunc.R +++ b/tests/testthat/test-trunc.R @@ -37,10 +37,12 @@ test_that("does not truncate to a length shorter than elipsis", { }) test_that("right side of ellipsis correctly substrings when internal var `width...` is 0 or 1", { - expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 3, "center", ".."), - c('', 'a', 'aa', 'aaa', 'a..','a..')) - expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 2, "left", ".."), - c('', 'a', 'aa', '..', '..','..')) + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 3, "center", ".."), c('', 'a', 'aa', 'aaa', 'a..','a..')) # width... == 1 + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 3, "left", ".."), c('', 'a', 'aa', 'aaa', '..a','..a')) # width... == 1 + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 2, "center", ".."), c('', 'a', 'aa', '..', '..','..')) # width... == 0 + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 2, "left", ".."), c('', 'a', 'aa', '..', '..','..')) # width... == 0 + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 1, "center", ""), c('', 'a', 'a', 'a', 'a','a')) # width... == 1, empty ellipsis + expect_equal(str_trunc(c('', 'a', 'aa', 'aaa', 'aaaa','aaaaa'), width = 1, "left", ""), c('', 'a', 'a', 'a', 'a','a')) # width... == 1, empty ellipsis })