Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Humanizer.Tests/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public void PluralizeWordsWithUnknownPlurality(string singular, string plural)
Assert.Equal(plural, singular.Pluralize(false));
}

[Fact]
public void Pluralize_Preserves_AllCaps_Suffix()
{
Assert.Equal("SINGULAR TYPE NAMES", "SINGULAR TYPE NAME".Pluralize());
Assert.Equal("SINGULAR TYPE NAMES", "singular type name".Humanize(LetterCasing.AllCaps).Pluralize());
}

[Theory]
[ClassData(typeof(PluralTestSource))]
public void Singularize(string singular, string plural) =>
Expand Down
33 changes: 30 additions & 3 deletions src/Humanizer/Inflections/Vocabulary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,36 @@ public void AddSingular(string rule, string replacement) =>
bool IsUncountable(string word) =>
uncountables.Contains(word);

static string MatchUpperCase(string word, string replacement) =>
char.IsUpper(word[0]) &&
char.IsLower(replacement[0]) ? StringHumanizeExtensions.Concat(char.ToUpper(replacement[0]), replacement.AsSpan(1)) : replacement;
static string MatchUpperCase(string word, string replacement)
{
var lettersCount = 0;
var allLettersUpper = true;
foreach (var c in word)
{
if (char.IsLetter(c))
{
lettersCount++;
if (!char.IsUpper(c))
{
allLettersUpper = false;
break;
}
}
}

// If the input contains multiple letters and all of them are uppercase
// (e.g. an ALL CAPS phrase or acronym of length > 1), return the replacement
// fully uppercased so the plural suffix matches the input casing.
if (lettersCount > 1 && allLettersUpper)
{
return replacement.ToUpperInvariant();
}

return char.IsUpper(word[0]) && char.IsLower(replacement[0])
? StringHumanizeExtensions.Concat(char.ToUpper(replacement[0]), replacement.AsSpan(1))
: replacement;
}


/// <summary>
/// If the word is the letter s, singular or plural, return the letter s singular
Expand Down