Skip to content

Commit

Permalink
Add AppendIf, StringBuilder now return the StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzprojects committed Jul 12, 2015
1 parent e3e4b1a commit 237c1ff
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 6 deletions.
37 changes: 37 additions & 0 deletions src/Z.Core/System.String/String.Concatenate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;

public static partial class Extensions
{
/// <summary>An IEnumerable&lt;string&gt; extension method that concatenates the given this.</summary>
/// <param name="this">The @this to act on.</param>
/// <returns>A string.</returns>
public static string Concatenate(this IEnumerable<string> @this)
{
var sb = new StringBuilder();

foreach (var s in @this)
{
sb.Append(s);
}

return sb.ToString();
}

/// <summary>An IEnumerable&lt;T&gt; extension method that concatenates.</summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="source">The source to act on.</param>
/// <param name="func">The function.</param>
/// <returns>A string.</returns>
public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string> func)
{
var sb = new StringBuilder();
foreach (var item in source)
{
sb.Append(func(item));
}

return sb.ToString();
}
}
24 changes: 24 additions & 0 deletions src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendIf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Text;

public static partial class Extensions
{
/// <summary>A StringBuilder extension method that appends a when.</summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="predicate">The predicate.</param>
/// <param name="values">A variable-length parameters list containing values.</param>
/// <returns>A StringBuilder.</returns>
public static StringBuilder AppendIf<T>(this StringBuilder @this, Func<T, bool> predicate, params T[] values)
{
foreach (var value in values)
{
if (predicate(value))
{
@this.Append(value);
}
}

return @this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ public static partial class Extensions
/// <param name="this">The @this to act on.</param>
/// <param name="separator">The separator.</param>
/// <param name="values">The values.</param>
public static void AppendJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
public static StringBuilder AppendJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
{
@this.Append(string.Join(separator, values));

return @this;
}

/// <summary>A StringBuilder extension method that appends a join.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="separator">The separator.</param>
/// <param name="values">The values.</param>
public static void AppendJoin(this StringBuilder @this, string separator, params object[] values)
public static StringBuilder AppendJoin<T>(this StringBuilder @this, string separator, params T[] values)
{
@this.Append(string.Join(separator, values));

return @this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public static partial class Extensions
/// <param name="this">The @this to act on.</param>
/// <param name="format">Describes the format to use.</param>
/// <param name="args">A variable-length parameters list containing arguments.</param>
public static void AppendLineFormat(this StringBuilder @this, string format, params object[] args)
public static StringBuilder AppendLineFormat(this StringBuilder @this, string format, params object[] args)
{
@this.AppendLine(string.Format(format, args));

return @this;
}

/// <summary>
Expand All @@ -26,8 +28,10 @@ public static void AppendLineFormat(this StringBuilder @this, string format, par
/// <param name="this">The @this to act on.</param>
/// <param name="format">Describes the format to use.</param>
/// <param name="args">A variable-length parameters list containing arguments.</param>
public static void AppendLineFormat(this StringBuilder @this, string format, List<IEnumerable<object>> args)
public static StringBuilder AppendLineFormat(this StringBuilder @this, string format, List<IEnumerable<object>> args)
{
@this.AppendLine(string.Format(format, args));

return @this;
}
}
30 changes: 30 additions & 0 deletions src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendLineIf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System;
using System.Text;

public static partial class Extensions
{
/// <summary>A StringBuilder extension method that appends a line when.</summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="predicate">The predicate.</param>
/// <param name="values">A variable-length parameters list containing values.</param>
/// <returns>A StringBuilder.</returns>
public static StringBuilder AppendLineIf<T>(this StringBuilder @this, Func<T, bool> predicate, params T[] values)
{
foreach (var value in values)
{
if (predicate(value))
{
@this.AppendLine(value.ToString());
}
}

return @this;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library


using System.Collections.Generic;
using System.Text;

Expand All @@ -8,17 +15,21 @@ public static partial class Extensions
/// <param name="this">The @this to act on.</param>
/// <param name="separator">The separator.</param>
/// <param name="values">The values.</param>
public static void AppendLineJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
public static StringBuilder AppendLineJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
{
@this.AppendLine(string.Join(separator, values));

return @this;
}

/// <summary>A StringBuilder extension method that appends a line join.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="separator">The separator.</param>
/// <param name="values">The values.</param>
public static void AppendLineJoin(this StringBuilder @this, string separator, params object[] values)
public static StringBuilder AppendLineJoin(this StringBuilder @this, string separator, params object[] values)
{
@this.AppendLine(string.Join(separator, values));

return @this;
}
}
3 changes: 3 additions & 0 deletions src/Z.Core/Z.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,9 @@
<Compile Include="System.String\_ExtractValueType\String.ExtractUInt32.cs" />
<Compile Include="System.String\_ExtractValueType\String.ExtractUInt64.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendJoin.cs" />
<Compile Include="System.String\String.Concatenate.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendLineIf.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendIf.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.Substring.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.GetIndexAfterNextDoubleQuote.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.GetIndexAfterNextSingleQuote.cs" />
Expand Down
29 changes: 29 additions & 0 deletions test/Z.Core.Test/System.String/String.Concatenate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Z.Core.Test
{
[TestClass]
public class System_Text_StringBuilder_Concatenate
{
[TestMethod]
public void Concatenate()
{
var list = new List<string> {"Fizz", "Buzz"};

// Exemples
var result1 = list.Concatenate();
var result2 = list.Concatenate(x => x + x);

// Unit Test
Assert.AreEqual("FizzBuzz", result1);
Assert.AreEqual("FizzFizzBuzzBuzz", result2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Z.Core.Test
{
[TestClass]
public class System_Text_StringBuilder_AppendIf
{
[TestMethod]
public void AppendIf()
{
// Type
var @this = new StringBuilder();

// Exemples
@this.AppendIf(x => x.Contains("F"), "Fizz", "Buzz"); // return "FizzBuzz";

// Unit Test
Assert.AreEqual("Fizz", @this.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Z.Core.Test
{
[TestClass]
public class System_Text_StringBuilder_AppendLineIf
{
[TestMethod]
public void AppendLineIf()
{
// Type
var @this = new StringBuilder();

// Exemples
@this.AppendLineIf(x => x.Contains("F"), "Fizz", "Buzz"); // return "FizzBuzz";

// Unit Test
Assert.AreEqual("Fizz", @this.ToString());
}
}
}
3 changes: 3 additions & 0 deletions test/Z.Core.Test/Z.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@
<Compile Include="System.String\String.ToXDocument.cs" />
<Compile Include="System.String\String.ToXmlDocument.cs" />
<Compile Include="System.String\String.Truncate.cs" />
<Compile Include="System.String\String.Concatenate.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendJoin.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendJoinLine.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendLineIf.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendIf.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.Substring.cs" />
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendLineFormat.cs" />
<Compile Include="System.TimeSpan\TimeSpan.Ago.cs" />
Expand Down

0 comments on commit 237c1ff

Please sign in to comment.