-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AppendIf, StringBuilder now return the StringBuilder
- Loading branch information
zzzprojects
committed
Jul 12, 2015
1 parent
e3e4b1a
commit 237c1ff
Showing
11 changed files
with
207 additions
and
6 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
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<string> 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<T> 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
24
src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendIf.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,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; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/Z.Core/System.Text.StringBuilder/StringBuilder.AppendLineIf.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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.AppendIf.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,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()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/Z.Core.Test/System.Text.StringBuilder/StringBuilder.AppendLineIf.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,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()); | ||
} | ||
} | ||
} |
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