Skip to content

Commit

Permalink
cleanup on #408
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed May 24, 2015
1 parent fd2dc65 commit d334fbb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,19 @@ public class In

public class Vocabularies
{
public Vocabularies() { }
public Humanizer.Inflections.Vocabulary Default { get; }
}

public class Vocabulary
{
public Vocabulary() { }
public void AddIrregular(string singular, string plural) { }
public void AddPlural(string rule, string replacement) { }
public void AddSingular(string rule, string replacement) { }
public void AddUncountable(string word) { }
public string Pluralize(string word, bool inputIsKnownToBeSingular) { }
public string Pluralize(string word, Humanizer.Plurality plurality) { }
public string Singularize(string word, bool inputIsKnownToBePlural) { }
public string Singularize(string word, Humanizer.Plurality plurality) { }
}

public class InflectorExtensions
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer/Inflections/Vocabularies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Humanizer.Inflections
/// <summary>
/// Container for registered Vocabularies. At present, only a single vocabulary is supported: Default.
/// </summary>
public class Vocabularies
public static class Vocabularies
{
private static Vocabulary _default;

Expand Down
34 changes: 29 additions & 5 deletions src/Humanizer/Inflections/Vocabulary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

Expand All @@ -10,6 +11,10 @@ namespace Humanizer.Inflections
/// </summary>
public class Vocabulary
{
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>();
Expand Down Expand Up @@ -75,6 +80,18 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
return result;
}

/// <summary>
/// Pluralizes the provided input considering irregular words
/// </summary>
/// <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>
[Obsolete("Use string.Pluralize(bool) instead. This method will be removed in next major release.")]
public string Pluralize(string word, Plurality plurality)
{
return plurality == Plurality.Plural ? word : Pluralize(word, inputIsKnownToBeSingular: false);
}

/// <summary>
/// Singularizes the provided input considering irregular words
/// </summary>
Expand All @@ -83,7 +100,6 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true)
/// <returns></returns>
public string Singularize(string word, bool inputIsKnownToBePlural = true)
{

var result = ApplyRules(_singulars, word);

if (inputIsKnownToBePlural)
Expand All @@ -98,6 +114,18 @@ public string Singularize(string word, bool inputIsKnownToBePlural = true)
return result ?? word;
}

/// <summary>
/// Singularizes the provided input considering irregular words
/// </summary>
/// <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>
[Obsolete("Use string.Singularize(bool) instead. This method will be removed in next major release.")]
public string Singularize(string word, Plurality plurality)
{
return plurality == Plurality.Singular ? word : Singularize(word, inputIsKnownToBePlural: false);
}

private string ApplyRules(IList<Rule> rules, string word)
{
if (word == null)
Expand All @@ -110,9 +138,7 @@ private string ApplyRules(IList<Rule> rules, string word)
for (int i = rules.Count - 1; i >= 0; i--)
{
if ((result = rules[i].Apply(word)) != null)
{
break;
}
}
return result;
}
Expand All @@ -136,9 +162,7 @@ public Rule(string pattern, string replacement)
public string Apply(string word)
{
if (!_regex.IsMatch(word))
{
return null;
}

return _regex.Replace(word, _replacement);
}
Expand Down

0 comments on commit d334fbb

Please sign in to comment.