Skip to content

Commit

Permalink
Count
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 9, 2024
1 parent c03037b commit 53b7ab3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,64 @@ public static IQueryable Cast(this JArray source, string typeName)
}
#endregion Cast

#region Count
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be counted.</param>
/// <returns>The number of elements in the input sequence.</returns>
public static int Count(this JArray source)
{
Check.NotNull(source);

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

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be counted.</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 number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

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

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be counted.</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 number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JArray source, string predicate, params object?[] args)
{
return Count(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be counted.</param>
/// <param name="lambda">A cached Lambda Expression.</param>
/// <returns>The number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JArray source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#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 @@ -224,6 +224,64 @@ public static IQueryable Cast(this JsonDocument source, string typeName)
}
#endregion Cast

#region Count
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</param>
/// <returns>The number of elements in the input sequence.</returns>
public static int Count(this JsonDocument source)
{
Check.NotNull(source);

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

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</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 number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);

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

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</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 number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JsonDocument source, string predicate, params object?[] args)
{
return Count(source, SystemTextJsonParsingConfig.Default, predicate, args);
}

/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</param>
/// <param name="lambda">A cached Lambda Expression.</param>
/// <returns>The number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JsonDocument source, LambdaExpression lambda)
{
Check.NotNull(source);

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

#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 @@ -77,6 +77,22 @@ public void Cast()
resultTypeName.Should().Contain(expected);
}

[Fact]
public void Count()
{
// Act 1
var result1 = _source.Count();

// Assert 1
result1.Should().Be(2);

// Act 2
var result2 = _source.Count("Age > 30");

// Assert 2
result2.Should().Be(1);
}

[Fact]
public void Select()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ public void Cast()
resultTypeName.Should().Contain(expected);
}

[Fact]
public void Count()
{
// Act 1
var result1 = _source.Count();

// Assert 1
result1.Should().Be(2);

// Act 2
var result2 = _source.Count("Age > 30");

// Assert 2
result2.Should().Be(1);
}

[Fact]
public void Select()
{
Expand Down

0 comments on commit 53b7ab3

Please sign in to comment.