Skip to content

Commit

Permalink
Implement FromHeadingArrow
Browse files Browse the repository at this point in the history
  • Loading branch information
hangy committed Aug 17, 2021
1 parent b13a0a4 commit 295b731
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Humanizer.Tests.Shared/HeadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,36 @@ 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ namespace Humanizer
public class static HeadingExtensions
{
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
34 changes: 34 additions & 0 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 @@ -93,5 +94,38 @@ public static double FromAbbreviatedHeading(this string heading, CultureInfo cul

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 * 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 295b731

Please sign in to comment.