Skip to content

Commit 237c1ff

Browse files
author
zzzprojects
committed
Add AppendIf, StringBuilder now return the StringBuilder
1 parent e3e4b1a commit 237c1ff

11 files changed

Lines changed: 207 additions & 6 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
public static partial class Extensions
6+
{
7+
/// <summary>An IEnumerable&lt;string&gt; extension method that concatenates the given this.</summary>
8+
/// <param name="this">The @this to act on.</param>
9+
/// <returns>A string.</returns>
10+
public static string Concatenate(this IEnumerable<string> @this)
11+
{
12+
var sb = new StringBuilder();
13+
14+
foreach (var s in @this)
15+
{
16+
sb.Append(s);
17+
}
18+
19+
return sb.ToString();
20+
}
21+
22+
/// <summary>An IEnumerable&lt;T&gt; extension method that concatenates.</summary>
23+
/// <typeparam name="T">Generic type parameter.</typeparam>
24+
/// <param name="source">The source to act on.</param>
25+
/// <param name="func">The function.</param>
26+
/// <returns>A string.</returns>
27+
public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string> func)
28+
{
29+
var sb = new StringBuilder();
30+
foreach (var item in source)
31+
{
32+
sb.Append(func(item));
33+
}
34+
35+
return sb.ToString();
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Text;
3+
4+
public static partial class Extensions
5+
{
6+
/// <summary>A StringBuilder extension method that appends a when.</summary>
7+
/// <typeparam name="T">Generic type parameter.</typeparam>
8+
/// <param name="this">The @this to act on.</param>
9+
/// <param name="predicate">The predicate.</param>
10+
/// <param name="values">A variable-length parameters list containing values.</param>
11+
/// <returns>A StringBuilder.</returns>
12+
public static StringBuilder AppendIf<T>(this StringBuilder @this, Func<T, bool> predicate, params T[] values)
13+
{
14+
foreach (var value in values)
15+
{
16+
if (predicate(value))
17+
{
18+
@this.Append(value);
19+
}
20+
}
21+
22+
return @this;
23+
}
24+
}

src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendJoin.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ public static partial class Extensions
88
/// <param name="this">The @this to act on.</param>
99
/// <param name="separator">The separator.</param>
1010
/// <param name="values">The values.</param>
11-
public static void AppendJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
11+
public static StringBuilder AppendJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
1212
{
1313
@this.Append(string.Join(separator, values));
14+
15+
return @this;
1416
}
1517

1618
/// <summary>A StringBuilder extension method that appends a join.</summary>
1719
/// <param name="this">The @this to act on.</param>
1820
/// <param name="separator">The separator.</param>
1921
/// <param name="values">The values.</param>
20-
public static void AppendJoin(this StringBuilder @this, string separator, params object[] values)
22+
public static StringBuilder AppendJoin<T>(this StringBuilder @this, string separator, params T[] values)
2123
{
2224
@this.Append(string.Join(separator, values));
25+
26+
return @this;
2327
}
2428
}

src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendLineFormat.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public static partial class Extensions
1515
/// <param name="this">The @this to act on.</param>
1616
/// <param name="format">Describes the format to use.</param>
1717
/// <param name="args">A variable-length parameters list containing arguments.</param>
18-
public static void AppendLineFormat(this StringBuilder @this, string format, params object[] args)
18+
public static StringBuilder AppendLineFormat(this StringBuilder @this, string format, params object[] args)
1919
{
2020
@this.AppendLine(string.Format(format, args));
21+
22+
return @this;
2123
}
2224

