Skip to content

Commit

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

#region LastOrDefault
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

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

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JArray source, string predicate, params object?[] args)
{
return LastOrDefault(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JArray source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#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 @@ -500,6 +500,64 @@ public static JsonElement Last(this JsonDocument source, LambdaExpression lambda
}
#endregion Last

#region LastOrDefault
/// <summary>
/// Returns the last element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <returns>default if source is empty; otherwise, the last element in source.</returns>
public static JsonElement? LastOrDefault(this JsonDocument source)
{
Check.NotNull(source);

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

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

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

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JsonDocument source, string predicate, params object?[] args)
{
return LastOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </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? LastOrDefault(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#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 @@ -140,6 +140,16 @@ public void Last()
((string?)_source.First("Age > 30")["Name"]).Should().Be("Doe");
}

[Fact]
public void LastOrDefault()
{
// Act + Assert 1
((string?)_source.LastOrDefault("Age > 0")!["Name"]).Should().Be("Doe");

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

[Fact]
public void Select()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ public void Last()
_source.Last("Age > 0").GetRawText().Should().BeEquivalentTo(JsonDocument.Parse(@"{""Name"":""Doe"",""Age"":40}").RootElement.GetRawText());
}

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

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

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

[Fact]
public void Select()
{
Expand Down

0 comments on commit 9f074d0

Please sign in to comment.