• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 26594173245

28 May 2026 06:28PM UTC coverage: 55.553% (+0.7%) from 54.902%
26594173245

push

github

pwelter34
Move JSON support, add docs and examples

1358 of 3215 branches covered (42.24%)

Branch coverage included in aggregate %.

103 of 234 new or added lines in 9 files covered. (44.02%)

371 existing lines in 26 files now uncovered.

4389 of 7130 relevant lines covered (61.56%)

312.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.89
/src/FluentCommand/Extensions/CollectionExtensions.cs
1
namespace FluentCommand.Extensions;
2

3
/// <summary>
4
/// Collection extension methods
5
/// </summary>
6
public static class CollectionExtensions
7
{
8
    /// <summary>
9
    /// Gets the first element from the <paramref name="source"/> that passes the test specified by <paramref name="predicate" />;
10
    /// otherwise the <paramref name="valueFactory"/> is called and the result is added to the source.
11
    /// </summary>
12
    /// <typeparam name="T">The type of the elements of <paramref name="source" />.</typeparam>
13
    /// <param name="source">An <see cref="T:System.Collections.Generic.ICollection`1" /> to get or add an element from.</param>
14
    /// <param name="predicate">A function to test each element for a condition.</param>
15
    /// <param name="valueFactory">The function used to generate a value when not found in the collection.</param>
16
    /// <returns>
17
    /// The value from <paramref name="valueFactory"/> if <paramref name="source" /> is empty or if no element passes the test specified by <paramref name="predicate" />;
18
    /// otherwise, the first element in <paramref name="source" /> that passes the test specified by <paramref name="predicate" />.
19
    /// </returns>
20
    public static T FirstOrAdd<T>(this ICollection<T> source, Func<T, bool> predicate, Func<T> valueFactory)
21
    {
22
        ArgumentNullException.ThrowIfNull(source);
144✔
23
        ArgumentNullException.ThrowIfNull(predicate);
144✔
24
        ArgumentNullException.ThrowIfNull(valueFactory);
144✔
25

26
        // return first match
27
        foreach (T element in source.Where(predicate))
293✔
28
            return element;
5✔
29

30
        // no match, use factory
31
        T value = valueFactory();
139✔
32
        source.Add(value);
139✔
33

34
        return value;
139✔
35
    }
5✔
36

37
    /// <summary>
38
    /// Removes the all the elements that match the conditions defined by the specified predicate.
39
    /// </summary>
40
    /// <typeparam name="T"><see cref="Type"/> of the items.</typeparam>
41
    /// <param name="collection">The collection to remove items from.</param>
42
    /// <param name="filter">The delegate that defines the conditions of the elements to remove.</param>
43
    /// <returns>The number of items removed.</returns>
44
    public static int RemoveAll<T>(this ICollection<T> collection, Func<T, bool> filter)
45
    {
UNCOV
46
        ArgumentNullException.ThrowIfNull(collection);
×
UNCOV
47
        ArgumentNullException.ThrowIfNull(filter);
×
48

49
        var removed = collection.Where(filter).ToArray();
×
50
        foreach (var remove in removed)
×
51
            collection.Remove(remove);
×
52

UNCOV
53
        return removed.Length;
×
54
    }
55
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc