Skip to content

Commit

Permalink
min max
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 10, 2024
1 parent cf34f09 commit 775788a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ public static JToken Last(this JArray source, LambdaExpression lambda)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
public static JToken Max(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
public static object Max(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJToken(queryable.Max(config, predicate, args))!;
return queryable.Max(config, predicate, args);
}

/// <summary>
Expand All @@ -490,7 +490,7 @@ public static JToken Max(this JArray source, NewtonsoftJsonParsingConfig config,
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
public static JToken Max(this JArray source, string predicate, params object?[] args)
public static object Max(this JArray source, string predicate, params object?[] args)
{
return Max(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
Expand All @@ -501,12 +501,12 @@ public static JToken Max(this JArray source, string predicate, params object?[]
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <param name="lambda">A Lambda Expression.</param>
/// <returns>The max element in the sequence.</returns>
public static JToken Max(this JArray source, LambdaExpression lambda)
public static object Max(this JArray source, LambdaExpression lambda)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJToken(queryable.Max(lambda))!;
return queryable.Max(lambda);
}
#endregion Max

Expand All @@ -519,13 +519,13 @@ public static JToken Max(this JArray source, LambdaExpression lambda)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
public static JToken Min(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
public static object Min(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJToken(queryable.Min(config, predicate, args))!;
return queryable.Min(config, predicate, args);
}

/// <summary>
Expand All @@ -535,7 +535,7 @@ public static JToken Min(this JArray source, NewtonsoftJsonParsingConfig config,
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
public static JToken Min(this JArray source, string predicate, params object?[] args)
public static object Min(this JArray source, string predicate, params object?[] args)
{
return Min(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
Expand All @@ -546,12 +546,12 @@ public static JToken Min(this JArray source, string predicate, params object?[]
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <param name="lambda">A Lambda Expression.</param>
/// <returns>The min element in the sequence.</returns>
public static JToken Min(this JArray source, LambdaExpression lambda)
public static object Min(this JArray source, LambdaExpression lambda)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJToken(queryable.Min(lambda))!;
return queryable.Min(lambda);
}
#endregion Min

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,12 @@ public static JsonElement Last(this JsonDocument source, LambdaExpression lambda
/// </summary>
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <returns>The max element in the sequence.</returns>
public static JsonElement Max(this JsonDocument source)
public static object Max(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Max()) ?? default;
return queryable.Max();
}

/// <summary>
Expand All @@ -581,13 +581,13 @@ public static JsonElement Max(this JsonDocument source)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
public static JsonElement Max(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
public static object Max(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJsonElement(queryable.Max(config, predicate, args)) ?? default;
return queryable.Max(config, predicate, args);
}

/// <summary>
Expand All @@ -597,7 +597,7 @@ public static JsonElement Max(this JsonDocument source, SystemTextJsonParsingCon
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
public static JsonElement Max(this JsonDocument source, string predicate, params object?[] args)
public static object Max(this JsonDocument source, string predicate, params object?[] args)
{
return Max(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
Expand All @@ -608,12 +608,12 @@ public static JsonElement Max(this JsonDocument source, string predicate, params
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <param name="lambda">A Lambda Expression.</param>
/// <returns>The max element in the sequence.</returns>
public static JsonElement Max(this JsonDocument source, LambdaExpression lambda)
public static object Max(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Max(lambda)) ?? default;
return queryable.Max(lambda);
}
#endregion Max

Expand All @@ -623,12 +623,12 @@ public static JsonElement Max(this JsonDocument source, LambdaExpression lambda)
/// </summary>
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <returns>The min element in the sequence.</returns>
public static JsonElement Min(this JsonDocument source)
public static object Min(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Min()) ?? default;
return queryable.Min();
}

/// <summary>
Expand All @@ -639,13 +639,13 @@ public static JsonElement Min(this JsonDocument source)
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
public static JsonElement Min(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
public static object Min(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return ToJsonElement(queryable.Min(config, predicate, args)) ?? default;
return queryable.Min(config, predicate, args);
}

/// <summary>
Expand All @@ -655,7 +655,7 @@ public static JsonElement Min(this JsonDocument source, SystemTextJsonParsingCon
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
public static JsonElement Min(this JsonDocument source, string predicate, params object?[] args)
public static object Min(this JsonDocument source, string predicate, params object?[] args)
{
return Min(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
Expand All @@ -666,12 +666,12 @@ public static JsonElement Min(this JsonDocument source, string predicate, params
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <param name="lambda">A Lambda Expression.</param>
/// <returns>The min element in the sequence.</returns>
public static JsonElement Min(this JsonDocument source, LambdaExpression lambda)
public static object Min(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Min(lambda)) ?? default;
return queryable.Min(lambda);
}
#endregion Min

Expand Down Expand Up @@ -1018,6 +1018,65 @@ public static JsonDocument SkipWhile(this JsonDocument source, string predicate,
}
#endregion SkipWhile

#region Sum
/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return queryable.Sum();
}

/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source, config);
return queryable.Sum(predicate, args);
}

/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source, string predicate, params object?[] args)
{
return Sum(source, SystemTextJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <param name="lambda">A Lambda Expression.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);
Check.NotNull(lambda);

var queryable = ToQueryable(source);
return queryable.Sum(lambda);
}
#endregion Sum

#region Where
/// <summary>
/// Filters a sequence of values based on a predicate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ public void LastOrDefault()
public void Max()
{
// Act + Assert
((string?)_source.Max("Age")).Should().Be("40");
((int?)_source.Max("Age")).Should().Be(40);
}

[Fact]
public void Min()
{
// Act + Assert
((string?)_source.Min("Age")).Should().Be("30");
((int?)_source.Min("Age")).Should().Be(30);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ public void LastOrDefault()
public void Max()
{
// Act + Assert
_source.Max("Age").GetRawText().Should().BeEquivalentTo("40");
_source.Max("Age").Should().BeEquivalentTo(40);
}

[Fact]
public void Min()
{
// Act + Assert
_source.Min("Age").GetRawText().Should().BeEquivalentTo("30");
_source.Min("Age").Should().BeEquivalentTo(30);
}

[Fact]
Expand Down

0 comments on commit 775788a

Please sign in to comment.