diff --git a/src/Humanizer.Tests/TruncatorTests.cs b/src/Humanizer.Tests/TruncatorTests.cs index 7a8fcabfa..b9dfc57d1 100644 --- a/src/Humanizer.Tests/TruncatorTests.cs +++ b/src/Humanizer.Tests/TruncatorTests.cs @@ -10,6 +10,15 @@ public void Truncate(string? input, int length, string? expectedOutput) => Assert.Equal(expectedOutput, input.Truncate(length)); + [Theory] + [InlineData("short text", 20, null, "short text")] + [InlineData("short text", 20, "trunc", "short text")] + [InlineData("short text", 20, "very long truncation string", "short text")] + [InlineData("Text longer than truncate length", 10, "very long truncation string", "Text longe")] + [InlineData("Text longer than truncate length", 10, "trunc", "Text trunc")] + public void TruncateWithCustomTruncationString(string? input, int length, string? trunactionString, string? expectedOutput) => + Assert.Equal(expectedOutput, input.Truncate(length, trunactionString)); + [Theory] [InlineData(null, 10, null)] [InlineData("", 10, "")] diff --git a/src/Humanizer/Truncation/FixedLengthTruncator.cs b/src/Humanizer/Truncation/FixedLengthTruncator.cs index 42af71d29..f39a56f9b 100644 --- a/src/Humanizer/Truncation/FixedLengthTruncator.cs +++ b/src/Humanizer/Truncation/FixedLengthTruncator.cs @@ -13,7 +13,7 @@ class FixedLengthTruncator : ITruncator return null; } - if (value.Length == 0) + if (value.Length == 0 || value.Length <= length) { return value; } @@ -25,15 +25,6 @@ class FixedLengthTruncator : ITruncator : value.Substring(value.Length - length); } - if (truncateFrom == TruncateFrom.Left) - { - return value.Length > length - ? StringHumanizeExtensions.Concat(truncationString.AsSpan(), value.AsSpan(value.Length - length + truncationString.Length)) - : value; - } - - return value.Length > length - ? StringHumanizeExtensions.Concat(value.AsSpan(0, length - truncationString.Length), truncationString.AsSpan()) - : value; + return truncateFrom == TruncateFrom.Left ? StringHumanizeExtensions.Concat(truncationString.AsSpan(), value.AsSpan(value.Length - length + truncationString.Length)) : StringHumanizeExtensions.Concat(value.AsSpan(0, length - truncationString.Length), truncationString.AsSpan()); } } \ No newline at end of file