Skip to content

Commit

Permalink
Merge pull request #727 from Humanizr/heading-case
Browse files Browse the repository at this point in the history
Upper-case headings. Change boolean to enum. Short to Abbreviation
  • Loading branch information
Oren Novotny authored Jun 30, 2018
2 parents 99902de + 0f6d79c commit 9d7291c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public class HeadingTests
[InlineData(348.7, "NNW")]
[InlineData(348.8, "N")]
[InlineData(720, "N")]
public void ToHeadingShort(double heading, string expected) {
Assert.Equal(heading.ToHeading(true), expected);
[Theory]
public void ToHeadingAbbreviated(double heading, string expected)
{
Assert.Equal(expected, heading.ToHeading());
}

[InlineData(0, "north")]
Expand All @@ -75,8 +77,10 @@ public void ToHeadingShort(double heading, string expected) {
[InlineData(315, "northwest")]
[InlineData(337.5, "north-northwest")]
[InlineData(720, "north")]
public void ToHeading(double heading, string expected) {
Assert.Equal(heading.ToHeading(), expected);
[Theory]
public void ToHeading(double heading, string expected)
{
Assert.Equal(expected, heading.ToHeading(HeadingStyle.Full));
}

[InlineData("N" , 0)]
Expand All @@ -95,8 +99,10 @@ public void ToHeading(double heading, string expected) {
[InlineData("WNW", 292.5)]
[InlineData("NW" , 315)]
[InlineData("NNW", 337.5)]
public void FromShortHeading(string heading, double expected) {
Assert.Equal(heading.FromShortHeading(), expected);
[Theory]
public void FromShortHeading(string heading, double expected)
{
Assert.Equal(expected, heading.FromAbbreviatedHeading());
}

[InlineData(0, '↑')]
Expand Down Expand Up @@ -147,8 +153,10 @@ public void FromShortHeading(string heading, double expected) {
[InlineData(337.5, '↑')]
[InlineData(348.7, '↑')]
[InlineData(348.8, '↑')]
public void ToHeadignArrow(double heading, char expected) {
Assert.Equal(heading.ToHeadingArrow(), expected);
[Theory]
public void ToHeadignArrow(double heading, char expected)
{
Assert.Equal(expected, heading.ToHeadingArrow());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FluentDate\InTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FluentDate\OnTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FluentDate\PrepositionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HeadingTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)InflectorTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\af\DateHumanizeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\af\NumberToWordsTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,15 @@ namespace Humanizer
}
public class static HeadingExtensions
{
public static double FromShortHeading(this string heading) { }
public static string ToHeading(this double heading, bool shortResult = False, System.Globalization.CultureInfo culture = null) { }
public static double FromAbbreviatedHeading(this string heading) { }
public static string ToHeading(this double heading, Humanizer.HeadingStyle style = 0, System.Globalization.CultureInfo culture = null) { }
public static char ToHeadingArrow(this double heading) { }
}
public enum HeadingStyle
{
Abbreviated = 0,
Full = 1,
}
public class In
{
public In() { }
Expand Down
25 changes: 21 additions & 4 deletions src/Humanizer/HeadingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

namespace Humanizer
{
/// <summary>
/// Style for the cardinal direction humanization
/// </summary>
public enum HeadingStyle
{
/// <summary>
/// Returns an abbreviated format
/// </summary>
Abbreviated,

/// <summary>
/// Returns the full format
/// </summary>
Full
}

/// <summary>
/// Contains extensions to transform a number indicating a heading into the
/// textual representation of the heading.
Expand All @@ -21,13 +37,14 @@ public static class HeadingExtensions
/// </summary>
/// <returns>A textual representation of the heading</returns>
/// <param name="culture">The culture to return the textual representation in</param>
/// <param name="shortResult">Whether to return a short result or not</param>
public static string ToHeading(this double heading, bool shortResult = false, CultureInfo culture = null) {
/// <param name="style">Whether to return a short result or not. <see cref="HeadingStyle"/></param>
public static string ToHeading(this double heading, HeadingStyle style = HeadingStyle.Abbreviated, CultureInfo culture = null) {
int val = (int)((heading / 22.5) + .5);

string result = headings[val % 16];

if (shortResult) return Resources.GetResource($"{result}_Short", culture);
if (style == HeadingStyle.Abbreviated)
return Resources.GetResource($"{result}_Short", culture);

result = Resources.GetResource(result, culture);
return result;
Expand All @@ -49,7 +66,7 @@ public static char ToHeadingArrow(this double heading) {
/// Returns a heading based on the short textual representation of the heading.
/// </summary>
/// <returns>The heading. -1 if the heading could not be parsed.</returns>
public static double FromShortHeading(this string heading) {
public static double FromAbbreviatedHeading(this string heading) {
var index = Array.IndexOf(headings, heading.ToUpperInvariant());

if (index == -1) return -1;
Expand Down
34 changes: 17 additions & 17 deletions src/Humanizer/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -628,51 +628,51 @@
<value>north-northwest</value>
</data>
<data name="N_Short" xml:space="preserve">
<value>n</value>
<value>N</value>
</data>
<data name="NNE_Short" xml:space="preserve">
<value>nne</value>
<value>NNE</value>
</data>
<data name="NE_Short" xml:space="preserve">
<value>ne</value>
<value>NE</value>
</data>
<data name="ENE_Short" xml:space="preserve">
<value>ene</value>
<value>ENE</value>
</data>
<data name="E_Short" xml:space="preserve">
<value>e</value>
<value>E</value>
</data>
<data name="ESE_Short" xml:space="preserve">
<value>ese</value>
<value>ESE</value>
</data>
<data name="SE_Short" xml:space="preserve">
<value>se</value>
<value>SE</value>
</data>
<data name="SSE_Short" xml:space="preserve">
<value>sse</value>
<value>SSE</value>
</data>
<data name="S_Short" xml:space="preserve">
<value>s</value>
<value>S</value>
</data>
<data name="SSW_Short" xml:space="preserve">
<value>ssw</value>
<value>SSW</value>
</data>
<data name="SW_Short" xml:space="preserve">
<value>sw</value>
<value>SW</value>
</data>
<data name="WSW_Short" xml:space="preserve">
<value>wsw</value>
<value>WSW</value>
</data>
<data name="W_Short" xml:space="preserve">
<value>w</value>
<value>W</value>
</data>
<data name="WNW_Short" xml:space="preserve">
<value>wnw</value>
<value>WNW</value>
</data>
<data name="NW_Short" xml:space="preserve">
<value>nw</value>
<value>NW</value>
</data>
<data name="NNW_Short" xml:space="preserve">
<value>nnw</value>
<value>NNW</value>
</data>
</root>
</root>

0 comments on commit 9d7291c

Please sign in to comment.