generated from GlitchedPolygons/netstandard2.0-class-lib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IEnumerableStringExtensions + unit tests.
Release 1.0.3
- Loading branch information
1 parent
ee261ba
commit d57dbc0
Showing
3 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
|
||
namespace GlitchedPolygons.ExtensionMethods | ||
{ | ||
/// <summary> | ||
/// <c>IEnumerable<string> extension methods.</c> | ||
/// </summary> | ||
public static class IEnumerableStringExtensions | ||
{ | ||
/// <summary> | ||
/// The initial capacity of the internal <see cref="StringBuilder"/>.<para> </para> | ||
/// Try to give a rough estimate of your output string's length here. | ||
/// </summary> | ||
public static int StringBuilderInitialCap = 64; | ||
|
||
/// <summary> | ||
/// Converts a collection of <c>string</c>s to a single, comma-separated <c>string</c>. | ||
/// </summary> | ||
/// <param name="e">The collection of <c>string</c>s to convert.</param> | ||
/// <returns>The comma-separated <c>string</c> containing the input <c>string</c>s (e.g. "element1,element2,element3").</returns> | ||
public static string ToCommaSeparatedString(this IEnumerable<string> e) | ||
{ | ||
return ToCustomCharSeparatedString(e, ','); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a collection of <c>string</c>s to a single, custom-character-separated <c>string</c>.<para> </para> | ||
/// Make ABSOLUTELY sure that none of the <c>string</c>s in the collection contains the <paramref name="separatorChar"/>!!<para> </para> | ||
/// It would mess with the reverse method (which is <c>string.Split(<paramref name="separatorChar"/>)</c>) | ||
/// </summary> | ||
/// <param name="strings">The collection of <c>string</c>s to convert.</param> | ||
/// <param name="separatorChar">The <c>char</c> that will separate one entry from the other in the output <c>string</c>. Could be '|', '+' or something like that, just ensure that it is unique (no string inside the collection should contain the separator char!!!).</param> | ||
/// <returns>The flattened <c>string</c>, where each <c>string</c> is separated by <paramref name="separatorChar"/> from one another.</returns> | ||
/// <exception cref="ArgumentException">Thrown when one or more <c>string</c>s inside the <paramref name="strings"/> input collection contains the <paramref name="separatorChar"/> character.</exception> | ||
public static string ToCustomCharSeparatedString(this IEnumerable<string> strings, char separatorChar) | ||
{ | ||
var sb = new StringBuilder(StringBuilderInitialCap); | ||
|
||
foreach (string str in strings) | ||
{ | ||
if (str.IndexOf(separatorChar) != -1) | ||
{ | ||
throw new ArgumentException($"{nameof(IEnumerableStringExtensions)}::{nameof(ToCustomCharSeparatedString)}: One or more strings contains the separator char (a '{separatorChar}' in this case). This would mess with the reverse method (string.Split(char)). Problematic string: {str}"); | ||
} | ||
sb.Append(str).Append(separatorChar); | ||
} | ||
|
||
return sb.ToString().TrimEnd(separatorChar); | ||
} | ||
} | ||
} |
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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Xunit; | ||
|
||
namespace GlitchedPolygons.ExtensionMethods.UnitTests | ||
{ | ||
public class IEnumerableStringExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("test1", "test2", "te,st3_uh_oh...")] | ||
[InlineData("test1", "test2", "test3", "test4", "test5,")] | ||
public void ToCommaSeparatedList_OneOrMoreStringsContainsComma_ThrowsException(params string[] strings) | ||
{ | ||
Assert.Throws<ArgumentException>(strings.ToCommaSeparatedString); | ||
} | ||
|
||
[Theory] | ||
[InlineData("test1", "test2", "test3", "test4", "test5", "test6")] | ||
[InlineData("test1", "SPECIAL_charächtërsss*#°§¬ !!", "test3", "test4", "test5", "test6", " ", ".", " ", "@@\\\\ \\ \t\n")] | ||
public void ToCommaSeparatedList_ShouldReturnCorrectly(params string[] strings) | ||
{ | ||
string o = strings.ToCommaSeparatedString(); | ||
Assert.True(o.NotNullNotEmpty()); | ||
Assert.Equal(strings, o.Split(',')); | ||
} | ||
|
||
[Fact] | ||
public void ToCommaSeparatedList_EmptyStringsArray_ShouldReturnEmptyString() | ||
{ | ||
Assert.Empty(Array.Empty<string>().ToCommaSeparatedString()); | ||
} | ||
} | ||
} |