Skip to content

Commit

Permalink
refactor: align API of shim with the built-in API. (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer authored Oct 3, 2024
1 parent b802059 commit c6fc8e5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/IbanNet/Extensions/ChunkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class ChunkExtensions
/// <param name="source">The source sequence.</param>
/// <param name="size">The size of each chunk to split the <paramref name="source" /> into.</param>
/// <returns>an enumerable of chunks</returns>
public static IEnumerable<IEnumerable<TSource>> Chunk<TSource>(this IEnumerable<TSource> source, int size)
public static IEnumerable<TSource[]> Chunk<TSource>(this IEnumerable<TSource> source, int size)
{
if (source is null)
{
Expand All @@ -32,23 +32,23 @@ public static IEnumerable<IEnumerable<TSource>> Chunk<TSource>(this IEnumerable<
return ChunkIterator(source, size);
}

private static IEnumerable<IEnumerable<TSource>> ChunkIterator<TSource>(this IEnumerable<TSource> source, int size)
private static IEnumerable<TSource[]> ChunkIterator<TSource>(this IEnumerable<TSource> source, int size)
{
var chunks = new List<TSource>(size);
foreach (TSource item in source)
{
chunks.Add(item);
if (chunks.Count == size)
{
yield return chunks;
yield return chunks.ToArray();

chunks = new List<TSource>(size);
chunks.Clear();
}
}

if (chunks.Count > 0)
{
yield return chunks;
yield return chunks.ToArray();
}
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions test/IbanNet.Tests/Extensions/ChunkExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public void Given_collection_when_chunking_it_should_return_expected_chunks(int
actual.Should().HaveCount(expectedPartitions);
actual.Take(actual.Count - 1)
.Should()
.OnlyContain(inner =>
#if NET6_0_OR_GREATER
inner.Length == size,
#else
inner.Count() == size,
#endif
.OnlyContain(inner => inner.Length == size,
"all but the last should at least be of the requested size"
);
actual.Last().Should().HaveCount(expectedLastPartitionSize, "the last partition can be less than or equal to the requested size");
Expand Down

0 comments on commit c6fc8e5

Please sign in to comment.