Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add singularize/pluralize overload without using obsolete plurality enum #306

Merged
merged 5 commits into from
Jun 27, 2014
Merged
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
22 changes: 14 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,17 @@ There are also a few inflector methods:
"string".Pluralize() => "strings"
```

Normally you would call `Pluralize` on a singular word but if you're unsure about the singularity of the word you can call the method with the optional `plurality` argument:
Normally you would call `Pluralize` on a singular word but if you're unsure about the singularity of the word you can call the method with the optional `inputIsKnownToBeSingular` argument:

```C#
"Men".Pluralize(Plurality.CouldBeEither) => "Men"
"Man".Pluralize(Plurality.CouldBeEither) => "Men"
"string".Pluralize(Plurality.CouldBeEither) => "strings"
"Men".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"Man".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"string".Pluralize(inputIsKnownToBeSingular: false) => "strings"
```


The overload of `Pluralize` with `plurality` argument is obsolete and will be removed in next major release.

####<a id="singularize">Singularize</a>
`Singularize` singularizes the provided input while taking irregular and uncountable words into consideration:

Expand All @@ -384,14 +387,17 @@ Normally you would call `Pluralize` on a singular word but if you're unsure abou
"strings".Singularize() => "string"
```

Normally you would call `Singularize` on a plural word but if you're unsure about the plurality of the word you can call the method with the optional `plurality` argument:
Normally you would call `Singularize` on a plural word but if you're unsure about the plurality of the word you can call the method with the optional `inputIsKnownToBePlural` argument:

```C#
"Men".Singularize(Plurality.CouldBeEither) => "Man"
"Man".Singularize(Plurality.CouldBeEither) => "Man"
"strings".Singularize(Plurality.CouldBeEither) => "string"
"Men".Singularize(inputIsKnownToBePlural: false) => "Man"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a much better API. Thanks for the suggestion and for fixing it.

"Man".Singularize(inputIsKnownToBePlural: false) => "Man"
"strings".Singularize(inputIsKnownToBePlural: false) => "string"
```


The overload of `Singularize` with `plurality` argument is obsolete and will be removed in next major release.

####<a id="toquantity">ToQuantity</a>
Many times you want to call `Singularize` and `Pluralize` to prefix a word with a number; e.g. "2 requests", "3 men". `ToQuantity` prefixes the provided word with the number and accordingly pluralizes or singularizes the word:

Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###In Development
- [#306](https://github.com/MehdiK/Humanizer/pull/306): Added Singularize/Pluralize overload without using obsolete plurality enum

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.27.0...master)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public class InflectorExtensions
public string Hyphenate(string underscoredWord) { }
public string Pascalize(string input) { }
public string Pluralize(string word, Humanizer.Plurality plurality) { }
public string Pluralize(string word, bool inputIsKnownToBeSingular) { }
public string Singularize(string word, Humanizer.Plurality plurality) { }
public string Singularize(string word, bool inputIsKnownToBePlural) { }
public string Titleize(string input) { }
public string Underscore(string input) { }
}
Expand Down
22 changes: 4 additions & 18 deletions src/Humanizer.Tests/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,12 @@ public void Pluralize(string singular, string plural)
Assert.Equal(plural, singular.Pluralize());
}

[Theory]
[ClassData(typeof(PluralTestSource))]
public void PluralizeAlreadyPluralWord(string singular, string plural)
{
Assert.Equal(plural, plural.Pluralize(Plurality.Plural));
}

[Theory]
[ClassData(typeof(PluralTestSource))]
public void PluralizeWordsWithUnknownPlurality(string singular, string plural)
{
Assert.Equal(plural, plural.Pluralize(Plurality.CouldBeEither));
Assert.Equal(plural, singular.Pluralize(Plurality.CouldBeEither));
Assert.Equal(plural, plural.Pluralize(false));
Assert.Equal(plural, singular.Pluralize(false));
}

