From 5ad2074837866ce2c7ba0904ce9eab2ce7158839 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Fri, 17 Jan 2014 16:52:57 +0330 Subject: [PATCH] Added ShowQuantityAs parameter to ToQuantity - #61 --- readme.md | 7 ++++ src/Humanizer.Tests/ToQuantityTests.cs | 53 ++++++++++++++++++++++++++ src/Humanizer/ToQuantityExtensions.cs | 22 ++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 79c069680..2c0396a18 100644 --- a/readme.md +++ b/readme.md @@ -187,6 +187,13 @@ Many times you want to call `Singularize` and `Pluralize` to prefix a word with "processes".ToQuantity(1) => "1 process" ``` +You can also pass a second argument, `ShowQuantityAs`, to `ToQuantity` to specify how you want the provided quantity to be outputted. The default value is `ShowQuantityAs.Numeric` which is what we saw above. The other two values are `ShowQuantityAs.Words` and `ShowQuantityAs.None`. + +```C# +"case".ToQuantity(5, ShowQuantityAs.Words) => "five cases" +"case".ToQuantity(5, ShowQuantityAs.None) => "cases" +``` + ####Ordinalize numbers & strings `Ordinalize` turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th: diff --git a/src/Humanizer.Tests/ToQuantityTests.cs b/src/Humanizer.Tests/ToQuantityTests.cs index 1c43602b2..1f1ac3085 100644 --- a/src/Humanizer.Tests/ToQuantityTests.cs +++ b/src/Humanizer.Tests/ToQuantityTests.cs @@ -21,5 +21,58 @@ public void ToQuantity(string word, int quatity, string expected) { Assert.Equal(expected, word.ToQuantity(quatity)); } + + [Theory] + [InlineData("case", 0, "cases")] + [InlineData("case", 1, "case")] + [InlineData("case", 5, "cases")] + [InlineData("man", 0, "men")] + [InlineData("man", 1, "man")] + [InlineData("man", 2, "men")] + [InlineData("men", 2, "men")] + [InlineData("process", 2, "processes")] + [InlineData("process", 1, "process")] + [InlineData("processes", 2, "processes")] + [InlineData("processes", 1, "process")] + public void ToQuantityWithNoQuantity(string word, int quatity, string expected) + { + Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.None)); + } + + [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")] + [InlineData("men", 2, "2 men")] + [InlineData("process", 2, "2 processes")] + [InlineData("process", 1, "1 process")] + [InlineData("processes", 2, "2 processes")] + [InlineData("processes", 1, "1 process")] + public void ToQuantityNumeric(string word, int quatity, string expected) + { +// ReSharper disable once RedundantArgumentDefaultValue + Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.Numeric)); + } + + [Theory] + [InlineData("case", 0, "zero cases")] + [InlineData("case", 1, "one case")] + [InlineData("case", 5, "five cases")] + [InlineData("man", 0, "zero men")] + [InlineData("man", 1, "one man")] + [InlineData("man", 2, "two men")] + [InlineData("men", 2, "two men")] + [InlineData("process", 2, "two processes")] + [InlineData("process", 1, "one process")] + [InlineData("processes", 2, "two processes")] + [InlineData("processes", 1200, "one thousand two hundred processes")] + [InlineData("processes", 1, "one process")] + public void ToQuantityWords(string word, int quatity, string expected) + { + Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.Words)); + } } } diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs index 32d31c5d3..02ea1b252 100644 --- a/src/Humanizer/ToQuantityExtensions.cs +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -1,5 +1,12 @@ namespace Humanizer { + public enum ShowQuantityAs + { + None = 0, + Numeric, + Words + } + public static class ToQuantityExtensions { /// @@ -7,6 +14,7 @@ public static class ToQuantityExtensions /// /// The word to be prefixes /// The quantity of the word + /// How to show the quantity. Numeric by default /// /// "request".ToQuantity(0) => "0 requests" /// "request".ToQuantity(1) => "1 request" @@ -15,9 +23,19 @@ public static class ToQuantityExtensions /// "men".ToQuantity(1) => "1 man" /// /// - public static string ToQuantity(this string input, int quantity) + public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric) { - return string.Format("{0} {1}", quantity, quantity == 1 ? input.Singularize(Plurality.CouldBeEither) : input.Pluralize(Plurality.CouldBeEither)); + var transformedInput = quantity == 1 + ? input.Singularize(Plurality.CouldBeEither) + : input.Pluralize(Plurality.CouldBeEither); + + if (showQuantityAs == ShowQuantityAs.None) + return transformedInput; + + if (showQuantityAs == ShowQuantityAs.Numeric) + return string.Format("{0} {1}", quantity, transformedInput); + + return string.Format("{0} {1}", quantity.ToWords(), transformedInput); } } }