Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 2b75171

Browse files
Adding some extension to spool strings
1 parent 61ee04b commit 2b75171

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace GitHub.Unity
6+
{
7+
public static class ListExtensions
8+
{
9+
public static string Join<T>(this IEnumerable<T> list, string separator)
10+
{
11+
if (list == null)
12+
return null;
13+
return String.Join(separator, list.Select(x => x?.ToString()).ToArray());
14+
}
15+
16+
public static IEnumerable<IList<string>> Spool<T>(this IEnumerable<T> items, int spoolLength)
17+
{
18+
var currentSpoolLength = 0;
19+
var currentList= new List<string>();
20+
21+
foreach (var item in items)
22+
{
23+
var itemValue = item.ToString();
24+
var itemValueLength = itemValue.Length;
25+
26+
if (currentSpoolLength + itemValueLength > spoolLength)
27+
{
28+
yield return currentList;
29+
30+
currentSpoolLength = 0;
31+
currentList = new List<string>();
32+
}
33+
34+
currentSpoolLength += itemValueLength;
35+
currentList.Add(itemValue);
36+
}
37+
38+
if (currentList.Any())
39+
{
40+
yield return currentList;
41+
}
42+
}
43+
}
44+
}

src/GitHub.Api/Extensions/StringExtensions.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,6 @@ public static string LeftBeforeLast(this string s, char search)
151151
}
152152
}
153153

154-
public static class ListExtensions
155-
{
156-
public static string Join<T>(this IEnumerable<T> list, string separator)
157-
{
158-
if (list == null)
159-
return null;
160-
return String.Join(separator, list.Select(x => x?.ToString()).ToArray());
161-
}
162-
}
163-
164154
public struct StringResult
165155
{
166156
public string Chunk;

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="Application\ApplicationInfo.cs" />
9797
<Compile Include="Application\LoginResult.cs" />
9898
<Compile Include="Application\AppConfiguration.cs" />
99+
<Compile Include="Extensions\ListExtensions.cs" />
99100
<Compile Include="Helpers\AssemblyResources.cs" />
100101
<Compile Include="Authentication\IKeychain.cs" />
101102
<Compile Include="Authentication\Keychain.cs" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using GitHub.Unity;
4+
using NUnit.Framework;
5+
6+
namespace UnitTests
7+
{
8+
[TestFixture]
9+
public class ListExtensionTests
10+
{
11+
[Test]
12+
public void SpoolTest()
13+
{
14+
var results = new[] { "qwer", "asdf", "zxcv", "wert", "1234" }.Spool(10).ToArray();
15+
results.Length.Should().Be(3);
16+
results[0].Count.Should().Be(2);
17+
results[0][0].Should().Be("qwer");
18+
results[0][1].Should().Be("asdf");
19+
20+
results[1].Count.Should().Be(2);
21+
results[1][0].Should().Be("zxcv");
22+
results[1][1].Should().Be("wert");
23+
24+
results[2].Count.Should().Be(1);
25+
results[2][0].Should().Be("1234");
26+
}
27+
}
28+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</ItemGroup>
8282
<ItemGroup>
8383
<Compile Include="Authentication\KeychainTests.cs" />
84+
<Compile Include="Extensions\ListExtensionTests.cs" />
8485
<Compile Include="Git\ValidationTests.cs" />
8586
<Compile Include="Git\GitConfigTests.cs" />
8687
<Compile Include="IO\BranchListOutputProcessorTests.cs" />

0 commit comments

Comments
 (0)