Skip to content

Commit

Permalink
Added IEnumerableStringExtensions + unit tests.
Browse files Browse the repository at this point in the history
Release 1.0.3
  • Loading branch information
GlitchedPolygons committed Sep 3, 2019
1 parent ee261ba commit d57dbc0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ExtensionMethods.csproj
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Glitched Polygons</Authors>
<Company>Glitched Polygons</Company>
Expand All @@ -14,7 +13,8 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<PackageIconUrl>https://media.githubusercontent.com/media/GlitchedPolygons/NugetIcons/661dca007a8e49de5e31e5be268b2550dfa2aea7/extension-methods.png</PackageIconUrl>
<PackageVersion>1.0.2</PackageVersion>
<PackageVersion>1.0.3</PackageVersion>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down
53 changes: 53 additions & 0 deletions src/IEnumerableStringExtensions.cs
@@ -0,0 +1,53 @@
using System;
using System.Text;
using System.Collections.Generic;

namespace GlitchedPolygons.ExtensionMethods
{
/// <summary>
/// <c>IEnumerable&lt;string&gt; 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);
}
}
}
34 changes: 34 additions & 0 deletions tests/IEnumerableStringExtensionsTests.cs
@@ -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());
}
}
}

0 comments on commit d57dbc0

Please sign in to comment.