Skip to content

Commit

Permalink
Merge pull request #1117 from hangy/issue1103
Browse files Browse the repository at this point in the history
  • Loading branch information
clairernovotny authored Aug 23, 2021
2 parents aa107c6 + 9d48857 commit edc57f7
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
46 changes: 44 additions & 2 deletions src/Humanizer.Tests.Shared/HeadingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xunit;
using System.Globalization;

using Xunit;

namespace Humanizer.Tests
{
Expand Down Expand Up @@ -154,9 +156,49 @@ public void FromShortHeading(string heading, double expected)
[InlineData(348.7, '↑')]
[InlineData(348.8, '↑')]
[Theory]
public void ToHeadignArrow(double heading, char expected)
public void ToHeadingArrow(double heading, char expected)
{
Assert.Equal(expected, heading.ToHeadingArrow());
}

[InlineData('↑', 0)]
[InlineData('↗', 45)]
[InlineData('→', 90)]
[InlineData('↘', 135)]
[InlineData('↓', 180)]
[InlineData('↙', 225)]
[InlineData('←', 270)]
[InlineData('↖', 315)]
[InlineData('\n', -1)]
[Theory]
public void FromHeadingArrow(char heading, double expected)
{
Assert.Equal(expected, heading.FromHeadingArrow());
}

[InlineData("↑", 0)]
[InlineData("↗", 45)]
[InlineData("→", 90)]
[InlineData("↘", 135)]
[InlineData("↓", 180)]
[InlineData("↙", 225)]
[InlineData("←", 270)]
[InlineData("↖", 315)]
[InlineData("", -1)]
[InlineData("xyz", -1)]
[Theory]
public void FromHeadingArrow_Also_Works_With_Strings(string heading, double expected)
{
Assert.Equal(expected, heading.FromHeadingArrow());
}

[InlineData("NNW", "en-US", 337.5)]
[InlineData("ØNØ", "da", 67.5)]
[InlineData("O", "de-DE", 90.0)]
[Theory]
public void FromShortHeading_CanSpecifyCultureExplicitly(string heading, string culture, double expected)
{
Assert.Equal(expected, heading.FromAbbreviatedHeading(new CultureInfo(culture)));
}
}
}
22 changes: 22 additions & 0 deletions src/Humanizer.Tests.Shared/Localisation/de/HeadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,27 @@ public void ToHeading(double heading, string expected)
{
Assert.Equal(expected, heading.ToHeading(HeadingStyle.Full));
}

[InlineData("N", 0)]
[InlineData("NNO", 22.5)]
[InlineData("NO", 45)]
[InlineData("ONO", 67.5)]
[InlineData("O", 90)]
[InlineData("OSO", 112.5)]
[InlineData("SO", 135)]
[InlineData("SSO", 157.5)]
[InlineData("S", 180)]
[InlineData("SSW", 202.5)]
[InlineData("SW", 225)]
[InlineData("WSW", 247.5)]
[InlineData("W", 270)]
[InlineData("WNW", 292.5)]
[InlineData("NW", 315)]
[InlineData("NNW", 337.5)]
[Theory]
public void FromShortHeading(string heading, double expected)
{
Assert.Equal(expected, heading.FromAbbreviatedHeading());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ namespace Humanizer
public class static HeadingExtensions
{
public static double FromAbbreviatedHeading(this string heading) { }
public static double FromAbbreviatedHeading(this string heading, System.Globalization.CultureInfo culture = null) { }
public static double FromHeadingArrow(this char heading) { }
public static double FromHeadingArrow(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) { }
}
Expand Down
58 changes: 56 additions & 2 deletions src/Humanizer/HeadingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;

using Humanizer.Localisation;

namespace Humanizer
Expand Down Expand Up @@ -73,14 +74,67 @@ public static char ToHeadingArrow(this double heading)
/// <returns>The heading. -1 if the heading could not be parsed.</returns>
public static double FromAbbreviatedHeading(this string heading)
{
var index = Array.IndexOf(headings, heading.ToUpperInvariant());
return heading.FromAbbreviatedHeading(null);
}

/// <summary>
/// Returns a heading based on the short textual representation of the heading.
/// </summary>
/// <param name="culture">The culture of the heading</param>
/// <returns>The heading. -1 if the heading could not be parsed.</returns>
public static double FromAbbreviatedHeading(this string heading, CultureInfo culture = null)
{
if (heading == null)
{
throw new ArgumentNullException(nameof(heading));
}

culture ??= CultureInfo.CurrentCulture;

var upperCaseHeading = culture.TextInfo.ToUpper(heading);
for (var index = 0; index < headings.Length; ++index)
{
var localizedShortHeading = Resources.GetResource($"{headings[index]}_Short", culture);
if (culture.CompareInfo.Compare(upperCaseHeading, localizedShortHeading) == 0)
{
return (index * 22.5);
}
}

return -1;
}

/// <summary>
/// Returns a heading based on the heading arrow.
/// </summary>
public static double FromHeadingArrow(this char heading)
{
var index = Array.IndexOf(headingArrows, heading);

if (index == -1)
{
return -1;
}

return (index * 22.5);
return (index * 45.0);
}

/// <summary>
/// Returns a heading based on the heading arrow.
/// </summary>
public static double FromHeadingArrow(this string heading)
{
if (heading == null)
{
throw new ArgumentNullException(nameof(heading));
}

if (heading.Length != 1)
{
return -1;
}

return heading[0].FromHeadingArrow();
}
}
}

0 comments on commit edc57f7

Please sign in to comment.