Skip to content

Commit

Permalink
Merge pull request #1178 from neilboyd/singularize-single-letter
Browse files Browse the repository at this point in the history
  • Loading branch information
clairernovotny authored Mar 1, 2022
2 parents 4fe70a5 + 784cb60 commit 6374139
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/Humanizer.Tests.Shared/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -280,6 +293,7 @@ public IEnumerator<object[]> 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" };
Expand Down Expand Up @@ -377,11 +391,18 @@ public IEnumerator<object[]> 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()
Expand Down
22 changes: 22 additions & 0 deletions src/Humanizer/Inflections/Vocabulary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal Vocabulary()
private readonly List<Rule> _plurals = new List<Rule>();
private readonly List<Rule> _singulars = new List<Rule>();
private readonly List<string> _uncountables = new List<string>();
private readonly Regex _letterS = new Regex("^([sS])[sS]*$");

/// <summary>
/// Adds a word to the vocabulary which cannot easily be pluralized/singularized by RegEx, e.g. "person" and "people".
Expand Down Expand Up @@ -75,6 +76,12 @@ public void AddSingular(string rule, string replacement)
/// <returns></returns>
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)
Expand All @@ -101,6 +108,12 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
/// <returns></returns>
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)
Expand Down Expand Up @@ -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;
}

/// <summary>
/// If the word is the letter s, singular or plural, return the letter s singular
/// </summary>
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;
Expand Down

0 comments on commit 6374139

Please sign in to comment.