Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Rhythm.Core/StringExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ public static string ToSnakeCase(this string source)
}

/// <summary>
/// Converts a snake case string to camel case (e.g., converts "this-thing" to "thisThing").
/// Converts a string to camel case (e.g., converts "this-thing" to "thisThing").
/// </summary>
/// <param name="source">
/// The snake case string to convert.
/// The string to convert.
/// </param>
/// <returns>
/// The camel case string.
Expand All @@ -147,10 +147,18 @@ public static string ToCamelCase(this string source)
{
return null;
}
return DashAndCharRegex.Replace(source, x =>

var camelCase = DashAndCharRegex.Replace(source, x =>
{
return x.Groups["CHAR"].Value.ToUpper();
});

if (!string.IsNullOrEmpty(camelCase) && camelCase.Length > 1)
{
return char.ToLowerInvariant(camelCase[0]) + camelCase.Substring(1);
}

return camelCase.ToLowerInvariant();
}

/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions src/Tests/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public class StringTests
/// Test if camel case works.
/// </summary>
[TestMethod]
public void CamelCase()
[DataRow("hello-world-test", "helloWorldTest")]
[DataRow("HelloWorldTest", "helloWorldTest")]
[DataRow("A", "a")]
public void CamelCase(string input, string expected)
{
var result = "hello-world-test".ToCamelCase();
Assert.AreEqual("helloWorldTest", result);
var result = input.ToCamelCase();
Assert.AreEqual(expected, result);
}

/// <summary>
/// Test if pascal case works.
/// </summary>
Expand Down