2325
/// <summary>
@@ -26,8 +28,10 @@ public static void AppendLineFormat(this StringBuilder @this, string format, par
2628
/// <param name="this">The @this to act on.</param>
2729
/// <param name="format">Describes the format to use.</param>
2830
/// <param name="args">A variable-length parameters list containing arguments.</param>
29-
public static void AppendLineFormat(this StringBuilder @this, string format, List<IEnumerable<object>> args)
31+
public static StringBuilder AppendLineFormat(this StringBuilder @this, string format, List<IEnumerable<object>> args)
3032
{
3133
@this.AppendLine(string.Format(format, args));
34+
35+
return @this;
3236
}
3337
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System;
8+
using System.Text;
9+
10+
public static partial class Extensions
11+
{
12+
/// <summary>A StringBuilder extension method that appends a line when.</summary>
13+
/// <typeparam name="T">Generic type parameter.</typeparam>
14+
/// <param name="this">The @this to act on.</param>
15+
/// <param name="predicate">The predicate.</param>
16+
/// <param name="values">A variable-length parameters list containing values.</param>
17+
/// <returns>A StringBuilder.</returns>
18+
public static StringBuilder AppendLineIf<T>(this StringBuilder @this, Func<T, bool> predicate, params T[] values)
19+
{
20+
foreach (var value in values)
21+
{
22+
if (predicate(value))
23+
{
24+
@this.AppendLine(value.ToString());
25+
}
26+
}
27+
28+
return @this;
29+
}
30+
}

src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendLineJoin.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
18
using System.Collections.Generic;
29
using System.Text;
310

@@ -8,17 +15,21 @@ public static partial class Extensions
815
/// <param name="this">The @this to act on.</param>
916
/// <param name="separator">The separator.</param>
1017
/// <param name="values">The values.</param>
11-
public static void AppendLineJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
18+
public static StringBuilder AppendLineJoin<T>(this StringBuilder @this, string separator, IEnumerable<T> values)
1219
{
1320
@this.AppendLine(string.Join(separator, values));
21+
22+
return @this;
1423
}
1524

1625
/// <summary>A StringBuilder extension method that appends a line join.</summary>
1726
/// <param name="this">The @this to act on.</param>
1827
/// <param name="separator">The separator.</param>
1928
/// <param name="values">The values.</param>
20-
public static void AppendLineJoin(this StringBuilder @this, string separator, params object[] values)
29+
public static StringBuilder AppendLineJoin(this StringBuilder @this, string separator, params object[] values)
2130
{
2231
@this.AppendLine(string.Join(separator, values));
32+
33+
return @this;
2334
}
2435
}

src/Z.Core/Z.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@
627627
<Compile Include="System.String\_ExtractValueType\String.ExtractUInt32.cs" />
628628
<Compile Include="System.String\_ExtractValueType\String.ExtractUInt64.cs" />
629629
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendJoin.cs" />
630+
<Compile Include="System.String\String.Concatenate.cs" />
631+
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendLineIf.cs" />
632+
<Compile Include="System.Text.StringBuilder\StringBuilder.AppendIf.cs" />
630633
<Compile Include="System.Text.StringBuilder\StringBuilder.Substring.cs" />
631634
<Compile Include="System.Text.StringBuilder\StringBuilder.GetIndexAfterNextDoubleQuote.cs" />
632635
<Compile Include="System.Text.StringBuilder\StringBuilder.GetIndexAfterNextSingleQuote.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System.Collections.Generic;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace Z.Core.Test
11+
{
12+
[TestClass]
13+
public class System_Text_StringBuilder_Concatenate
14+
{
15+
[TestMethod]
16+
public void Concatenate()
17+
{
18+
var list = new List<string> {"Fizz", "Buzz"};
19+
20+
// Exemples
21+
var result1 = list.Concatenate();
22+
var result2 = list.Concatenate(x => x + x);
23+
24+
// Unit Test
25+
Assert.AreEqual("FizzBuzz", result1);
26+
Assert.AreEqual("FizzFizzBuzzBuzz", result2);
27+
}
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System.Text;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace Z.Core.Test
11+
{
12+
[TestClass]
13+
public class System_Text_StringBuilder_AppendIf
14+
{
15+
[TestMethod]
16+
public void AppendIf()
17+
{
18+
// Type
19+
var @this = new StringBuilder();
20+
21+
// Exemples
22+
@this.AppendIf(x => x.Contains("F"), "Fizz", "Buzz"); // return "FizzBuzz";
23+
24+
// Unit Test
25+
Assert.AreEqual("Fizz", @this.ToString());
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System.Text;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace Z.Core.Test
11+
{
12+
[TestClass]
13+
public class System_Text_StringBuilder_AppendLineIf
14+
{
15+
[TestMethod]
16+
public void AppendLineIf()
17+
{
18+
// Type
19+
var @this = new StringBuilder();
20+
21+
// Exemples
22+
@this.AppendLineIf(x => x.Contains("F"), "Fizz", "Buzz"); // return "FizzBuzz";
23+
24+
// Unit Test
25+
Assert.AreEqual("Fizz", @this.ToString());
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)