Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
StringFilter is no longer case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic committed Oct 26, 2019
1 parent 81e8919 commit 26f97b3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 52 deletions.
114 changes: 91 additions & 23 deletions src/BlazorTable/Filters/StringFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,45 @@ protected override void OnInitialized()
{
bool NotCondition = false;

Expression method;
Expression method = Column.Filter.Body;

if (Column.Filter.Body is UnaryExpression unary)
if (method is BinaryExpression binary)
{
NotCondition = unary.NodeType == ExpressionType.Not;
method = binary.Right;
}

method = unary.Operand;
if (method is BinaryExpression binary2)
{
NotCondition = binary2.NodeType == ExpressionType.LessThanOrEqual;
method = binary2.Left;
}
else

if (method is UnaryExpression unary1)
{
method = Column.Filter.Body;
NotCondition = unary1.NodeType == ExpressionType.Not;
method = unary1.Operand;
}

if (method is MethodCallExpression methodCall)
{
var MethodName = methodCall.Method.Name;

if (methodCall.Arguments[0] != null && methodCall.Arguments[0] is ConstantExpression constantExpression)
if (methodCall.Arguments[0] is ConstantExpression constantExpression)
{
FilterText = constantExpression.Value.ToString();
FilterText = constantExpression.Value?.ToString();
}

Condition = GetConditionFromMethod(MethodName, NotCondition);
Condition = GetConditionFromMethod(methodCall.Method.Name, NotCondition);
}
}
}
}

private StringCondition GetConditionFromMethod(string method, bool not)
{
if (method == nameof(string.Contains) && !not)
if (method == nameof(string.IndexOf) && !not)
{
return StringCondition.Contains;
}
else if (method == nameof(string.Contains) && not)
else if (method == nameof(string.IndexOf) && not)
{
return StringCondition.DoesNotContain;
}
Expand All @@ -88,7 +92,7 @@ private StringCondition GetConditionFromMethod(string method, bool not)
}
else if (method == nameof(string.IsNullOrEmpty) && not)
{
return StringCondition.IsNullOrEmpty;
return StringCondition.IsNotNulOrEmpty;
}

throw new InvalidOperationException("Shouldn't be here");
Expand All @@ -97,25 +101,89 @@ private StringCondition GetConditionFromMethod(string method, bool not)
public Expression<Func<TableItem, bool>> GetFilter()
{
FilterText = FilterText?.Trim();

switch (Condition)
{
case StringCondition.Contains:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.Contains), typeof(string), FilterText);
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.GreaterThanOrEqual(
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.IndexOf), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }),
Expression.Constant(0))),
Column.Field.Parameters);

case StringCondition.DoesNotContain:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.Contains), typeof(string), FilterText).Not();
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.LessThanOrEqual(
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.IndexOf), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }),
Expression.Constant(-1))),
Column.Field.Parameters);

case StringCondition.StartsWith:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.StartsWith), typeof(string), FilterText);
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.StartsWith), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters);

case StringCondition.EndsWith:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.EndsWith), typeof(string), FilterText);
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.EndsWith), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters);

case StringCondition.IsEqualTo:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.Equals), typeof(string), FilterText);
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.Equals), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters);

case StringCondition.IsNotEqualTo:
return Utillities.CallMethodType(Column.Field, typeof(string), nameof(string.Equals), typeof(string), FilterText).Not();
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Field.Body, Expression.Constant(null)),
Expression.Not(
Expression.Call(
Column.Field.Body,
typeof(string).GetMethod(nameof(string.Equals), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }))),
Column.Field.Parameters);

case StringCondition.IsNullOrEmpty:
return Utillities.CallMethodTypeStaticSelf(Column.Field, typeof(string), nameof(string.IsNullOrEmpty), typeof(string));
return Expression.Lambda<Func<TableItem, bool>>(
Expression.Call(
typeof(string).GetMethod(nameof(string.IsNullOrEmpty), new[] { typeof(string)}),
Column.Field.Body),
Column.Field.Parameters);

case StringCondition.IsNotNulOrEmpty:
return Utillities.CallMethodTypeStaticSelf(Column.Field, typeof(string), nameof(string.IsNullOrEmpty), typeof(string)).Not();
return Expression.Lambda<Func<TableItem, bool>>(
Expression.Not(
Expression.Call(
typeof(string).GetMethod(nameof(string.IsNullOrEmpty), new[] { typeof(string)}),
Column.Field.Body)),
Column.Field.Parameters);

default:
throw new ArgumentException(Condition + " is not defined!");
}
Expand Down
29 changes: 0 additions & 29 deletions src/BlazorTable/Utillities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,6 @@ public static string GetDescription<T>(this T e) where T : IConvertible
return null; // could also return string.Empty
}

public static Expression<Func<T, bool>> CallMethodType<T>(Expression<Func<T, object>> expression, Type type, string method, Type parameter, object value)
{
return CallMethodType(expression, type, method, new[] { parameter }, new[] { value });
}

public static Expression<Func<T, bool>> CallMethodType<T>(Expression<Func<T, object>> expression, Type type, string method, Type[] parameters, object[] values)
{
return Expression.Lambda<Func<T, bool>>(
Expression.Call(
expression.Body,
type.GetMethod(method, parameters),
values.OrEmptyIfNull().Select(Expression.Constant)),
expression.Parameters);
}

public static Expression<Func<T, bool>> CallMethodTypeStaticSelf<T>(Expression<Func<T, object>> expression, Type type, string method, Type parameter)
{
return Expression.Lambda<Func<T, bool>>(
Expression.Call(
type.GetMethod(method, new[] { parameter }),
expression.Body),
expression.Parameters);
}

public static Type GetMemberUnderlyingType(this MemberInfo member)
{
switch (member.MemberType)
Expand Down Expand Up @@ -132,10 +108,5 @@ public static MemberInfo GetPropertyMemberInfo<T>(this Expression<Func<T, object

return body?.Member;
}

public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
{
return Expression.Lambda<Func<T, bool>>(Expression.Not(expression.Body), expression.Parameters[0]);
}
}
}

0 comments on commit 26f97b3

Please sign in to comment.