diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index 02f7764dd..f7828b81a 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -73,6 +73,7 @@ + diff --git a/src/Humanizer.Tests/ToQuantityTests.cs b/src/Humanizer.Tests/ToQuantityTests.cs new file mode 100644 index 000000000..c62487d52 --- /dev/null +++ b/src/Humanizer.Tests/ToQuantityTests.cs @@ -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)); + } + } +} diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index feb688697..66c323abd 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -82,6 +82,7 @@ True On.Days.tt + diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs new file mode 100644 index 000000000..cbe335085 --- /dev/null +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -0,0 +1,19 @@ +namespace Humanizer +{ + public static class ToQuantityExtensions + { + /// + /// "request".ToQuantity(0) => "0 requests", "request".ToQuantity(1) => "1 request", "request".ToQuantity(2) => "2 requests" + /// + /// + /// + /// + 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()); + } + } +}