Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Run-UnitConverter] Use capital letters in DegreePrefixer #34845

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ This plugin uses a package called [UnitsNet](https://github.com/angularsen/Units
- [Temperature](https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/GeneratedCode/Units/TemperatureUnit.g.cs)
- [Volume](https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/GeneratedCode/Units/VolumeUnit.g.cs)

These are the ones that are currently enabled (though UnitsNet supports many more). They are defined in [`Main.cs`](/src/modules/launcher/Plugins/Community.PowerToys.Run.UnitConverter/Main.cs).
These are the ones that are currently enabled (though UnitsNet supports many more). They are defined in [`UnitHandler.cs`](/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs).


### [`InputInterpreter`](/src/modules/launcher/Plugins/Community.PowerToys.Run.UnitConverter/InputInterpreter.cs)
### [`InputInterpreter`](/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/InputInterpreter.cs)
- Class which manipulates user input such that it may be interpreted correctly and thus converted.
- Uses a regex amongst other things to do this.

### [`UnitHandler`](/src/modules/launcher/Plugins/Community.PowerToys.Run.UnitConverter/UnitHandler.cs)
### [`UnitHandler`](/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs)
- Class that does the actual conversion.
- Supports abbreviations in user input (single, double, or none).
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public void HandlesMetreVsMeterNotation(string[] input, string[] expectedResult)

[DataTestMethod]
[DataRow(new string[] { "5", "CeLsIuS", "in", "faHrenheiT" }, new string[] { "5", "DegreeCelsius", "in", "DegreeFahrenheit" })]
[DataRow(new string[] { "5", "f", "in", "celsius" }, new string[] { "5", "°f", "in", "DegreeCelsius" })]
[DataRow(new string[] { "5", "c", "in", "f" }, new string[] { "5", "°c", "in", "°f" })]
[DataRow(new string[] { "5", "f", "in", "c" }, new string[] { "5", "°f", "in", "°c" })]
[DataRow(new string[] { "5", "f", "in", "celsius" }, new string[] { "5", "°F", "in", "DegreeCelsius" })]
[DataRow(new string[] { "5", "c", "in", "f" }, new string[] { "5", "°C", "in", "°F" })]
[DataRow(new string[] { "5", "f", "in", "c" }, new string[] { "5", "°F", "in", "°C" })]
#pragma warning restore CA1861 // Avoid constant arrays as arguments
public void PrefixesDegrees(string[] input, string[] expectedResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void ShorthandFeetInchHandler(ref string[] split, CultureInfo cult

if (!isFeet || !isInches)
{
// atleast one could not be parsed correctly
// at least one could not be parsed correctly
break;
}

Expand All @@ -114,7 +114,7 @@ public static void ShorthandFeetInchHandler(ref string[] split, CultureInfo cult
}

/// <summary>
/// Adds degree prefixes to degree units for shorthand notation. E.g. '10 c in fahrenheit' becomes '10 °c in DegreeFahrenheit'.
/// Adds degree prefixes to degree units for shorthand notation. E.g. '10 c in fahrenheit' becomes '10 °C in DegreeFahrenheit'.
/// </summary>
public static void DegreePrefixer(ref string[] split)
{
Expand All @@ -129,11 +129,11 @@ public static void DegreePrefixer(ref string[] split)
break;

case "c":
split[1] = "°c";
split[1] = "°C";
break;

case "f":
split[1] = "°f";
split[1] = "°F";
break;

default:
Expand All @@ -151,34 +151,18 @@ public static void DegreePrefixer(ref string[] split)
break;

case "c":
split[3] = "°c";
split[3] = "°C";
break;

case "f":
split[3] = "°f";
split[3] = "°F";
break;

default:
break;
}
}

/// <summary>
/// The plural form "feet" is not recognized by UniteNets. Replace it with "ft".
/// </summary>
public static void FeetToFt(ref string[] split)
{
if (string.Equals(split[1], "feet", StringComparison.OrdinalIgnoreCase))
{
split[1] = "ft";
}

if (string.Equals(split[3], "feet", StringComparison.OrdinalIgnoreCase))
{
split[3] = "ft";
}
}

/// <summary>
/// Converts spelling "kph" to "km/h"
/// </summary>
Expand Down Expand Up @@ -291,7 +275,6 @@ public static ConvertModel Parse(Query query)

InputInterpreter.DegreePrefixer(ref split);
InputInterpreter.MetreToMeter(ref split);
InputInterpreter.FeetToFt(ref split);
InputInterpreter.KPHHandler(ref split);
InputInterpreter.GallonHandler(ref split, CultureInfo.CurrentCulture);
InputInterpreter.OunceHandler(ref split, CultureInfo.CurrentCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
{
public static class UnitHandler
{
private static readonly int _roundingFractionalDigits = 4;
private static readonly int _roundingSignificantDigits = 4;

private static readonly QuantityInfo[] _included = new QuantityInfo[]
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public static double Round(double value)

var power = Math.Floor(Math.Log10(Math.Abs(value)));
var exponent = Math.Pow(10, power);
var rounded = Math.Round(value / exponent, _roundingFractionalDigits) * exponent;
var rounded = Math.Round(value / exponent, _roundingSignificantDigits) * exponent;
return rounded;
}

Expand Down
Loading