Skip to content

Commit

Permalink
Merge pull request #62 from MehdiK/ToQuantity
Browse files Browse the repository at this point in the history
Added ShowQuantityAs parameter to ToQuantity - #61
  • Loading branch information
MehdiK committed Jan 20, 2014
2 parents 4fd93e5 + 5ad2074 commit 3f0a134
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
53 changes: 53 additions & 0 deletions src/Humanizer.Tests/ToQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
22 changes: 20 additions & 2 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
namespace Humanizer
{
public enum ShowQuantityAs
{
None = 0,
Numeric,
Words
}

public static class ToQuantityExtensions
{
/// <summary>
/// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
/// </summary>
/// <param name="input">The word to be prefixes</param>
/// <param name="quantity">The quantity of the word</param>
/// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
/// <example>
/// "request".ToQuantity(0) => "0 requests"
/// "request".ToQuantity(1) => "1 request"
Expand All @@ -15,9 +23,19 @@ public static class ToQuantityExtensions
/// "men".ToQuantity(1) => "1 man"
/// </example>
/// <returns></returns>
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);
}
}
}

0 comments on commit 3f0a134

Please sign in to comment.