-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from ErikSchierboom/truncate-correct
Added truncate methods. Implements #80
- Loading branch information
Showing
11 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests | ||
{ | ||
public class TruncatorTests | ||
{ | ||
[Theory] | ||
[InlineData(null, 10, null)] | ||
[InlineData("", 10, "")] | ||
[InlineData("a", 1, "a")] | ||
[InlineData("Text longer than truncate length", 10, "Text long…")] | ||
[InlineData("Text with length equal to truncate length", 41, "Text with length equal to truncate length")] | ||
[InlineData("Text smaller than truncate length", 34, "Text smaller than truncate length")] | ||
public void Truncate(string input, int length, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, null)] | ||
[InlineData("", 10, "")] | ||
[InlineData("a", 1, "a")] | ||
[InlineData("Text longer than truncate length", 10, "Text long…")] | ||
[InlineData("Text with length equal to truncate length", 41, "Text with length equal to truncate length")] | ||
[InlineData("Text smaller than truncate length", 34, "Text smaller than truncate length")] | ||
public void TruncateWithFixedLengthTruncator(string input, int length, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, Truncator.FixedLength)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, null)] | ||
[InlineData("", 10, "")] | ||
[InlineData("a", 1, "a")] | ||
[InlineData("Text with more characters than truncate length", 10, "Text with m…")] | ||
[InlineData("Text with number of characters equal to truncate length", 47, "Text with number of characters equal to truncate length")] | ||
[InlineData("Text with less characters than truncate length", 41, "Text with less characters than truncate length")] | ||
public void TruncateWithFixedNumberOfCharactersTruncator(string input, int length, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, Truncator.FixedNumberOfCharacters)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, null)] | ||
[InlineData("", 10, "")] | ||
[InlineData("a", 1, "a")] | ||
[InlineData("Text with more words than truncate length", 4, "Text with more words…")] | ||
[InlineData("Text with number of words equal to truncate length", 9, "Text with number of words equal to truncate length")] | ||
[InlineData("Text with less words than truncate length", 8, "Text with less words than truncate length")] | ||
[InlineData("Words are\nsplit\rby\twhitespace", 4, "Words are\nsplit\rby…")] | ||
public void TruncateWithFixedNumberOfWordsTruncator(string input, int length, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, Truncator.FixedNumberOfWords)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, "...", null)] | ||
[InlineData("", 10, "...", "")] | ||
[InlineData("a", 1, "...", "a")] | ||
[InlineData("Text longer than truncate length", 10, "...", "Text lo...")] | ||
[InlineData("Text with length equal to truncate length", 41, "...", "Text with length equal to truncate length")] | ||
[InlineData("Text smaller than truncate length", 34, "...", "Text smaller than truncate length")] | ||
[InlineData("Text with delimiter length greater than truncate length truncates to fixed length without truncation string", 2, "...", "Te")] | ||
[InlineData("Null truncation string truncates to truncate length without truncation string", 4, null, "Null")] | ||
public void TruncateWithTruncationString(string input, int length, string truncationString, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, truncationString)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, "...", null)] | ||
[InlineData("", 10, "...", "")] | ||
[InlineData("a", 1, "...", "a")] | ||
[InlineData("Text longer than truncate length", 10, "...", "Text lo...")] | ||
[InlineData("Text with different truncation string", 10, "---", "Text wi---")] | ||
[InlineData("Text with length equal to truncate length", 41, "...", "Text with length equal to truncate length")] | ||
[InlineData("Text smaller than truncate length", 34, "...", "Text smaller than truncate length")] | ||
[InlineData("Text with delimiter length greater than truncate length truncates to fixed length without truncation string", 2, "...", "Te")] | ||
[InlineData("Null truncation string truncates to truncate length without truncation string", 4, null, "Null")] | ||
public void TruncateWithTruncationStringAndFixedLengthTruncator(string input, int length, string truncationString, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, truncationString, Truncator.FixedLength)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, "...", null)] | ||
[InlineData("", 10, "...", "")] | ||
[InlineData("a", 1, "...", "a")] | ||
[InlineData("Text with more characters than truncate length", 10, "...", "Text wit...")] | ||
[InlineData("Text with different truncation string", 10, "---", "Text wit---")] | ||
[InlineData("Text with number of characters equal to truncate length", 47, "...", "Text with number of characters equal to truncate length")] | ||
[InlineData("Text with less characters than truncate length", 41, "...", "Text with less characters than truncate length")] | ||
[InlineData("Text with delimiter length greater than truncate length truncates to fixed length without truncation string", 2, "...", "Te")] | ||
[InlineData("Null truncation string truncates to truncate length without truncation string", 4, null, "Null")] | ||
public void TruncateWithTruncationStringAndFixedNumberOfCharactersTruncator(string input, int length, string truncationString, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, truncationString, Truncator.FixedNumberOfCharacters)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, 10, "...", null)] | ||
[InlineData("", 10, "...", "")] | ||
[InlineData("a", 1, "...", "a")] | ||
[InlineData("Text with more words than truncate length", 4, "...", "Text with more words...")] | ||
[InlineData("Text with different truncation string", 4, "---", "Text with different truncation---")] | ||
[InlineData("Text with number of words equal to truncate length", 9, "...", "Text with number of words equal to truncate length")] | ||
[InlineData("Text with less words than truncate length", 8, "...", "Text with less words than truncate length")] | ||
[InlineData("Words are\nsplit\rby\twhitespace", 4, "...", "Words are\nsplit\rby...")] | ||
[InlineData("Null truncation string truncates to truncate length without truncation string", 4, null, "Null truncation string truncates")] | ||
public void TruncateWithTruncationStringAndFixedNumberOfWordsTruncator(string input, int length, string truncationString, string expectedOutput) | ||
{ | ||
Assert.Equal(expectedOutput, input.Truncate(length, truncationString, Truncator.FixedNumberOfWords)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=FluentDate/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Truncation/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Resources/@EntryIndexedValue">False</s:Boolean> | ||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Transformer/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Truncate a string to a fixed length | ||
/// </summary> | ||
class FixedLengthTruncator : ITruncator | ||
{ | ||
public string Truncate(string value, int length, string truncationString) | ||
{ | ||
if (value == null) | ||
return null; | ||
|
||
if (value.Length == 0) | ||
return value; | ||
|
||
if (truncationString == null || truncationString.Length > length) | ||
return value.Substring(0, length); | ||
|
||
return value.Length > length ? value.Substring(0, length - truncationString.Length) + truncationString : value; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Humanizer/Truncation/FixedNumberOfCharactersTruncator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Truncate a string to a fixed number of characters | ||
/// </summary> | ||
class FixedNumberOfCharactersTruncator : ITruncator | ||
{ | ||
public string Truncate(string value, int length, string truncationString) | ||
{ | ||
if (value == null) | ||
return null; | ||
|
||
if (value.Length == 0) | ||
return value; | ||
|
||
if (truncationString == null || truncationString.Length > length) | ||
return value.Substring(0, length); | ||
|
||
var alphaNumericalCharactersProcessed = 0; | ||
|
||
var numberOfCharactersEqualToTruncateLength = value.ToCharArray().Count(Char.IsLetterOrDigit) == length; | ||
|
||
for (var i = 0; i < value.Length - truncationString.Length; i++) | ||
{ | ||
if (Char.IsLetterOrDigit(value[i])) | ||
alphaNumericalCharactersProcessed++; | ||
|
||
if (numberOfCharactersEqualToTruncateLength && alphaNumericalCharactersProcessed == length) | ||
return value; | ||
|
||
if (!numberOfCharactersEqualToTruncateLength && alphaNumericalCharactersProcessed + truncationString.Length == length) | ||
return value.Substring(0, i + 1) + truncationString; | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Truncate a string to a fixed number of words | ||
/// </summary> | ||
class FixedNumberOfWordsTruncator : ITruncator | ||
{ | ||
public string Truncate(string value, int length, string truncationString) | ||
{ | ||
if (value == null) | ||
return null; | ||
|
||
if (value.Length == 0) | ||
return value; | ||
|
||
var numberOfWordsProcessed = 0; | ||
var numberOfWords = value.Split((char[])null, StringSplitOptions.RemoveEmptyEntries).Count(); | ||
|
||
if (numberOfWords <= length) | ||
return value; | ||
|
||
var lastCharactersWasWhiteSpace = true; | ||
|
||
for (var i = 0; i < value.Length; i++) | ||
{ | ||
if (Char.IsWhiteSpace(value[i])) | ||
{ | ||
if (!lastCharactersWasWhiteSpace) | ||
numberOfWordsProcessed++; | ||
|
||
lastCharactersWasWhiteSpace = true; | ||
|
||
if (numberOfWordsProcessed == length) | ||
return value.Substring(0, i) + truncationString; | ||
} | ||
else | ||
{ | ||
lastCharactersWasWhiteSpace = false; | ||
} | ||
} | ||
|
||
return value + truncationString; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Can truncate a string. | ||
/// </summary> | ||
public interface ITruncator | ||
{ | ||
/// <summary> | ||
/// Truncate a string | ||
/// </summary> | ||
/// <param name="value">The string to truncate</param> | ||
/// <param name="length">The length to truncate to</param> | ||
/// <param name="truncationString">The string used to truncate with</param> | ||
/// <returns>The truncated string</returns> | ||
string Truncate(string value, int length, string truncationString); | ||
} | ||
} |
Oops, something went wrong.