-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
41 lines (40 loc) · 1.48 KB
/
Extensions.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
namespace CobbleBuild {
internal static class ListExtensions {
/// <summary>
/// Fills up a list with fillValue until it reaches targetCount
/// </summary>
public static List<T> FillUpTo<T>(this List<T> list, int targetCount, T fillValue) {
if (list.Count < targetCount) {
list.AddRange(Enumerable.Repeat(fillValue, targetCount - list.Count));
}
return list;
}
/// <summary>
/// Equivalent to the javascript .shift() function.
/// </summary>
/// <returns>The element shifted out.</returns>
/// <exception cref="InvalidOperationException">List is empty.</exception>
public static T Shift<T>(this List<T> list) {
if (list == null || list.Count == 0) {
throw new InvalidOperationException("Cannot shift an element from an empty list.");
}
T firstElement = list[0];
list.RemoveAt(0);
return firstElement;
}
/// <summary>
/// Removes objects in params if they exist in the list.
/// Does not mutate the original list.
/// </summary>
public static List<T> RemoveIfInList<T>(this List<T> list, params T[] items) {
var listCopy = list.ToArray().ToList();
foreach (var item in items) {
var index = listCopy.FindIndex(x => x?.Equals(item) == true);
if (index != -1) {
listCopy.RemoveAt(index);
}
}
return listCopy;
}
}
}