Skip to content

Commit

Permalink
Merge pull request #869 from kendallb/master
Browse files Browse the repository at this point in the history
Added support for ignoring singularization on simple words
  • Loading branch information
Oren Novotny committed Nov 13, 2019
2 parents 69b8651 + fc887b3 commit 6a8b106
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/Humanizer.Tests.Shared/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public void SingularizeWordsWithUnknownSingularity(string singular, string plura
Assert.Equal(singular, plural.Singularize(false));
}

[Theory]
[InlineData("tires", "tires")]
[InlineData("body", "bodies")]
[InlineData("traxxas", "traxxas")]
public void SingularizeSkipSimpleWords(string singular, string plural)
{
Assert.Equal(singular, plural.Singularize(skipSimpleWords: true));
}

//Uppercases individual words and removes some characters
[Theory]
[InlineData("some title", "Some Title")]
Expand Down
4 changes: 2 additions & 2 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net48;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' ">
Expand All @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<ProjectReference Include="..\Humanizer\Humanizer.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net472' ">
<ItemGroup Condition="'$(TargetFramework)' == 'net48' ">
<Reference Include="System.ComponentModel.DataAnnotations" />
<PackageReference Include="ApiApprover" Version="9.3.0" />
<PackageReference Include="DiffPlex" Version="1.4.4" />
Expand Down
28 changes: 15 additions & 13 deletions src/Humanizer/Inflections/Vocabulary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Humanizer.Inflections
Expand Down Expand Up @@ -75,15 +75,15 @@ public void AddSingular(string rule, string replacement)
/// <returns></returns>
public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
{
var result = ApplyRules(_plurals, word);
var result = ApplyRules(_plurals, word, false);

if (inputIsKnownToBeSingular)
{
return result;
return result ?? word;
}

var asSingular = ApplyRules(_singulars, word);
var asSingularAsPlural = ApplyRules(_plurals, asSingular);
var asSingular = ApplyRules(_singulars, word, false);
var asSingularAsPlural = ApplyRules(_plurals, asSingular, false);
if (asSingular != null && asSingular != word && asSingular + "s" != word && asSingularAsPlural == word && result != word)
{
return word;
Expand All @@ -97,19 +97,20 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
/// </summary>
/// <param name="word">Word to be singularized</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
/// <returns></returns>
public string Singularize(string word, bool inputIsKnownToBePlural = true)
public string Singularize(string word, bool inputIsKnownToBePlural = true, bool skipSimpleWords = false)
{
var result = ApplyRules(_singulars, word);
var result = ApplyRules(_singulars, word, skipSimpleWords);

if (inputIsKnownToBePlural)
{
return result;
return result ?? word;
}

// the Plurality is unknown so we should check all possibilities
var asPlural = ApplyRules(_plurals, word);
var asPluralAsSingular = ApplyRules(_singulars, asPlural);
var asPlural = ApplyRules(_plurals, word, false);
var asPluralAsSingular = ApplyRules(_singulars, asPlural, false);
if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word)
{
return word;
Expand All @@ -118,7 +119,7 @@ public string Singularize(string word, bool inputIsKnownToBePlural = true)
return result ?? word;
}

private string ApplyRules(IList<Rule> rules, string word)
private string ApplyRules(IList<Rule> rules, string word, bool skipFirstRule)
{
if (word == null)
{
Expand All @@ -131,7 +132,8 @@ private string ApplyRules(IList<Rule> rules, string word)
}

var result = word;
for (var i = rules.Count - 1; i >= 0; i--)
var end = skipFirstRule ? 1 : 0;
for (var i = rules.Count - 1; i >= end; i--)
{
if ((result = rules[i].Apply(word)) != null)
{
Expand Down Expand Up @@ -168,4 +170,4 @@ public string Apply(string word)
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/Humanizer/InflectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public static string Pluralize(this string word, bool inputIsKnownToBeSingular =
/// </summary>
/// <param name="word">Word to be singularized</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
/// <returns></returns>
public static string Singularize(this string word, bool inputIsKnownToBePlural = true)
public static string Singularize(this string word, bool inputIsKnownToBePlural = true, bool skipSimpleWords = false)
{
return Vocabularies.Default.Singularize(word, inputIsKnownToBePlural);
return Vocabularies.Default.Singularize(word, inputIsKnownToBePlural, skipSimpleWords);
}

/// <summary>
Expand Down

0 comments on commit 6a8b106

Please sign in to comment.