Skip to content

Commit

Permalink
Merge pull request #37 from MehdiK/ToQuantity
Browse files Browse the repository at this point in the history
Added ToQuantity - fixes issue #32
  • Loading branch information
MehdiK committed Dec 30, 2013
2 parents 9e6e167 + 6cb6019 commit 1784710
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="CasingTests.cs" />
<Compile Include="Localisation\DateHumanizeTests.nb-NO.cs" />
<Compile Include="RunnableInDebugModeOnlyAttribute.cs" />
<Compile Include="ToQuantityTests.cs" />
<Compile Include="TransformersTests.cs" />
<Compile Include="NumberToOrdinalWordsTests.cs" />
<Compile Include="Localisation\DateHumanizeTests.ro-RO.cs" />
Expand Down
21 changes: 21 additions & 0 deletions src/Humanizer.Tests/ToQuantityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class ToQuantityTests
{

[Theory]
[InlineData("case", 0, "0 cases")]
[InlineData("case", 1, "1 case")]
[InlineData("case", 5, "5 cases")]
[InlineData("man", 0, "0 men")]
[InlineData("man", 1, "1 man")]
[InlineData("man", 2, "2 men")]
public void ToQuantity(string word, int quatity, string expected)
{
Assert.Equal(expected, word.ToQuantity(quatity));
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>On.Days.tt</DependentUpon>
</Compile>
<Compile Include="ToQuantityExtensions.cs" />
<Compile Include="Transformer\To.cs" />
<Compile Include="Transformer\IStringTransformer.cs" />
<Compile Include="NumberToOrdinalWordsExtension.cs" />
Expand Down
19 changes: 19 additions & 0 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Humanizer
{
public static class ToQuantityExtensions
{
/// <summary>
/// "request".ToQuantity(0) => "0 requests", "request".ToQuantity(1) => "1 request", "request".ToQuantity(2) => "2 requests"
/// </summary>
/// <param name="input"></param>
/// <param name="quantity"></param>
/// <returns></returns>
public static string ToQuantity(this string input, int quantity)
{
if (quantity == 1)
return string.Format("{0} {1}", quantity, input);

return string.Format("{0} {1}", quantity, input.Pluralize());
}
}
}

0 comments on commit 1784710

Please sign in to comment.