From c673638874bbd0612598edd22c125d96acf38b4c Mon Sep 17 00:00:00 2001 From: Gordon Date: Sun, 12 Sep 2021 22:01:47 +0100 Subject: [PATCH] Fixed issue #1102. Dehumanize() by pascalizing the words. Also expanded the tests to catch these edge cases --- .../StringDehumanizeTests.cs | 21 ++++++++++++++----- src/Humanizer/StringDehumanizeExtensions.cs | 10 +++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Humanizer.Tests.Shared/StringDehumanizeTests.cs b/src/Humanizer.Tests.Shared/StringDehumanizeTests.cs index 80e5d441e..f906f170c 100644 --- a/src/Humanizer.Tests.Shared/StringDehumanizeTests.cs +++ b/src/Humanizer.Tests.Shared/StringDehumanizeTests.cs @@ -5,15 +5,26 @@ namespace Humanizer.Tests public class StringDehumanizeTests { [Theory] - [InlineData("Pascal case sentence is camelized", "PascalCaseSentenceIsCamelized")] - [InlineData("Title Case Sentence Is Camelized", "TitleCaseSentenceIsCamelized")] - [InlineData("Mixed case sentence Is Camelized", "MixedCaseSentenceIsCamelized")] - [InlineData("lower case sentence is camelized", "LowerCaseSentenceIsCamelized")] - [InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")] [InlineData("", "")] + [InlineData("Pascal case sentence is pascalized", "PascalCaseSentenceIsPascalized")] + [InlineData("Title Case Sentence Is Pascalized", "TitleCaseSentenceIsPascalized")] + [InlineData("Mixed case sentence Is Pascalized", "MixedCaseSentenceIsPascalized")] + [InlineData("lower case sentence is pascalized", "LowerCaseSentenceIsPascalized")] [InlineData("A special character is removed?", "ASpecialCharacterIsRemoved")] [InlineData("A special character is removed after a space ?", "ASpecialCharacterIsRemovedAfterASpace")] [InlineData("Internal special characters ?)@ are removed", "InternalSpecialCharactersAreRemoved")] + [InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")] + [InlineData("CanDehumanizeIntoAPascalCaseWord", "CanDehumanizeIntoAPascalCaseWord")] + [InlineData("CanDehumanizeIntoAPascalCaseWord AndAnother", "CanDehumanizeIntoAPascalCaseWordAndAnother")] + [InlineData("OneAndTwo", "OneAndTwo")] + [InlineData("OneOrTwo", "OneOrTwo")] + [InlineData("OneOfTwo", "OneOfTwo")] + [InlineData("OneButTwo", "OneButTwo")] + [InlineData("OneATwo", "OneATwo")] + [InlineData("OneAsTwo", "OneAsTwo")] + [InlineData("OneYetTwo", "OneYetTwo")] + [InlineData("OneNorTwo", "OneNorTwo")] + [InlineData("WordSoTwo", "WordSoTwo")] public void CanDehumanizeIntoAPascalCaseWord(string input, string expectedResult) { Assert.Equal(expectedResult, input.Dehumanize()); diff --git a/src/Humanizer/StringDehumanizeExtensions.cs b/src/Humanizer/StringDehumanizeExtensions.cs index 92a6d76d7..7712b20ae 100644 --- a/src/Humanizer/StringDehumanizeExtensions.cs +++ b/src/Humanizer/StringDehumanizeExtensions.cs @@ -1,5 +1,7 @@ using System.Linq; +using Humanizer; + namespace Humanizer { /// @@ -9,13 +11,17 @@ public static class StringDehumanizeExtensions { /// /// Dehumanizes a string; e.g. 'some string', 'Some String', 'Some string' -> 'SomeString' + /// If a string is already dehumanized then it leaves it alone 'SomeStringAndAnotherString' -> 'SomeStringAndAnotherString' /// /// The string to be dehumanized /// public static string Dehumanize(this string input) { - var titlizedWords = input.Split(' ').Select(word => word.Humanize(LetterCasing.Title)); - return string.Join("", titlizedWords).Replace(" ", ""); + var pascalizedWords = input.Split(' ').Select(word => word.Humanize().Pascalize()); + return string.Join("", pascalizedWords).Replace(" ", ""); } } } + + +