Skip to content

Commit

Permalink
Last
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 9, 2024
1 parent 1c25fe2 commit bf2e7fa
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,51 @@ public static JToken First(this JArray source, LambdaExpression lambda)
}
#endregion FirstOrDefault

#region Last
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return the last element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 first element in source that passes the test in predicate.</returns>
public static JToken Last(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source);
return ToJToken(queryable.Last(predicate, args));
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return the last element 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 first element in source that passes the test in predicate.</returns>
public static JToken Last(this JArray source, string predicate, params object?[] args)
{
return Last(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return the last element of.</param>
/// <param name="lambda">A cached Lambda Expression.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JToken Last(this JArray source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,64 @@ public static JsonElement First(this JsonDocument source, LambdaExpression lambd
}
#endregion FirstOrDefault

#region Last
/// <summary>
/// Returns the last element of a sequence.
/// </summary>
/// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
/// <returns>The last element in source.</returns>
public static JsonElement Last(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Last());
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element 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 first element in source that passes the test in predicate.</returns>
public static JsonElement Last(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source);
return ToJsonElement(queryable.Last(predicate, args));
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element 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 first element in source that passes the test in predicate.</returns>
public static JsonElement Last(this JsonDocument source, string predicate, params object?[] args)
{
return Last(source, SystemTextJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="lambda">A cached Lambda Expression.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement Last(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,27 @@ public void Distinct()
[Fact]
public void First()
{
// Act + Assert 1
_source.First().Should().NotBeNull();

// Act + Assert 2
// Act + Assert
((string?)_source.First("Age > 30")["Name"]).Should().Be("Doe");
}

[Fact]
public void FirstOrDefault()
{
// Act + Assert 1
_source.FirstOrDefault().Should().NotBeNull();

// Act + Assert 2
((string?)_source.FirstOrDefault("Age > 30")!["Name"]).Should().Be("Doe");

// Act + Assert 3
// Act + Assert 2
_source.FirstOrDefault("Age > 999").Should().BeNull();
}

[Fact]
public void Last()
{
// Act + Assert
((string?)_source.First("Age > 30")["Name"]).Should().Be("Doe");
}

[Fact]
public void Select()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ public void FirstOrDefault()
_source.FirstOrDefault("Age > 999").Should().BeNull();
}

[Fact]
public void Last()
{
// Act + Assert 1
_source.Last().GetRawText().Should().BeEquivalentTo(JsonDocument.Parse(@"{""Name"":""Doe"",""Age"":40}").RootElement.GetRawText());

// Act + Assert 2
_source.Last("Age > 0").GetRawText().Should().BeEquivalentTo(JsonDocument.Parse(@"{""Name"":""Doe"",""Age"":40}").RootElement.GetRawText());
}

[Fact]
public void Select()
{
Expand Down

0 comments on commit bf2e7fa

Please sign in to comment.