< Summary

Information
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 110
Line coverage: 100%
Branch coverage
58%
Covered branches: 7
Total branches: 12
Branch coverage: 58.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToFilter(...)50%88100%
IsValidFile(...)100%22100%
Concat(...)50%22100%
ToFilterString(...)100%11100%
FilterByExtensions(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/Extensions/FileExtensionExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FileExtensionExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.IO;
 10using System.Linq;
 11using MyNet.IO.FileExtensions;
 12using MyNet.Primitives;
 13
 14#pragma warning disable IDE0130 // Namespace does not match folder structure
 15namespace MyNet.IO;
 16#pragma warning restore IDE0130 // Namespace does not match folder structure
 17
 18public static class FileExtensionExtensions
 19{
 20    extension(FileExtensionGroup group)
 21    {
 22        /// <summary>
 23        /// Converts the <see cref="FileExtensionGroup"/> to a <see cref="FileFilter"/> that can be used in file dialogs
 24        /// </summary>
 25        /// <param name="translate">A function to translate the group's key.</param>
 26        /// <returns>A <see cref="FileFilter"/> representing the group's extensions.</returns>
 27        public FileFilter ToFilter(Func<string, string?>? translate = null)
 28        {
 929            var pattern = string.Join(";", group.Extensions.Select(e => $"*{e.Value}"));
 30
 931            var title = translate?.Invoke(group.Key) ?? group.Key.OrEmpty();
 32
 933            if (!string.IsNullOrWhiteSpace(pattern) && pattern != "*.*")
 934                title = $"{title} ({pattern})";
 35
 936            return new(title, pattern);
 37        }
 38
 39        /// <summary>
 40        /// Determines whether the specified file path has a valid extension based on the extensions defined in the <see
 41        /// </summary>
 42        /// <param name="filePath">The file path to check.</param>
 43        /// <returns>True if the file has a valid extension; otherwise, false.</returns>
 44        public bool IsValidFile(string filePath)
 45        {
 946            if (string.IsNullOrWhiteSpace(filePath))
 347                return false;
 48
 649            var ext = Path.GetExtension(filePath);
 650            return group.Extensions.Any(e =>
 651                string.Equals(e.Value, ext, StringComparison.OrdinalIgnoreCase));
 52        }
 53
 54        /// <summary>
 55        /// Concatenates the extensions of the current <see cref="FileExtensionGroup"/> with those of another <see cref=
 56        /// </summary>
 57        /// <param name="second">The second <see cref="FileExtensionGroup"/> to concatenate with the current group.</par
 58        /// <param name="key">The key for the new <see cref="FileExtensionGroup"/>. If not provided, the key of the curr
 59        /// <returns>A new <see cref="FileExtensionGroup"/> that combines the extensions from both groups.</returns>
 60        public FileExtensionGroup Concat(
 61            FileExtensionGroup second,
 62            string? key = null)
 363            => new(key ?? group.Key, group.Extensions.Concat(second.Extensions));
 64    }
 65
 66    extension(IEnumerable<FileExtensionGroup> groups)
 67    {
 68        /// <summary>
 69        /// Converts a collection of <see cref="FileExtensionGroup"/> items to a filter string that can be used in file 
 70        /// </summary>
 71        /// <param name="translate">A function to translate the group's key.</param>
 72        /// <returns>A filter string that can be used in file dialogs.</returns>
 73        public string ToFilterString(Func<string, string?>? translate = null)
 374            => string.Join("|",
 375                groups.SelectMany(g =>
 376                {
 377                    var (title, pattern) = g.ToFilter(translate);
 378                    return new[] { title, pattern };
 379                }));
 80    }
 81
 82    extension(IEnumerable<string> files)
 83    {
 84        /// <summary>
 85        /// Filters a collection of file paths based on the extensions defined in a <see cref="FileExtensionGroup"/>. Th
 86        /// </summary>
 87        /// <param name="group">The file extension group to filter by.</param>
 88        /// <returns>A collection of file paths that have valid extensions according to the specified group.</returns>
 89        public IEnumerable<string> FilterByExtensions(FileExtensionGroup group)
 90        {
 391            var allowed = group.Extensions
 392                .Select(x => x.Value)
 393                .ToHashSet(StringComparer.OrdinalIgnoreCase);
 94
 395            return files.Where(file =>
 396            {
 397                var ext = Path.GetExtension(file);
 398                return allowed.Contains(ext);
 399            });
 100        }
 101    }
 102}
 103
 104/// <summary>
 105/// Represents a file filter that can be used in file dialogs. The <see cref="FileFilter"/> record contains a title and 
 106/// </summary>
 107/// <param name="Title">The title of the file filter.</param>
 108/// <param name="Pattern">The pattern of the file filter.</param>
 109public sealed record FileFilter(string Title, string Pattern);
 110