Skip to content

Commit

Permalink
Fixing #38 - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed Dec 30, 2013
1 parent 1784710 commit 7129726
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
40 changes: 38 additions & 2 deletions src/Humanizer.Tests/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Pluralize()
{
foreach (var pair in TestData)
{
Assert.Equal(pair.Key.Pluralize(), pair.Value);
Assert.Equal(pair.Value, pair.Key.Pluralize());
}
}

Expand All @@ -22,7 +22,43 @@ public void Singularize()
{
foreach (var pair in TestData)
{
Assert.Equal(pair.Value.Singularize(), pair.Key);
Assert.Equal(pair.Key, pair.Value.Singularize());
}
}

[Fact]
public void IsPlural()
{
foreach (var pair in TestData)
{
Assert.True(pair.Value.IsPlural());
}
}

[Fact]
public void IsSingular()
{
foreach (var pair in TestData)
{
Assert.True(pair.Key.IsSingular());
}
}

[Fact]
public void CanSingularizeSingularWords()
{
foreach (var pair in TestData)
{
Assert.Equal(pair.Key, pair.Key.Singularize());
}
}

[Fact]
public void CanPluralizePluralWords()
{
foreach (var pair in TestData)
{
Assert.Equal(pair.Value, pair.Value.Pluralize());
}
}

Expand Down
53 changes: 51 additions & 2 deletions src/Humanizer/InflectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public string Apply(string word)

return _regex.Replace(word, _replacement);
}

public bool IsMatch(string word)
{
return _regex.IsMatch(word);
}
}

private static void AddIrregular(string singular, string plural)
Expand Down Expand Up @@ -127,7 +132,10 @@ private static void AddSingular(string rule, string replacement)
/// <returns></returns>
public static string Pluralize(this string word)
{
return ApplyRules(Plurals, word);
if (!word.IsPlural())
return ApplyRules(Plurals, word);

return word;
}

/// <summary>
Expand All @@ -137,7 +145,48 @@ public static string Pluralize(this string word)
/// <returns></returns>
public static string Singularize(this string word)
{
return ApplyRules(Singulars, word);
if (!word.IsSingular())
return ApplyRules(Singulars, word);

return word;
}

/// <summary>
/// Returns whether the provided word is plural or not
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
public static bool IsPlural(this string word)
{
if (Uncountables.Contains(word.ToLowerInvariant()))
return true;

foreach (var singularizationRule in Singulars)
{
if (singularizationRule.IsMatch(word))
return true;
}

return false;
}

/// <summary>
/// Returns whether the provided word is singular or not
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
public static bool IsSingular(this string word)
{
if (Uncountables.Contains(word.ToLowerInvariant()))
return true;

foreach (var pluralizatinoRule in Plurals)
{
if (pluralizatinoRule.IsMatch(word))
return true;
}

return false;
}

private static string ApplyRules(List<Rule> rules, string word)
Expand Down

0 comments on commit 7129726

Please sign in to comment.