From 12e0593d1e19eff002e69f9137bbee682cacd8ed Mon Sep 17 00:00:00 2001 From: Neil Boyd Date: Wed, 16 Feb 2022 07:25:52 +0100 Subject: [PATCH 1/2] test cases for single letter --- src/Humanizer.Tests.Shared/InflectorTests.cs | 27 +++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Humanizer.Tests.Shared/InflectorTests.cs b/src/Humanizer.Tests.Shared/InflectorTests.cs index a4218c67d..3c688613c 100644 --- a/src/Humanizer.Tests.Shared/InflectorTests.cs +++ b/src/Humanizer.Tests.Shared/InflectorTests.cs @@ -71,6 +71,19 @@ public void SingularizeSkipSimpleWords(string singular, string plural) Assert.Equal(singular, plural.Singularize(skipSimpleWords: true)); } + [Theory] + [InlineData("a")] + [InlineData("A")] + [InlineData("s")] + [InlineData("S")] + [InlineData("z")] + [InlineData("Z")] + [InlineData("1")] + public void SingularizeSingleLetter(string input) + { + Assert.Equal(input, input.Singularize()); + } + //Uppercases individual words and removes some characters [Theory] [InlineData("some title", "Some Title")] @@ -280,6 +293,7 @@ public IEnumerator GetEnumerator() yield return new object[] { "alumna", "alumnae" }; yield return new object[] { "alumnus", "alumni" }; yield return new object[] { "fungus", "fungi" }; + yield return new object[] { "water", "water" }; yield return new object[] { "waters", "waters" }; yield return new object[] { "semen", "semen" }; @@ -377,11 +391,18 @@ public IEnumerator GetEnumerator() yield return new object[] { "hoe", "hoes" }; yield return new object[] { "toe", "toes" }; yield return new object[] { "woe", "woes" }; - // Duplicated test case - Already added by Bas Jansen - // yield return new object[] { "potato", "potatoes" }; - //Issue 1132 + //Issue #1132 yield return new object[] { "metadata", "metadata" }; + + //Issue #1154 + yield return new object[] { "a", "as" }; + yield return new object[] { "A", "As" }; + yield return new object[] { "s", "ss" }; + yield return new object[] { "S", "Ss" }; + yield return new object[] { "z", "zs" }; + yield return new object[] { "Z", "Zs" }; + yield return new object[] { "1", "1s" }; } IEnumerator IEnumerable.GetEnumerator() From 784cb60796253315fc9fd055b9ad36adb31b41e5 Mon Sep 17 00:00:00 2001 From: Neil Boyd Date: Sun, 20 Feb 2022 16:41:04 +0100 Subject: [PATCH 2/2] special case for letter s --- src/Humanizer/Inflections/Vocabulary.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Humanizer/Inflections/Vocabulary.cs b/src/Humanizer/Inflections/Vocabulary.cs index e78ea5c71..c345320b8 100644 --- a/src/Humanizer/Inflections/Vocabulary.cs +++ b/src/Humanizer/Inflections/Vocabulary.cs @@ -17,6 +17,7 @@ internal Vocabulary() private readonly List _plurals = new List(); private readonly List _singulars = new List(); private readonly List _uncountables = new List(); + private readonly Regex _letterS = new Regex("^([sS])[sS]*$"); /// /// Adds a word to the vocabulary which cannot easily be pluralized/singularized by RegEx, e.g. "person" and "people". @@ -75,6 +76,12 @@ public void AddSingular(string rule, string replacement) /// public string Pluralize(string word, bool inputIsKnownToBeSingular = true) { + var s = LetterS(word); + if (s != null) + { + return s + "s"; + } + var result = ApplyRules(_plurals, word, false); if (inputIsKnownToBeSingular) @@ -101,6 +108,12 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true) /// public string Singularize(string word, bool inputIsKnownToBePlural = true, bool skipSimpleWords = false) { + var s = LetterS(word); + if (s != null) + { + return s; + } + var result = ApplyRules(_singulars, word, skipSimpleWords); if (inputIsKnownToBePlural) @@ -158,6 +171,15 @@ private string MatchUpperCase(string word, string replacement) return char.IsUpper(word[0]) && char.IsLower(replacement[0]) ? char.ToUpper(replacement[0]) + replacement.Substring(1) : replacement; } + /// + /// If the word is the letter s, singular or plural, return the letter s singular + /// + private string LetterS(string word) + { + var s = _letterS.Match(word); + return s.Groups.Count > 1 ? s.Groups[1].Value : null; + } + private class Rule { private readonly Regex _regex;