< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.ReadOnlyListExtension
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/ReadOnlyListExtension.cs
Line coverage
66%
Covered lines: 10
Uncovered lines: 5
Coverable lines: 15
Total lines: 77
Line coverage: 66.6%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IndexOf(...)16.66%16.67633.33%
FindIndex(...)83.33%6.17683.33%
FirstOrDefault(...)100%44100%

File(s)

/srv/git/jellyfin/src/Jellyfin.Extensions/ReadOnlyListExtension.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace Jellyfin.Extensions
 5{
 6    /// <summary>
 7    /// Static extensions for the <see cref="IReadOnlyList{T}"/> interface.
 8    /// </summary>
 9    public static class ReadOnlyListExtension
 10    {
 11        /// <summary>
 12        /// Finds the index of the desired item.
 13        /// </summary>
 14        /// <param name="source">The source list.</param>
 15        /// <param name="value">The value to fine.</param>
 16        /// <typeparam name="T">The type of item to find.</typeparam>
 17        /// <returns>Index if found, else -1.</returns>
 18        public static int IndexOf<T>(this IReadOnlyList<T> source, T value)
 19        {
 820            if (source is IList<T> list)
 21            {
 822                return list.IndexOf(value);
 23            }
 24
 025            for (int i = 0; i < source.Count; i++)
 26            {
 027                if (Equals(value, source[i]))
 28                {
 029                    return i;
 30                }
 31            }
 32
 033            return -1;
 34        }
 35
 36        /// <summary>
 37        /// Finds the index of the predicate.
 38        /// </summary>
 39        /// <param name="source">The source list.</param>
 40        /// <param name="match">The value to find.</param>
 41        /// <typeparam name="T">The type of item to find.</typeparam>
 42        /// <returns>Index if found, else -1.</returns>
 43        public static int FindIndex<T>(this IReadOnlyList<T> source, Predicate<T> match)
 44        {
 2245            if (source is List<T> list)
 46            {
 047                return list.FindIndex(match);
 48            }
 49
 7650            for (int i = 0; i < source.Count; i++)
 51            {
 2752                if (match(source[i]))
 53                {
 1154                    return i;
 55                }
 56            }
 57
 1158            return -1;
 59        }
 60
 61        /// <summary>
 62        /// Get the first or default item from a list.
 63        /// </summary>
 64        /// <param name="source">The source list.</param>
 65        /// <typeparam name="T">The type of item.</typeparam>
 66        /// <returns>The first item or default if list is empty.</returns>
 67        public static T? FirstOrDefault<T>(this IReadOnlyList<T>? source)
 68        {
 22869            if (source is null || source.Count == 0)
 70            {
 4571                return default;
 72            }
 73
 18374            return source[0];
 75        }
 76    }
 77}