Skip to content

Commit c65af0f

Browse files
authored
✨ feature: Add Split Camel Case Extension (#53)
* 📜 docs: Update API * ✨ feature: Create Split Camel Case Extension * 🚨 tests: Create Split Camel Case Tests * ✨ feature: Add null case * 🚨 tests: Add null case test * 📜 docs: Update Changelog
1 parent 60a396b commit c65af0f

File tree

10 files changed

+291
-97
lines changed

10 files changed

+291
-97
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Split camel case extension
13+
1014
## [0.3.0] - 2024-10-20
1115

1216
### Added
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using TJC.StringExtensions.Cases;
2+
3+
namespace TJC.StringExtensions.Tests.Cases;
4+
5+
[TestClass]
6+
public class CamelCaseExtensionsTests
7+
{
8+
[TestMethod]
9+
public void SplitCamelCase_Null_ReturnsEmpty()
10+
{
11+
// Arrange
12+
string? input = null;
13+
var expected = string.Empty;
14+
15+
// Act
16+
var result = input.SplitCamelCase();
17+
18+
// Assert
19+
Assert.AreEqual(expected, result);
20+
}
21+
22+
[TestMethod]
23+
public void SplitCamelCase_FirstLetterLowercase_SplitsWithSpace()
24+
{
25+
// Arrange
26+
var input = "camelCaseExtensions";
27+
var expected = "camel Case Extensions";
28+
29+
// Act
30+
var result = input.SplitCamelCase();
31+
32+
// Assert
33+
Assert.AreEqual(expected, result);
34+
}
35+
36+
[TestMethod]
37+
public void SplitCamelCase_FirstLetterUppercase_SplitsWithSpace()
38+
{
39+
// Arrange
40+
var input = "CamelCaseExtensions";
41+
var expected = "Camel Case Extensions";
42+
43+
// Act
44+
var result = input.SplitCamelCase();
45+
46+
// Assert
47+
Assert.AreEqual(expected, result);
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace TJC.StringExtensions.Cases;
2+
3+
/// <summary>
4+
/// Camel case extensions for strings.
5+
/// <para></para>
6+
/// Example of camel case: "camelCaseExtensions"
7+
/// </summary>
8+
public static class CamelCaseExtensions
9+
{
10+
/// <summary>
11+
/// Split camel case with a separator (typically a space).
12+
/// <para></para>
13+
/// Example: "camelCaseExtensions" becomes "camel Case Extensions"
14+
/// </summary>
15+
/// <param name="input"></param>
16+
/// <param name="separator"></param>
17+
/// <returns></returns>
18+
public static string SplitCamelCase(this string? input, string separator = " ")
19+
{
20+
if (string.IsNullOrEmpty(input))
21+
return string.Empty;
22+
return string.Concat(input.Select((x, i) => i > 0 && char.IsUpper(x) ? $"{separator}{x}" : x.ToString()));
23+
}
24+
}
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
using TJC.StringExtensions.Lines;
22
using TJC.StringExtensions.Padding;
33

4-
namespace TJC.StringExtensions.Header
4+
namespace TJC.StringExtensions.Header;
5+
6+
/// <summary>
7+
/// Extensions for creating text headers.
8+
/// </summary>
9+
public static class HeaderExtensions
510
{
6-
public static class HeaderExtensions
11+
/// <summary>
12+
/// Create a text header with a border of specified characters.
13+
/// <para></para>
14+
/// For use within console applications.
15+
/// </summary>
16+
/// <param name="lines"></param>
17+
/// <param name="internalLineLimit"></param>
18+
/// <param name="character"></param>
19+
/// <param name="linePrefix"></param>
20+
/// <param name="lineSuffix"></param>
21+
/// <returns></returns>
22+
public static IEnumerable<string> GenerateHeader(this IEnumerable<string> lines, int internalLineLimit = 70, char character = '#', string linePrefix = "### ", string lineSuffix = " ###")
723
{
8-
public static IEnumerable<string> GenerateHeader(this IEnumerable<string> lines, int internalLineLimit = 70, char character = '#', string linePrefix = "### ", string lineSuffix = " ###")
9-
{
10-
var tempLines = new List<string>();
11-
// Get lines
12-
foreach (var newLine in lines)
13-
foreach (var splitLine in newLine.SplitNewLine())
14-
foreach (var line in splitLine.SplitLines(internalLineLimit))
15-
tempLines.Add(line);
24+
var tempLines = new List<string>();
25+
// Get lines
26+
foreach (var newLine in lines)
27+
foreach (var splitLine in newLine.SplitNewLine())
28+
foreach (var line in splitLine.SplitLines(internalLineLimit))
29+
tempLines.Add(line);
1630

17-
// Get border
18-
var longestLine = tempLines.Max(x => x.Length);
19-
var headerBorder = new string(character, longestLine + linePrefix.Length + lineSuffix.Length);
31+
// Get border
32+
var longestLine = tempLines.Max(x => x.Length);
33+
var headerBorder = new string(character, longestLine + linePrefix.Length + lineSuffix.Length);
2034

21-
// Get lines with prefix and suffix
22-
var headerLines = new List<string> { headerBorder };
23-
foreach (var line in tempLines)
24-
headerLines.Add($"{linePrefix}{line.PadBoth(longestLine)}{lineSuffix}");
25-
headerLines.Add(headerBorder);
26-
return headerLines;
27-
}
35+
// Get lines with prefix and suffix
36+
var headerLines = new List<string> { headerBorder };
37+
foreach (var line in tempLines)
38+
headerLines.Add($"{linePrefix}{line.PadBoth(longestLine)}{lineSuffix}");
39+
headerLines.Add(headerBorder);
40+
return headerLines;
2841
}
2942
}

TJC.StringExtensions/Lines/LineExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace TJC.StringExtensions.Lines;
22

3+
/// <summary>
4+
/// Extensions for lines of text.
5+
/// </summary>
36
public static class LineExtensions
47
{
58
private const int MaxLineWidth = 60;

TJC.StringExtensions/Padding/PaddingExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
namespace TJC.StringExtensions.Padding;
22

3+
/// <summary>
4+
/// Padding extensions for strings.
5+
/// </summary>
36
public static class PaddingExtension
47
{
8+
/// <summary>
9+
/// Pad a string with spaces on both sides to make it the specified length.
10+
/// </summary>
11+
/// <param name="source"></param>
12+
/// <param name="length"></param>
13+
/// <returns></returns>
514
public static string PadBoth(this string source, int length)
615
{
716
var spaces = length - source.Length;

TJC.StringExtensions/Parsing/StringParserExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,61 @@
22

33
namespace TJC.StringExtensions.Parsing;
44

5+
/// <summary>
6+
/// Parsing extensions for strings.
7+
/// </summary>
58
public static partial class StringParserExtensions
69
{
710
/// <summary>
8-
/// Keep Alpha Only
11+
/// Keep Alpha Only.
912
/// </summary>
1013
/// <param name="str"></param>
1114
/// <returns></returns>
1215
public static string KeepAlpha(this string str) => AlphaRegex().Replace(str, string.Empty);
1316
[GeneratedRegex("[^a-zA-Z]")] public static partial Regex AlphaRegex();
1417

1518
/// <summary>
16-
/// Keep Numeric Only
19+
/// Keep Numeric Only.
1720
/// </summary>
1821
/// <param name="str"></param>
1922
/// <returns></returns>
2023
public static string KeepNumeric(this string str) => NumericRegex().Replace(str, string.Empty);
2124
[GeneratedRegex("[^0-9]")] public static partial Regex NumericRegex();
2225

2326
/// <summary>
24-
/// Keep Numeric & Period
27+
/// Keep Numeric &amp; Period.
2528
/// </summary>
2629
/// <param name="str"></param>
2730
/// <returns></returns>
2831
public static string KeepNumericAndPeriod(this string str) => NumericAndPeriodRegex().Replace(str, string.Empty);
2932
[GeneratedRegex("[^0-9.]")] public static partial Regex NumericAndPeriodRegex();
3033

3134
/// <summary>
32-
/// Keep Alpha & Numeric
35+
/// Keep Alpha &amp; Numeric.
3336
/// </summary>
3437
/// <param name="str"></param>
3538
/// <returns></returns>
3639
public static string KeepAlphaNumeric(this string str) => AlphaNumericRegex().Replace(str, string.Empty);
3740
[GeneratedRegex("[^a-zA-Z0-9]")] public static partial Regex AlphaNumericRegex();
3841

3942
/// <summary>
40-
/// Keep Alpha, Numeric & Space
43+
/// Keep Alpha, Numeric &amp; Space.
4144
/// </summary>
4245
/// <param name="str"></param>
4346
/// <returns></returns>
4447
public static string KeepAlphaNumericAndSpace(this string str) => AlphaNumericAndSpaceRegex().Replace(str, string.Empty);
4548
[GeneratedRegex("[^a-zA-Z0-9 ]")] public static partial Regex AlphaNumericAndSpaceRegex();
4649

4750
/// <summary>
48-
/// Keep Alpha, Numeric, Space & Period
51+
/// Keep Alpha, Numeric, Space &amp; Period.
4952
/// </summary>
5053
/// <param name="str"></param>
5154
/// <returns></returns>
5255
public static string KeepAlphaNumericSpaceAndPeriod(this string str) => AlphaNumericSpaceAndPeriodRegex().Replace(str, string.Empty);
5356
[GeneratedRegex("[^a-zA-Z0-9. ]")] public static partial Regex AlphaNumericSpaceAndPeriodRegex();
5457

5558
/// <summary>
56-
/// Keep Only Alpha, Numeric, Spaces, & Excluded Symbols
59+
/// Keep Only Alpha, Numeric, Spaces, &amp; Excluded Symbols.
5760
/// </summary>
5861
/// <param name="str"></param>
5962
/// <param name="exceptions"></param>

TJC.StringExtensions/Pluralize/PluralizeExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace TJC.StringExtensions.Pluralize;
22

3+
/// <summary>
4+
/// Extensions for pluralizing strings.
5+
/// </summary>
36
public static class PluralizeExtensions
47
{
58
/// <summary>

0 commit comments

Comments
 (0)