From fad7148cc08b55675dddd98c4ce1392369fa543d Mon Sep 17 00:00:00 2001 From: Christopher Redekop Date: Tue, 10 Feb 2015 08:01:08 -0500 Subject: [PATCH 1/2] Fixes trailing question mark reported in #378. --- src/Humanizer.Tests/StringDehumanizeTests.cs | 3 +++ src/Humanizer.Tests/StringHumanizeTests.cs | 6 ++++++ src/Humanizer/StringHumanizeExtensions.cs | 15 +++++---------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Humanizer.Tests/StringDehumanizeTests.cs b/src/Humanizer.Tests/StringDehumanizeTests.cs index 9e5574d8b..54aea0b01 100644 --- a/src/Humanizer.Tests/StringDehumanizeTests.cs +++ b/src/Humanizer.Tests/StringDehumanizeTests.cs @@ -12,6 +12,9 @@ public class StringDehumanizeTests [InlineData("lower case sentence is camelized", "LowerCaseSentenceIsCamelized")] [InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")] [InlineData("", "")] + [InlineData("A special character is removed?", "ASpecialCharacterIsRemoved")] + [InlineData("A special character is removed after a space ?", "ASpecialCharacterIsRemovedAfterASpace")] + [InlineData("Internal special characters ?)@ are removed", "InternalSpecialCharactersAreRemoved")] public void CanDehumanizeIntoAPascalCaseWord(string input, string expectedResult) { Assert.Equal(expectedResult, input.Dehumanize()); diff --git a/src/Humanizer.Tests/StringHumanizeTests.cs b/src/Humanizer.Tests/StringHumanizeTests.cs index 0d3ce49c9..515de1420 100644 --- a/src/Humanizer.Tests/StringHumanizeTests.cs +++ b/src/Humanizer.Tests/StringHumanizeTests.cs @@ -12,6 +12,12 @@ public class StringHumanizeTests [InlineData("NumberIsAtTheEnd100", "Number is at the end 100")] [InlineData("XIsFirstWordInTheSentence", "X is first word in the sentence")] [InlineData("XIsFirstWordInTheSentence ThenThereIsASpace", "X is first word in the sentence then there is a space")] + [InlineData("ContainsSpecial?)@Characters", "Contains special characters")] + [InlineData("a", "A")] + [InlineData("A", "A")] + [InlineData("?)@", "")] + [InlineData("?", "")] + [InlineData("", "")] public void CanHumanizeStringInPascalCase(string input, string expectedResult) { Assert.Equal(expectedResult, input.Humanize()); diff --git a/src/Humanizer/StringHumanizeExtensions.cs b/src/Humanizer/StringHumanizeExtensions.cs index d438222f0..3cb065646 100644 --- a/src/Humanizer/StringHumanizeExtensions.cs +++ b/src/Humanizer/StringHumanizeExtensions.cs @@ -26,20 +26,15 @@ static string FromUnderscoreDashSeparatedWords (string input) static string FromPascalCase(string input) { - if (input.Length == 1) - return Char.ToUpper(input[0]).ToString(); - - var result = PascalCaseWordPartsRegex + var result = String.Join(" ", PascalCaseWordPartsRegex .Matches(input).Cast() .Select(match => match.Value.ToCharArray().All(Char.IsUpper) && (match.Value.Length > 1 || (match.Index > 0 && input[match.Index - 1] == ' ') || match.Value == "I") ? match.Value - : match.Value.ToLower()) - .Aggregate((res, word) => res + " " + word); + : match.Value.ToLower())); - result = Char.ToUpper(result[0]) + - result.Substring(1, result.Length - 1); - return result; + return result.Length > 0 ? Char.ToUpper(result[0]) + + result.Substring(1, result.Length - 1) : result; } /// @@ -53,7 +48,7 @@ public static string Humanize(this string input) if (input.ToCharArray().All(Char.IsUpper)) return input; - // if input contains a dash or underscore which preceeds or follows a space (or both, i.g. free-standing) + // if input contains a dash or underscore which preceeds or follows a space (or both, e.g. free-standing) // remove the dash/underscore and run it through FromPascalCase if (FreestandingSpacingCharRegex.IsMatch(input)) return FromPascalCase(FromUnderscoreDashSeparatedWords(input)); From a650caf9a361ae4cf3fa0a5c895e8bc10f262c43 Mon Sep 17 00:00:00 2001 From: Christopher Redekop Date: Wed, 11 Feb 2015 08:32:34 -0500 Subject: [PATCH 2/2] Fixes trailing question mark reported in #378; updated the release notes. --- release_notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release_notes.md b/release_notes.md index 5978ba7e3..e7cf714a0 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,5 +1,5 @@ ###In Development - + - [#381](https://github.com/MehdiK/Humanizer/pull/381): Fixes trailing question mark reported in #378. [Commits](https://github.com/MehdiK/Humanizer/compare/v1.33.7...master)