Skip to content

Commit

Permalink
Merge pull request #1513 from liquidboy/issue-1480
Browse files Browse the repository at this point in the history
fix #1480 - ToCamelCase broken for a single word with multiple upperc…
  • Loading branch information
alexeyzimarev committed Oct 23, 2020
2 parents f9aa955 + 1b7dd95 commit 4cd6eb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/RestSharp/Extensions/StringExtensions.cs
Expand Up @@ -222,9 +222,12 @@ public static string ToPascalCase(this string text, bool removeUnderscores, Cult

string CaseWord(string word)
{
var restOfWord = word.Substring(1).ToLower(culture);
var restOfWord = word.Substring(1);
var firstChar = char.ToUpper(word[0], culture);

if (restOfWord.IsUpperCase())
restOfWord = restOfWord.ToLower(culture);

return string.Concat(firstChar, restOfWord);
}
}
Expand Down Expand Up @@ -274,6 +277,13 @@ public static string AddDashes(this string pascalCasedWord)
"-"
);

/// <summary>
/// Checks to see if a string is all uppper case
/// </summary>
/// <param name="inputString">String to check</param>
/// <returns>bool</returns>
public static bool IsUpperCase(this string inputString) => IsUpperCaseRegex.IsMatch(inputString);

/// <summary>
/// Add an underscore prefix to a pascal-cased string
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions test/RestSharp.Tests/StringExtensionsTests.cs
Expand Up @@ -66,6 +66,14 @@ public void ToPascalCase(string start, bool removeUnderscores, string finish)
Assert.AreEqual(finish, result);
}

[Test, TestCase("DueDate", "dueDate"), TestCase("ID", "id"), TestCase("IDENTIFIER", "identifier"), TestCase("primaryId", "primaryId"), TestCase("A", "a"), TestCase("ThisIsATest", "thisIsATest")]
public void ToCamelCase(string start, string finish)
{
var result = start.ToCamelCase(CultureInfo.InvariantCulture);

Assert.AreEqual(finish, result);
}

[Test]
public void Does_not_throw_on_invalid_encoding()
{
Expand Down

0 comments on commit 4cd6eb5

Please sign in to comment.