diff --git a/src/Rhythm.Core/StringExtensionMethods.cs b/src/Rhythm.Core/StringExtensionMethods.cs index 9aa94a6..288758e 100644 --- a/src/Rhythm.Core/StringExtensionMethods.cs +++ b/src/Rhythm.Core/StringExtensionMethods.cs @@ -133,10 +133,10 @@ public static string ToSnakeCase(this string source) } /// - /// 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"). /// /// - /// The snake case string to convert. + /// The string to convert. /// /// /// The camel case string. @@ -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(); } /// diff --git a/src/Tests/StringTests.cs b/src/Tests/StringTests.cs index 0ce29f6..633f6e2 100644 --- a/src/Tests/StringTests.cs +++ b/src/Tests/StringTests.cs @@ -18,12 +18,14 @@ public class StringTests /// Test if camel case works. /// [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); } - /// /// Test if pascal case works. ///