-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntExtensions.cs
117 lines (113 loc) · 4.56 KB
/
IntExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// *****************************************************
// EXTENSIONS
// by Shane Whitehead
// *****************************************************
// The software is released under the GNU GPL:
// http://www.gnu.org/licenses/gpl.txt
//
// Feel free to use, modify and distribute this software
// I only ask you to keep this comment intact.
// Please contact me with bugs, ideas, modification etc.
// *****************************************************
namespace BWakaBats.Extensions
{
public static class IntExtensions
{
/// <summary>
/// Get a string in the form: "no [type]s" for value 0, "[an/a] [type]" for value 1, or "[n] [type]s" for more than 1 .
/// </summary>
/// <remarks>
/// The parameter "userAn" is used to determine, for a single item, the type will be prefixed with "an"
/// (see the example "an hat")
/// </remarks>
/// <seealso cref="ToPluralString(int,string,string)"/>
/// <example>
/// 0 leaf,false = no leafs
/// 1,leaf,false = a leaf
/// 10,leaf,false = 10 leafs
/// 0,leaf,false,leaves = no leaves
/// 1,leaf,false,leaves = a leaf
/// 10,leaf,false,leaves = 10 leaves
/// 1,hotel,true = an hotel
/// 1,apple,true = an apple
/// 1,hat,false = a hat
/// </example>
/// <param name="value">The numeric value</param>
/// <param name="type">The type of thing you are counting</param>
/// <param name="forceAn">Force the logic to use "an"</param>
/// <param name="plural">The plural version of the thing you are counting</param>
/// <returns>[no/an/a/N] [types](s)</returns>
public static string ToPluralString(this int value, string type, bool forceAn, string plural = null)
{
switch (value)
{
case 0:
return "no " + (plural ?? type + "s");
case 1:
return (forceAn ? "an " : "a ") + type;
}
return value + " " + (plural ?? type + "s");
}
/// <summary>
/// Get a string in the form: "no [type]s" for value 0, "[an/a] [type]" for value 1, or "[n] [type]s" for more than 1
/// </summary>
/// <remarks>
/// The logic assumes that, for a single item, any type name that starting with any one of AEIOUHaeiouh will be prefixed with "an"
/// (see the example "an hat")
/// </remarks>
/// <seealso cref="ToPluralString(int,string,bool,string)"/>
/// <example>
/// 0 leaf = no leafs
/// 1,leaf = a leaf
/// 10,leaf = 10 leafs
/// 0,leaf,leaves = no leaves
/// 1,leaf,leaves = a leaf
/// 10,leaf,leaves = 10 leaves
/// 1,hotel = an hotel
/// 1,apple = an apple
/// 1,hat = an hat
/// </example>
/// <param name="value">The numeric value</param>
/// <param name="type">The type of thing you are counting</param>
/// <param name="plural">The plural version of the thing you are counting</param>
/// <param name="useOne">Use 1 instead of a or an</param>
/// <returns>[no/an/a/N] [types](s)</returns>
public static string ToPluralString(this int value, string type, string plural = null, bool useOne = false)
{
switch (value)
{
case 0:
return "no " + (plural ?? type + "s");
case 1:
if (useOne)
return "1 " + type;
return ("AEIOUHaeiouh".IndexOf(type[0]) > -1 ? "an " : "a ") + type;
}
return value + " " + (plural ?? type + "s");
}
/// <summary>
/// Get the English ordinal for a number
/// </summary>
/// <param name="index">The value to get the ordinal for</param>
/// <returns>The English ordinal (including the original number)</returns>
public static string ToOrdinalString(this int index)
{
if (index <= 3 || index >= 21)
{
if (index == 0)
return "Last";
switch (index % 10)
{
case 1:
return index + "st";
case 2:
return index + "nd";
case 3:
return index + "rd";
}
}
return index + "th";
}
}
}