Skip to content

Commit

Permalink
Skip
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 10, 2024
1 parent 8677ab5 commit cf34f09
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -765,21 +765,6 @@ public static JToken Single(this JArray source, LambdaExpression lambda)
#endregion Single

#region SingleOrDefault
/// <summary>
/// Returns the only element of a sequence, or a default value if the sequence
/// is empty; this method throws an exception if there is more than one element
/// in the sequence.
/// </summary>
/// <param name="source">A <see cref="JArray"/> to return the single element of.</param>
/// <returns>The single element of the input sequence, or default if the sequence contains no elements.</returns>
public static JToken? SingleOrDefault(this JArray source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJToken(queryable.SingleOrDefault());
}

/// <summary>
/// Returns the only element of a sequence that satisfies a specified condition or a default value if the sequence
/// is empty; and throws an exception if there is not exactly one element in the sequence.
Expand Down Expand Up @@ -827,6 +812,37 @@ public static JToken Single(this JArray source, LambdaExpression lambda)
}
#endregion SingleOrDefault

#region SkipWhile
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JArray"/> to return elements from.</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>An <see cref="JArray"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JArray SkipWhile(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object[]? args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source);
return ToJArray(() => queryable.SkipWhile(predicate, args));
}

/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JArray"/> to return elements from.</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>An <see cref="JArray"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JArray SkipWhile(this JArray source, string predicate, params object[]? args)
{
return SkipWhile(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion SkipWhile

#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 @@ -971,6 +971,53 @@ public static JsonElement Single(this JsonDocument source, LambdaExpression lamb
}
#endregion SingleOrDefault

#region Skip
/// <summary>
/// Bypasses a specified number of elements in a sequence and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</param>
/// <param name="count">The number of elements to skip before returning the remaining elements.</param>
/// <returns>A <see cref="JsonDocument"/> that contains elements that occur after the specified index in the input sequence.</returns>
public static JsonDocument Skip(this JsonDocument source, int count)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.Skip(count));
}
#endregion Skip

#region SkipWhile
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</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>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JsonDocument SkipWhile(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object[]? args)
{
Check.NotNull(source);
Check.NotNull(config);

var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.SkipWhile(predicate, args));
}

/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</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>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JsonDocument SkipWhile(this JsonDocument source, string predicate, params object[]? args)
{
return SkipWhile(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion SkipWhile

#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 @@ -260,7 +260,21 @@ public void SingleOrDefault()
}

[Fact]
public void Where_Select()
public void SkipWhile()
{
var json = @"[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
var source = JArray.Parse(json);

// Act
var result = source.SkipWhile("it > 5");

// Assert
var array = result.Select(x => x.Value<int>());
array.Should().ContainInOrder(6, 7, 8, 9, 0);
}

[Fact]
public void Where_With_Select()
{
// Act
var result = _source.Where("Age > 30").Select("Name");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Text.Json;
using System.Text.Json;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -270,7 +269,35 @@ public void Select_ResultType()
}

[Fact]
public void Where_Select()
public void Skip()
{
var json = @"[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
var source = JsonDocument.Parse(json);

// Act
var result = source.Skip(3);

// Assert
var array = result.RootElement.EnumerateArray().Select(x => x.GetInt32());
array.Should().ContainInOrder(4, 5, 6, 7, 8, 9, 0);
}

[Fact]
public void SkipWhile()
{
var json = @"[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
var source = JsonDocument.Parse(json);

// Act
var result = source.SkipWhile("it > 5");

// Assert
var array = result.RootElement.EnumerateArray().Select(x => x.GetInt32());
array.Should().ContainInOrder(6, 7, 8, 9, 0);
}

[Fact]
public void Where_With_Select()
{
// Act
var result = _source.Where("Age > 30").Select("Name");
Expand Down

0 comments on commit cf34f09

Please sign in to comment.