Skip to content

Commit

Permalink
correct ToHeading doc (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilboyd authored Feb 12, 2024
1 parent 27594ab commit 534b65f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,18 +1114,18 @@ Humanizer includes methods to change a numeric heading to words. The heading can

```C#
360.ToHeading();
// north
// N
720.ToHeading();
// north
// N
```

In order to retrieve a short version of the heading you can use the following call:
In order to retrieve a full version of the heading, use the following call:

```C#
180.ToHeading(true);
// S
360.ToHeading(true);
// N
180.ToHeading(HeadingStyle.Full);
// south
360.ToHeading(HeadingStyle.Full);
// north
```

Please note that a textual representation has a maximum deviation of 11.25°.
Expand Down
4 changes: 4 additions & 0 deletions src/Humanizer.Tests.Shared/HeadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class HeadingTests
[InlineData(337.5, "NNW")]
[InlineData(348.7, "NNW")]
[InlineData(348.8, "N")]
[InlineData(360, "N")]
[InlineData(720, "N")]
[Theory]
public void ToHeadingAbbreviated(double heading, string expected)
Expand All @@ -76,6 +77,7 @@ public void ToHeadingAbbreviated(double heading, string expected)
[InlineData(292.5, "west-northwest")]
[InlineData(315, "northwest")]
[InlineData(337.5, "north-northwest")]
[InlineData(360, "north")]
[InlineData(720, "north")]
[Theory]
public void ToHeading(double heading, string expected)
Expand Down Expand Up @@ -153,6 +155,8 @@ public void FromShortHeading(string heading, double expected)
[InlineData(337.5, '↑')]
[InlineData(348.7, '↑')]
[InlineData(348.8, '↑')]
[InlineData(360, '↑')]
[InlineData(720, '↑')]
[Theory]
public void ToHeadingArrow(double heading, char expected)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Humanizer/HeadingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public enum HeadingStyle
/// </summary>
public static class HeadingExtensions
{
internal static readonly string[] headings = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" };
internal static readonly char[] headingArrows = { '↑', '↗', '→', '↘', '↓', '↙', '←', '↖' };
internal static readonly string[] Headings = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW" };
internal static readonly char[] HeadingArrows = { '↑', '↗', '→', '↘', '↓', '↙', '←', '↖' };

// https://stackoverflow.com/a/7490772/1720761
/// <summary>
Expand All @@ -37,13 +37,13 @@ public static class HeadingExtensions
/// </summary>
/// <returns>A textual representation of the heading</returns>
/// <param name="heading">The heading value</param>
/// <param name="culture">The culture to return the textual representation in</param>
/// <param name="style">Whether to return a short result or not. <see cref="HeadingStyle"/></param>
/// <param name="culture">The culture to return the textual representation in</param>
public static string ToHeading(this double heading, HeadingStyle style = HeadingStyle.Abbreviated, CultureInfo culture = null)
{
var val = (int)((heading / 22.5) + .5);

var result = headings[val % 16];
var result = Headings[val % 16];

if (style == HeadingStyle.Abbreviated)
{
Expand All @@ -64,7 +64,7 @@ public static char ToHeadingArrow(this double heading)
{
var val = (int)((heading / 45) + .5);

return headingArrows[val % 8];
return HeadingArrows[val % 8];
}

/// <summary>
Expand Down Expand Up @@ -93,12 +93,12 @@ public static double FromAbbreviatedHeading(this string heading, CultureInfo cul
culture ??= CultureInfo.CurrentCulture;

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

Expand All @@ -110,8 +110,8 @@ public static double FromAbbreviatedHeading(this string heading, CultureInfo cul
/// </summary>
public static double FromHeadingArrow(this char heading)
{
var index = Array.IndexOf(headingArrows, heading);
var index = Array.IndexOf(HeadingArrows, heading);

if (index == -1)
{
return -1;
Expand Down

0 comments on commit 534b65f

Please sign in to comment.