Skip to content

Commit

Permalink
Distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 9, 2024
1 parent 53b7ab3 commit 9080f0c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,50 @@ public static int Count(this JArray source, LambdaExpression lambda)
}
#endregion Count

#region DefaultIfEmpty
/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return a default value for if empty.</param>
/// <returns>An <see cref="JArray"/> that contains default if source is empty; otherwise, source.</returns>
public static JArray DefaultIfEmpty(this JArray source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJArray(queryable.DefaultIfEmpty);
}

/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return a default value for if empty.</param>
/// <param name="defaultValue">The value to return if the sequence is empty.</param>
/// <returns>An <see cref="JArray"/> that contains defaultValue if source is empty; otherwise, source.</returns>
public static JArray DefaultIfEmpty(this JArray source, object? defaultValue)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJArray(() => queryable.DefaultIfEmpty(defaultValue));
}
#endregion

#region Distinct
/// <summary>
/// Returns distinct elements from a sequence by using the default equality comparer to compare values.
/// </summary>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <returns>An <see cref="JArray"/> that contains distinct elements from the source sequence.</returns>
public static JArray Distinct(this JArray source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJArray(queryable.Distinct);
}
#endregion Distinct

#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 @@ -282,6 +282,50 @@ public static int Count(this JsonDocument source, LambdaExpression lambda)
}
#endregion Count

#region DefaultIfEmpty
/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return a default value for if empty.</param>
/// <returns>An <see cref="JsonDocument"/> that contains default if source is empty; otherwise, source.</returns>
public static JsonDocument DefaultIfEmpty(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonDocumentArray(queryable.DefaultIfEmpty);
}

/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return a default value for if empty.</param>
/// <param name="defaultValue">The value to return if the sequence is empty.</param>
/// <returns>An <see cref="JsonDocument"/> that contains defaultValue if source is empty; otherwise, source.</returns>
public static JsonDocument DefaultIfEmpty(this JsonDocument source, object? defaultValue)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.DefaultIfEmpty(defaultValue));
}
#endregion

#region Distinct
/// <summary>
/// Returns distinct elements from a sequence by using the default equality comparer to compare values.
/// </summary>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <returns>An <see cref="JsonDocument"/> that contains distinct elements from the source sequence.</returns>
public static JsonDocument Distinct(this JsonDocument source)
{
Check.NotNull(source);

var queryable = ToQueryable(source);
return ToJsonDocumentArray(queryable.Distinct);
}
#endregion Distinct

#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
Expand Down Expand Up @@ -352,17 +396,7 @@ private static JsonDocument ToJsonDocumentArray(Func<IQueryable> func)
var array = new List<object>();
foreach (var dynamicElement in func())
{
object element;
if (dynamicElement is DynamicClass dynamicClass)
{
element = JsonElementUtils.FromObject(dynamicClass);
}
else
{
element = dynamicElement;
}

//var element = dynamicElement is DynamicClass dynamicClass ? JsonElementUtils.FromObject(dynamicClass) : dynamicElement;
var element = dynamicElement is DynamicClass dynamicClass ? JsonElementUtils.FromObject(dynamicClass) : dynamicElement;
array.Add(element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ public void Count()
result2.Should().Be(1);
}

[Fact]
public void Distinct()
{
var json = @"[
{
""Name"": ""John""
},
{
""Name"": ""Doe""
},
{
""Name"": ""John""
}
]";
var source = JArray.Parse(json);

// Act
var result = source.Select("Name").Distinct();

// Assert
result.Should().HaveCount(2);
}

[Fact]
public void Select()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ public void Count()
result2.Should().Be(1);
}

[Fact]
public void Distinct()
{
var json = @"[
{
""Name"": ""John""
},
{
""Name"": ""Doe""
},
{
""Name"": ""John""
}
]";
var source = JsonDocument.Parse(json);

// Act
var result = source.Select("Name").Distinct();

// Assert
result.RootElement.EnumerateArray().Should().HaveCount(2);
}

[Fact]
public void Select()
{
Expand Down

0 comments on commit 9080f0c

Please sign in to comment.