Skip to content

Commit 4b0af89

Browse files
FrederikLynggaardyavuztor
authored andcommitted
Criteria check logic extracted and method simplified
1 parent c5c4e18 commit 4b0af89

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

JsonLogic.Net/EvaluateOperators.cs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -319,41 +319,27 @@ private Func<IProcessJsonLogic, JToken[], object, object> GenericArgsSatisfy(Fun
319319
return (p, args, data) =>
320320
{
321321
var values = args
322-
.Select(a => p.Apply(a, data))
322+
.Select(a => p.Apply(a, data));
323+
324+
var isAllText = values
323325
.Where(a => a != null)
324326
.Select(a => JToken.FromObject(a))
325-
.ToArray();
327+
.All(a => a.Type == JTokenType.String);
326328

327-
328-
var isAllText = values.All(a => a.Type == JTokenType.String);
329-
if (isAllText)
330-
{
331-
var valuesText = args
332-
.Select(a => p.Apply(a, data))
333-
.Select(a => a == null ? "" : a.ToString())
334-
.ToArray();
335-
for (int i = 1; i < valuesText.Length; i++)
336-
{
337-
338-
if (!criteriaText(valuesText[i - 1], valuesText[i])) return false;
339-
}
340-
341-
return true;
342-
}
329+
return isAllText ? CheckCriteria(values.Cast<string>().ToArray(), criteriaText)
330+
: CheckCriteria(values.Select(a => a == null ? 0d : Double.Parse(a.ToString())).ToArray(), criteriaDouble);
331+
};
332+
}
343333

344-
// not all values are of type text, assume these are Doubles or any other type and therefore handle this as before
345-
var valuesDouble = args
346-
.Select(a => p.Apply(a, data))
347-
.Select(a => a == null ? 0d : Double.Parse(a.ToString()))
348-
.ToArray();
349-
for (int i = 1; i < valuesDouble.Length; i++)
350-
{
334+
private bool CheckCriteria<T> (T [] values, Func<T, T, bool> criteria)
335+
{
336+
for (int i = 1; i < values.Length; i++)
337+
{
351338

352-
if (!criteriaDouble(valuesDouble[i - 1], valuesDouble[i])) return false;
353-
}
339+
if (!criteria(values[i - 1], values[i])) return false;
340+
}
354341

355-
return true;
356-
};
342+
return true;
357343
}
358344

359345
private static Func<IProcessJsonLogic, JToken[], object, object> ReduceDoubleArgs(double defaultValue, Func<double, double, double> reducer)

0 commit comments

Comments
 (0)