mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[PTRun][Unit]Support for plural units and improve alternative spellings (#19961)
* Upgrade UnitsNet package to a version that supports plural * Add support for plurals Fix metre conversion Add and update unit tests
This commit is contained in:
@@ -14,21 +14,21 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
private static readonly int _roundingFractionalDigits = 4;
|
||||
|
||||
private static readonly QuantityType[] _included = new QuantityType[]
|
||||
private static readonly QuantityInfo[] _included = new QuantityInfo[]
|
||||
{
|
||||
QuantityType.Acceleration,
|
||||
QuantityType.Angle,
|
||||
QuantityType.Area,
|
||||
QuantityType.Duration,
|
||||
QuantityType.Energy,
|
||||
QuantityType.Information,
|
||||
QuantityType.Length,
|
||||
QuantityType.Mass,
|
||||
QuantityType.Power,
|
||||
QuantityType.Pressure,
|
||||
QuantityType.Speed,
|
||||
QuantityType.Temperature,
|
||||
QuantityType.Volume,
|
||||
Acceleration.Info,
|
||||
Angle.Info,
|
||||
Area.Info,
|
||||
Duration.Info,
|
||||
Energy.Info,
|
||||
Information.Info,
|
||||
Length.Info,
|
||||
Mass.Info,
|
||||
Power.Info,
|
||||
Pressure.Info,
|
||||
Speed.Info,
|
||||
Temperature.Info,
|
||||
Volume.Info,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -37,7 +37,9 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
/// <returns>Corresponding enum or null.</returns>
|
||||
private static Enum GetUnitEnum(string unit, QuantityInfo unitInfo)
|
||||
{
|
||||
UnitInfo first = Array.Find(unitInfo.UnitInfos, info => info.Name.ToLowerInvariant() == unit.ToLowerInvariant());
|
||||
UnitInfo first = Array.Find(unitInfo.UnitInfos, info =>
|
||||
unit.ToLowerInvariant() == info.Name.ToLowerInvariant() || unit.ToLowerInvariant() == info.PluralName.ToLowerInvariant());
|
||||
|
||||
if (first != null)
|
||||
{
|
||||
return first.Value;
|
||||
@@ -72,12 +74,10 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
|
||||
/// </summary>
|
||||
/// <returns>The converted value as a double.</returns>
|
||||
public static double ConvertInput(ConvertModel convertModel, QuantityType quantityType)
|
||||
public static double ConvertInput(ConvertModel convertModel, QuantityInfo quantityInfo)
|
||||
{
|
||||
QuantityInfo unitInfo = Quantity.GetInfo(quantityType);
|
||||
|
||||
var fromUnit = GetUnitEnum(convertModel.FromUnit, unitInfo);
|
||||
var toUnit = GetUnitEnum(convertModel.ToUnit, unitInfo);
|
||||
var fromUnit = GetUnitEnum(convertModel.FromUnit, quantityInfo);
|
||||
var toUnit = GetUnitEnum(convertModel.ToUnit, quantityInfo);
|
||||
|
||||
if (fromUnit != null && toUnit != null)
|
||||
{
|
||||
@@ -94,13 +94,13 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
public static IEnumerable<UnitConversionResult> Convert(ConvertModel convertModel)
|
||||
{
|
||||
var results = new List<UnitConversionResult>();
|
||||
foreach (QuantityType quantityType in _included)
|
||||
foreach (var quantityInfo in _included)
|
||||
{
|
||||
double convertedValue = UnitHandler.ConvertInput(convertModel, quantityType);
|
||||
double convertedValue = UnitHandler.ConvertInput(convertModel, quantityInfo);
|
||||
|
||||
if (!double.IsNaN(convertedValue))
|
||||
{
|
||||
UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityType);
|
||||
UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityInfo);
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user