Skip to content

Commit

Permalink
Add String.Equals with StringComparison (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored May 11, 2024
1 parent 63ec144 commit 588ef30
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 446 deletions.
4 changes: 2 additions & 2 deletions src/Handlebars.Net.Helpers.Core/Parsers/ArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public static class ArgumentsParser
return argument;
}

public static object ParseAsIntLongOrDouble(IHandlebars context, object value)
public static object ParseAsIntLongOrDouble(IHandlebars context, object? value)
{
var parsedValue = StringValueParser.Parse(context, value as string ?? value.ToString() ?? string.Empty);
var parsedValue = StringValueParser.Parse(context, value as string ?? value?.ToString() ?? string.Empty);

if (SupportedTypes.Contains(parsedValue.GetType()))
{
Expand Down
35 changes: 35 additions & 0 deletions src/Handlebars.Net.Helpers.Core/Utils/EnumUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics.CodeAnalysis;

namespace HandlebarsDotNet.Helpers.Utils;

public static class EnumUtils
{
public static bool TryParse<TEnum>(object? value, [NotNullWhen(true)] out TEnum? comparisonType) where TEnum : struct, Enum
{
switch (value)
{
case TEnum valueAsEnum:
comparisonType = valueAsEnum;
return true;

case int valueAsInt when Enum.IsDefined(typeof(TEnum), valueAsInt):
comparisonType = IntToEnum<TEnum>(valueAsInt);
return true;

case string stringValue when Enum.TryParse<TEnum>(stringValue, out var parsedAsEnum):
comparisonType = parsedAsEnum;
return true;

case string stringValue when int.TryParse(stringValue, out var parsedAsInt) && Enum.IsDefined(typeof(TEnum), parsedAsInt):
comparisonType = IntToEnum<TEnum>(parsedAsInt);
return true;

default:
comparisonType = default;
return false;
}
}

public static TEnum IntToEnum<TEnum>(int value) where TEnum : struct, Enum
=> (TEnum)Enum.ToObject(typeof(TEnum), value);
}
6 changes: 4 additions & 2 deletions src/Handlebars.Net.Helpers/Helpers/MathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace HandlebarsDotNet.Helpers.Helpers;

internal class MathHelpers : BaseHelpers, IHelpers
{
private const double Tolerance = 1e-20; // 20 Digits of precision

[HandlebarsWriter(WriterType.Value)]
public object Add(object value1, object value2)
{
Expand Down Expand Up @@ -40,7 +42,7 @@ public double Divide(object value1, object value2)
[HandlebarsWriter(WriterType.Value)]
public bool Equal(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 == x2, (x1, x2) => x1 == x2, (x1, x2) => x1 == x2);
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 == x2, (x1, x2) => x1 == x2, (x1, x2) => Math.Abs(x1 - x2) < Tolerance);
}

[HandlebarsWriter(WriterType.Value)]
Expand Down Expand Up @@ -106,7 +108,7 @@ public object Multiply(object value1, object value2)
[HandlebarsWriter(WriterType.Value)]
public bool NotEqual(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 != x2, (x1, x2) => x1 != x2, (x1, x2) => x1 != x2);
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 != x2, (x1, x2) => x1 != x2, (x1, x2) => Math.Abs(x1 - x2) > Tolerance);
}

[HandlebarsWriter(WriterType.Value)]
Expand Down
Loading

0 comments on commit 588ef30

Please sign in to comment.