Skip to content

Commit 6a8b106

Browse files
author
Oren Novotny
authored
Merge pull request #869 from kendallb/master
Added support for ignoring singularization on simple words
2 parents 69b8651 + fc887b3 commit 6a8b106

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/Humanizer.Tests.Shared/InflectorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public void SingularizeWordsWithUnknownSingularity(string singular, string plura
6161
Assert.Equal(singular, plural.Singularize(false));
6262
}
6363

64+
[Theory]
65+
[InlineData("tires", "tires")]
66+
[InlineData("body", "bodies")]
67+
[InlineData("traxxas", "traxxas")]
68+
public void SingularizeSkipSimpleWords(string singular, string plural)
69+
{
70+
Assert.Equal(singular, plural.Singularize(skipSimpleWords: true));
71+
}
72+
6473
//Uppercases individual words and removes some characters
6574
[Theory]
6675
[InlineData("some title", "Some Title")]

src/Humanizer.Tests/Humanizer.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
3+
<TargetFrameworks>net48;netcoreapp2.1</TargetFrameworks>
44
</PropertyGroup>
55

66
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' ">
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
1414
<ProjectReference Include="..\Humanizer\Humanizer.csproj" />
1515
</ItemGroup>
16-
<ItemGroup Condition="'$(TargetFramework)' == 'net472' ">
16+
<ItemGroup Condition="'$(TargetFramework)' == 'net48' ">
1717
<Reference Include="System.ComponentModel.DataAnnotations" />
1818
<PackageReference Include="ApiApprover" Version="9.3.0" />
1919
<PackageReference Include="DiffPlex" Version="1.4.4" />

src/Humanizer/Inflections/Vocabulary.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Text.RegularExpressions;
33

44
namespace Humanizer.Inflections
@@ -75,15 +75,15 @@ public void AddSingular(string rule, string replacement)
7575
/// <returns></returns>
7676
public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
7777
{
78-
var result = ApplyRules(_plurals, word);
78+
var result = ApplyRules(_plurals, word, false);
7979

8080
if (inputIsKnownToBeSingular)
8181
{
82-
return result;
82+
return result ?? word;
8383
}
8484

85-
var asSingular = ApplyRules(_singulars, word);
86-
var asSingularAsPlural = ApplyRules(_plurals, asSingular);
85+
var asSingular = ApplyRules(_singulars, word, false);
86+
var asSingularAsPlural = ApplyRules(_plurals, asSingular, false);
8787
if (asSingular != null && asSingular != word && asSingular + "s" != word && asSingularAsPlural == word && result != word)
8888
{
8989
return word;
@@ -97,19 +97,20 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
9797
/// </summary>
9898
/// <param name="word">Word to be singularized</param>
9999
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
100+
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
100101
/// <returns></returns>
101-
public string Singularize(string word, bool inputIsKnownToBePlural = true)
102+
public string Singularize(string word, bool inputIsKnownToBePlural = true, bool skipSimpleWords = false)
102103
{
103-
var result = ApplyRules(_singulars, word);
104+
var result = ApplyRules(_singulars, word, skipSimpleWords);
104105

105106
if (inputIsKnownToBePlural)
106107
{
107-
return result;
108+
return result ?? word;
108109
}
109110

110111
// the Plurality is unknown so we should check all possibilities
111-
var asPlural = ApplyRules(_plurals, word);
112-
var asPluralAsSingular = ApplyRules(_singulars, asPlural);
112+
var asPlural = ApplyRules(_plurals, word, false);
113+
var asPluralAsSingular = ApplyRules(_singulars, asPlural, false);
113114
if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word)
114115
{
115116
return word;
@@ -118,7 +119,7 @@ public string Singularize(string word, bool inputIsKnownToBePlural = true)
118119
return result ?? word;
119120
}
120121

121-
private string ApplyRules(IList<Rule> rules, string word)
122+
private string ApplyRules(IList<Rule> rules, string word, bool skipFirstRule)
122123
{
123124
if (word == null)
124125
{
@@ -131,7 +132,8 @@ private string ApplyRules(IList<Rule> rules, string word)
131132
}
132133

133134
var result = word;
134-
for (var i = rules.Count - 1; i >= 0; i--)
135+
var end = skipFirstRule ? 1 : 0;
136+
for (var i = rules.Count - 1; i >= end; i--)
135137
{
136138
if ((result = rules[i].Apply(word)) != null)
137139
{
@@ -168,4 +170,4 @@ public string Apply(string word)
168170
}
169171
}
170172
}
171-
}
173+
}

src/Humanizer/InflectorExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public static string Pluralize(this string word, bool inputIsKnownToBeSingular =
4747
/// </summary>
4848
/// <param name="word">Word to be singularized</param>
4949
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
50+
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
5051
/// <returns></returns>
51-
public static string Singularize(this string word, bool inputIsKnownToBePlural = true)
52+
public static string Singularize(this string word, bool inputIsKnownToBePlural = true, bool skipSimpleWords = false)
5253
{
53-
return Vocabularies.Default.Singularize(word, inputIsKnownToBePlural);
54+
return Vocabularies.Default.Singularize(word, inputIsKnownToBePlural, skipSimpleWords);
5455
}
5556

5657
/// <summary>

0 commit comments

Comments
 (0)