-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionFunctions.cs
More file actions
29 lines (28 loc) · 1011 Bytes
/
Copy pathCollectionFunctions.cs
File metadata and controls
29 lines (28 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Linq;
using System.Collections.Generic;
namespace DevToolbox.Helpers
{
public static class CollectionFunctions
{
/// <summary>
/// Joins items into a natural language list, e.g. "A, B, and C"
/// </summary>
/// <param name="items"></param>
/// <param name="listSeparator"></param>
/// <param name="listTerminator"></param>
/// <returns></returns>
public static string Oxford(this IEnumerable<string> items, string listSeparator = ",", string listTerminator = "and")
{
//special case, e.g. "A and B", which should not have commas
if (items.Count() == 2)
{
return string.Join($" {listTerminator} ", items);
}
var lastItemIndex = items.Count() - 1;
var commaSet = items.Take(lastItemIndex).Select(i => $"{i}{listSeparator}");
var lastItem = items.Skip(lastItemIndex).Select(i => $"{listTerminator} {i}");
return string.Join(" ", commaSet.Concat(lastItem));
}
}
}