Skip to content

Commit

Permalink
Fix readable list
Browse files Browse the repository at this point in the history
  • Loading branch information
rupertbenbrook-aa committed Jul 14, 2023
1 parent 7a689bb commit 6b2e422
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions ExtLibs/AltitudeAngelWings/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AltitudeAngelWings
Expand All @@ -14,22 +15,32 @@ public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey
return val;
}

public static string AsReadableList(this IEnumerable<string> list)
public static string AsReadableList(this IEnumerable<string> items, string separator = ", ", string lastSeparator = " and ")
{
var builder = new StringBuilder();
var list = items?.ToList();
if (list == null || list.Count == 0)
{
return string.Empty;
}

foreach (var item in list)
if (list.Count == 1)
{
builder.Append(item);
builder.Append(" ");
return list[0];
}

if (builder.Length == 0)
var builder = new StringBuilder();
for (var index = 0; index < list.Count - 1; index++)
{
return string.Empty;
builder.Append(list[index]);
if (index < list.Count - 2)
{
builder.Append(separator);
}
}

builder.Length -= " ".Length;
builder.Append(lastSeparator);
builder.Append(list[list.Count - 1]);

return builder.ToString();
}
}
Expand Down

0 comments on commit 6b2e422

Please sign in to comment.