[Theory]
Expand All @@ -61,19 +54,12 @@ public void Singularize(string singular, string plural)
Assert.Equal(singular, plural.Singularize());
}

[Theory]
[ClassData(typeof(PluralTestSource))]
public void SingularizeAlreadySingularWord(string singular, string plural)
{
Assert.Equal(singular, singular.Singularize(Plurality.Singular));
}

[Theory]
[ClassData(typeof(PluralTestSource))]
public void SingularizeWordsWithUnknownSingularity(string singular, string plural)
{
Assert.Equal(singular, singular.Singularize(Plurality.CouldBeEither));
Assert.Equal(singular, plural.Singularize(Plurality.CouldBeEither));
Assert.Equal(singular, singular.Singularize(false));
Assert.Equal(singular, plural.Singularize(false));
}

//Uppercases individual words and removes some characters
Expand Down
38 changes: 29 additions & 9 deletions src/Humanizer/InflectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -168,14 +169,23 @@ private static void AddSingular(string rule, string replacement)
/// <param name="word">Word to be pluralized</param>
/// <param name="plurality">Normally you call Pluralize on singular words; but if you're unsure call it with Plurality.CouldBeEither</param>
/// <returns></returns>
public static string Pluralize(this string word, Plurality plurality = Plurality.Singular)
[Obsolete("Use string.Pluralize(bool) instead. This method will be removed in next major release.")]
public static string Pluralize(this string word, Plurality plurality)
{
if (plurality == Plurality.Plural)
return word;
return plurality == Plurality.Plural ? word : word.Pluralize(inputIsKnownToBeSingular: false);
}

/// <summary>
/// Pluralizes the provided input considering irregular words
/// </summary>
/// <param name="word">Word to be pluralized</param>
/// <param name="inputIsKnownToBeSingular">Normally you call Pluralize on singular words; but if you're unsure call it with false</param>
/// <returns></returns>
public static string Pluralize(this string word, bool inputIsKnownToBeSingular = true)
{
var result = ApplyRules(Plurals, word);

if (plurality == Plurality.Singular)
if (inputIsKnownToBeSingular)
return result;

var asSingular = ApplyRules(Singulars, word);
Expand All @@ -192,20 +202,30 @@ public static string Pluralize(this string word, Plurality plurality = Plurality
/// <param name="word">Word to be singularized</param>
/// <param name="plurality">Normally you call Singularize on plural words; but if you're unsure call it with Plurality.CouldBeEither</param>
/// <returns></returns>
public static string Singularize(this string word, Plurality plurality = Plurality.Plural)
[Obsolete("Use string.Singularize(bool) instead. This method will be removed in next major release.")]
public static string Singularize(this string word, Plurality plurality)
{
return plurality == Plurality.Singular ? word : word.Singularize(inputIsKnownToBePlural: false);
}

/// <summary>
/// Singularizes the provided input considering irregular words
/// </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>
/// <returns></returns>
public static string Singularize(this string word, bool inputIsKnownToBePlural = true)
{
if (plurality == Plurality.Singular)
return word;

var result = ApplyRules(Singulars, word);

if (plurality == Plurality.Plural)
if (inputIsKnownToBePlural)
return result;

// the Plurality is unknown so we should check all possibilities
var asPlural = ApplyRules(Plurals, word);
var asPluralAsSingular = ApplyRules(Singulars, asPlural);
if (asPlural != word && word+"s" != asPlural && asPluralAsSingular == word && result != word)
if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word)
return word;

return result ?? word;
Expand Down
4 changes: 2 additions & 2 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public static string ToQuantity(this string input, int quantity, string format,
private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
{
var transformedInput = quantity == 1
? input.Singularize(Plurality.CouldBeEither)
: input.Pluralize(Plurality.CouldBeEither);
? input.Singularize(inputIsKnownToBePlural: false)
: input.Pluralize(inputIsKnownToBeSingular: false);

if (showQuantityAs == ShowQuantityAs.None)
return transformedInput;
Expand Down